【发布时间】:2011-07-05 10:55:28
【问题描述】:
我用 getimagesize 函数获取图像的宽度和高度,如下所示:
list($width,$height) = getimagesize($source_pic);
如何使用 IF 条件检查 getimagesize 函数执行时没有错误,并且 $width 和 $height 得到非空、非零值?
【问题讨论】:
标签: php
我用 getimagesize 函数获取图像的宽度和高度,如下所示:
list($width,$height) = getimagesize($source_pic);
如何使用 IF 条件检查 getimagesize 函数执行时没有错误,并且 $width 和 $height 得到非空、非零值?
【问题讨论】:
标签: php
if ($size = getimagesize($source_pic)) {
list($width,$height) = $size;
if($height > 0 && $width > 0) {
// do stuff
}
}
【讨论】:
if ($width === NULL) {
//handle error
}
如果有错误getimagesize 返回FALSE,而不是数组,那么list 赋值将导致变量为NULL。
【讨论】:
这就够了:
list($width, $height) = getimagesize($source_pic);
if( $width>0 && $height>0 ){
// Valid image with known size
}
如果不是有效的图像,$width 和 $height 都将是NULL。如果它是一个有效的图像,但 PHP 无法确定它的尺寸,它们将是 0。
某些格式可能不包含图像或 可能包含多个图像。在这些 情况下,getimagesize() 可能不是 能够正确确定图像 尺寸。 getimagesize() 将返回零 这些情况下的宽度和高度。
【讨论】:
$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
}
【讨论】: