473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

How can I check the size of an image file ?

290 100+
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_contents($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_contents() 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 2724
Markus
6,050 Expert 4TB
See filesize().
Mar 14 '10 #2
Atli
5,058 Expert 4TB
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 100+
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_content() 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 Expert 4TB
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 100+
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 Expert 4TB
Yes, that's exactly it. Does it work?
Mar 15 '10 #7
jeddiki
290 100+
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
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
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...
12
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...
12
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...
2
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...
9
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...
11
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....
2
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...
8
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
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.