3

I get the width and height of image with getimagesize function, like below:

list($width,$height) = getimagesize($source_pic);

How can I use IF condition to check that the getimagesize function executed without error and $width and $height got non-empty, non-zero values?

4 Answers 4

6
if ($size = getimagesize($source_pic)) {
    list($width,$height) = $size;
    if($height > 0 && $width > 0) {
        // do stuff
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Please note that this answer does not conform to all question requirements. In particular, it won't detect zero values.
3
if ($width === NULL) {
    //handle error
}

If there's an error getimagesize returns FALSE, not an an array, so the list assignment will result in the variables being NULL.

1 Comment

You still need to test against zero. According to the manual, there are cases when you can get such value.
1

This should be enough:

list($width, $height) = getimagesize($source_pic);
if( $width>0 && $height>0 ){
    // Valid image with known size
}

If it isn't a valid image, both $width and $height will be NULL. If it's a valid image but PHP could not determine its dimensions, they'll be 0.

A note from the manual:

Some formats may contain no image or may contain multiple images. In these cases, getimagesize() might not be able to properly determine the image size. getimagesize() will return zero for width and height in these cases.

Comments

1
$size = getimagesize('image.jpg');
list($height,$width) = getimagesize('image.jpg');

if($height>0 && $width>0){
   //it comes into if block if and only if both are not null
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.