获取文件宽高在 PHP 中有一个简单函数 getimagesize
。只需要传递文件名即可。
getimagesize ( string $filename [, array &$imageinfo ] ) : array
使用方法:
<?php
$image_arr = getimagesize('https://upyun.laravelcode.cn/uploads/images/resources/201906/24/jPINglfSMseh2Ri1g9JbgIY8ykisfe6mfJJmTh5P.jpeg');
return $image_arr;
返回内容为:
{
"0": 563,
"1": 1000,
"2": 2,
"3": "width=\"563\" height=\"1000\"",
"bits": 8,
"channels": 3,
"mime": "image/jpeg"
}
返回结果说明:
可见返回内容为数组,我们获取数组下标即可
<?php
$width = $image_arr[0];
$height = $image_arr[1];
$type = $image_arr[6];
另外、我们也可以使用 list 来获取数据.
<?php
list($width, $height, $type) = getimagesize('https://upyun.laravelcode.cn/uploads/images/resources/201906/24/jPINglfSMseh2Ri1g9JbgIY8ykisfe6mfJJmTh5P.jpeg');
return [
"width" => $width,
'height' => $height,
'type' => $type,
];