How can I check the size of an image file ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    How can I check the size of an image file ?

    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 )

    Code:
    $image_data = file_get_contents($url);
    
    IF( strlen($image_data) != ?? ) {  //   don't know what this should be
    
      if($image_data === FALSE) {
        write_log("$ctr )$id PROBLEM with READ \r\n");	
        }
      else {
        write_log("$ctr )Processing $image_loc \r\n");
        $file_size = file_put_contents($image_loc, $image_data);
        if($file_size  === FALSE) {
            write_log("$ctr )$id PROBLEM with WRITE \r\n");
    	}
        else {
    	write_log("$ctr )$id Image created size : $file_size\r\n");		 
    	}
      }
     }  // END IF
    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 ?
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    See filesize().

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      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.
      [code=php]<?php
      $headers = get_headers("UR L to non-existing image");
      print_r($header s);
      ?>[/code]
      If the first element has 404, you can use the above method.

      Comment

      • jeddiki
        Contributor
        • Jan 2009
        • 290

        #4
        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 :)

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          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.

          Comment

          • jeddiki
            Contributor
            • Jan 2009
            • 290

            #6
            OK - I see what you mean.

            I have incorporated that into my script:

            Code:
            if(file_exists($image_loc)) {
              echo "<img src=\"$image_file\" width='300px' height='200px' alt='Please wait while thumbnail for $title_sht loads.'>";
              }
            else {
              $url = "http://www.sd5.info/imager/webthumb.php?prod_id=$Db_prod";
              $url_head = get_headers($url);
              if($url_head === FALSE) {
                 echo "Image Unavailable";
                 }
              else {	
                 $head_chk = strpos ($url_head[0], '200 OK')
                 if( strpos($url_head[0], '200 OK') !== FALSE) {
            	$image_data = file_get_contents($url);
            	file_put_contents($image_loc, $image_data);
            	echo "<img src=\"$image_file\"  width='300' height='200' alt='Please wait while thumbnail loads ...'>";
                   }
                 else {
            	 echo "Image Not Available";
            	 }
               }
            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 ?



            .

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Yes, that's exactly it. Does it work?

              Comment

              • jeddiki
                Contributor
                • Jan 2009
                • 290

                #8
                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 :)


                .

                Comment

                Working...