View Full Version : How can I check the size of an image ?

03-14-2010, 07:26 PM

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 )

$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_contents() then check the file size, then delete if
it is the "standard" size. But that seems a waste of resources.

Any ideas ?


03-15-2010, 04:30 AM
Size? Do you mean filesize, or W X H (dimension) size?


Image dimensions:

// Get new dimensions
list($width, $height) = getimagesize($filename);

03-15-2010, 08:09 AM
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 am not meaning the 404 page
but their standard "Can't Create Image" image"

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.

I know what filesize() does, I would just like to know if there is way
of finding the size of the file before it is written by looking at the size of
the string that holds the file data from file_get_contents().

Maybe I have to store the image first 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 :)