473,698 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I check the size of an image file ?

290 Contributor
I want to check the size of an image file before I write it.

If it is a certain size, then I won't write it.

Can I check the future size of the file by
using strlen() on the file_get_conten ts($url);?

Here is my script:

First I get the image from an image website,
but if the image is the standard error image ( "Can't find image" ),
then I don't want to save it:

( The size of this standard error image is always 4.49Kb )

Expand|Select|Wrap|Line Numbers
  1. $image_data = file_get_contents($url);
  2.  
  3. IF( strlen($image_data) != ?? ) {  //   don't know what this should be
  4.  
  5.   if($image_data === FALSE) {
  6.     write_log("$ctr )$id PROBLEM with READ \r\n");    
  7.     }
  8.   else {
  9.     write_log("$ctr )Processing $image_loc \r\n");
  10.     $file_size = file_put_contents($image_loc, $image_data);
  11.     if($file_size  === FALSE) {
  12.         write_log("$ctr )$id PROBLEM with WRITE \r\n");
  13.     }
  14.     else {
  15.     write_log("$ctr )$id Image created size : $file_size\r\n");         
  16.     }
  17.   }
  18.  }  // END IF
  19.  
Or is there a better way to do this ?

The other thing I could do is write the file with the
file_put_conten ts() then check the file size, then delete if
it is the "standard" size. But that seems a waste of resources.

Any ideas ?
Mar 14 '10 #1
7 2742
Markus
6,050 Recognized Expert Expert
See filesize().
Mar 14 '10 #2
Atli
5,058 Recognized Expert Expert
Hey.

The "Can't find Image" error is a standard image? Does the request return the normal 200 status or does it return 404 (as it should, really).

If it returns 404, you could start by doing a get_headers on the URL of the image you are downloading. If it returns 404 ignore it, but if it returns 200 you would download it.

Try it. Do this on an image URL that you know is invalid (that gives you the error image) and see what it says.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $headers = get_headers("URL to non-existing image");
  3. print_r($headers);
  4. ?>
If the first element has 404, you can use the above method.
Mar 14 '10 #3
jeddiki
290 Contributor
Thanks for reply,

Maybe I was not clear.

I am using an image serving service, and if it can not create the image
then it serves a "standard" "Can't Create Image" image and returns that.

It then queues the image for later recovery. So I was not meaning the 404 page
but their standard "Can't Create Image" image" (4.49KB )

Now if it serves that image, then I do not want to store it because my
script will then think that we have the correct image available.

Maybe I need to store it with file_put_conten t() and then check its
filesize() and then delete it with unlink().

I was just trying to avoid the unnecesary steps by checking first.

Hope that explains it more fully :)
Mar 15 '10 #4
Atli
5,058 Recognized Expert Expert
Whether it is giving you an image or not, it will have to return a HTTP status header. And since it is giving you an error (whether in the form of an image or an error page) it may - or rather; should - not be giving you the standard 200 (success) header. It should be giving you 404 or a 500 header instead.

If that is true, then you could use that to determine whether or not you should be saving the image it is giving you without having to compare image sizes or something like that.
Mar 15 '10 #5
jeddiki
290 Contributor
OK - I see what you mean.

I have incorporated that into my script:

Expand|Select|Wrap|Line Numbers
  1. if(file_exists($image_loc)) {
  2.   echo "<img src=\"$image_file\" width='300px' height='200px' alt='Please wait while thumbnail for $title_sht loads.'>";
  3.   }
  4. else {
  5.   $url = "http://www.sd5.info/imager/webthumb.php?prod_id=$Db_prod";
  6.   $url_head = get_headers($url);
  7.   if($url_head === FALSE) {
  8.      echo "Image Unavailable";
  9.      }
  10.   else {    
  11.      $head_chk = strpos ($url_head[0], '200 OK')
  12.      if( strpos($url_head[0], '200 OK') !== FALSE) {
  13.     $image_data = file_get_contents($url);
  14.     file_put_contents($image_loc, $image_data);
  15.     echo "<img src=\"$image_file\"  width='300' height='200' alt='Please wait while thumbnail loads ...'>";
  16.        }
  17.      else {
  18.      echo "Image Not Available";
  19.      }
  20.    }     
  21.  
I hope I have done it right.

I chose two different error messages so I can tell
what might be wrong.

Is this what you had in mind ?



.
Mar 15 '10 #6
Atli
5,058 Recognized Expert Expert
Yes, that's exactly it. Does it work?
Mar 15 '10 #7
jeddiki
290 Contributor
Sorry for delay,

It was a got try - but as I suspected, the image returned image shows up as a successful response even though it is not the requested image.

When the image you want is not available it
sends this standard image:




And as it is not an error page, my script has now stored
it as the correct image to be served.

So in order to cache correct images I wanted to check
the file size.

I will print out the size of the image string and see what I get ...

back soon.

.EDIT:

OK back again.

Yer looks like my idea to use the strlen will work,
so I don't need to save the image if it a certain length.
BUT I am glad I asked the question because now I have an extra check in place for when there is not a proper response from the server.

Thanks :)


.
Mar 15 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
34742
by: newcomer | last post by:
Is there a way to check if a file exists in Javascript? This is what I'm trying to do: if(thisfile.htm exists) do this else do that
9
4086
by: Deepa | last post by:
Hi All, I'm facing problem displaying image of size 5000X5000 .My window size is smaller than image size so i'm not able to see the complete image.i can use scroll bars to view the image but i need to rotate the image and the see the result everytime. For this my boss has given me suggestion to display only 20% of the image on window ,and rotation has to be applied to original copy.
12
695
by: Ken | last post by:
How can I determine a image file size before uploading it? I would like to make sure the size is under a maximum before taking the time to upload it. If I have to upload the file before determining the size, it could take a few minutes (Mbs) on a slow connection before I can determine if the size is too large. Thanks!
12
2674
by: Xah Lee | last post by:
would anyone like to translate the following perl script to Python or Scheme (scsh)? the file takes a inpath, and report all html files in it above certain size. (counting inline images) also print a sorted report of html files and their size. (a copy of the script is here: http://xahlee.org/_scripts/check_file_size.pl )
2
8531
by: Sanjeeva Reddy | last post by:
hai Anti Keskinen, i have used the following code MyListView->LargeImageList->ImageSize = gcnew System::Drawing::Size(100, 100); // Sets large image size to 100, 100 here i am getting error like "gcnew is undeclared error",how to deeclare 'gcnew" and when i am using in runtime to change the size of images in imagelist in listview control in .net(forms application) by chnging one trckbar(like tb1->Value),
9
1670
by: Peter Proost | last post by:
Hi group, I've got a question about the size of a saved bitmap. What I need to do is open a bitmap if it needs resizing, resize and save it. This isn't a problem, but the problem I have is that the original image has got an 8 bit indexed pixelformat, but you can't create a graphics object for this pixelformat so my bitmap uses the default pixelformat: Format32bppArgb, but this ofcourse result in a much larger file size (420kB instead of...
11
9570
by: Parrot | last post by:
Is there any routine I can call to reduce the size of an image file after uploading a file from a client. I am looking to reduce file sizes programmatically using C# in my web page after uploading. I know that Photoshop can do it but I need to do it dynamically in my web page. I tried doing it myself with some of the Bitmap thumbnail instructions but the resulting image is very bad. I need a file reduction routine that creates a good image...
2
1862
by: Geoff | last post by:
Hi Previously I was able to check for an uploaded file to be a jpg or a gif by using the exif_imagetype() function. I had to change from hosting provider and the new one doesn't want to enable Zend on their php. I would like to have a way to check for a file to be a jpg or gif file in another way.
8
2342
by: miladhatam | last post by:
can i change the size of a file dynamically ? for example have 100 Kb and i want to decrease it to 20 Kb thanks
2
8635
butro
by: butro | last post by:
I have a form which can check whether is an image or not. But when i try to choose a real image whose name include more than one "." character, the script fails. The split function can not be detect the file extensions when the string has "." more than one. Here is my codes. This script allows only image (i mean, .jpg, .gif, etc. file extensions). <HTML> <Head> <Script Language=JavaScript> function getStats(fName){ nullIMG.src =...
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9026
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2328
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.