I get the width and height of image with getimagesize function, like below:
list($width,$height) = getimagesize($sourc开发者_StackOverflow社区e_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?
if ($size = getimagesize($source_pic)) {
list($width,$height) = $size;
if($height > 0 && $width > 0) {
// do stuff
}
}
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
.
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.
$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
}
精彩评论