PHP中的getimagesize()函数是一个内置函数,用于获取图像的大小。此函数接受文件名作为参数,并确定图像大小,并返回包含文件类型和图像高度/宽度的尺寸。
用法:
array getimagesize( $filename, $image_info )
参数:该函数接受上述和以下描述的两个参数:
$filename:它是指定图像文件名的必需参数。
$image_info:它是一个可选参数,允许您从图像文件中提取一些扩展信息,例如将不同的JPG APP标记作为关联数组。
返回值:它返回尺寸以及文件类型和高度/宽度文本字符串。
异常:
如果可能不包含图像或包含多个图像的格式,则getimagesize()函数的宽度和高度返回零。
imageinfo参数仅支持JFIF文件。
如果无法访问文件名图像,则getimagesize()函数将生成E_WARNING级别的错误。
如果读取中存在任何错误,则getimagesize()将生成E_NOTICE级错误。
以下示例程序旨在说明PHP中的getimagesize()函数:
注意:下面给出的图像(geeks.png)在以下程序中使用。
程序1:
// Calling getimagesize() function
$image_info = getimagesize("geeks.png");
print_r($image_info);
?>
输出:
Array ( [0] => 667
[1] => 184
[2] => 3
[3] => width="667" height="184"
[bits] => 8
[mime] => image/png )
程序2:
// Calling getimagesize() function
list($width, $height, $type, $attr) = getimagesize("geeks.png");
// Displaying dimensions of the image
echo "Width of image : " . $width . "
";
echo "Height of image : " . $height . "
";
echo "Image type :" . $type . "
";
echo "Image attribute :" .$attr;
?>
输出:
Width of image : 667
Height of image : 184
Image type :3
Image attribute :width="667" height="184"