getimagesize() 在 PHP 7.1 中不起作用

getimagesize() not working in PHP 7.1

我有一个代码可以获取 imgur link 并使用简单的方法获取图像的高度和宽度:

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

我是 运行 PHP 7.1,在我使用 getimagesize() 之前一切正常。调用该函数时,它每次都返回 false。然后我恢复到 PHP 5.3,代码立即运行。

我只是想问一下 getimagesize() 在 7.1 中停止工作是否有原因?文档说 PHP 7 所以我想我只是很困惑。

最好的猜测,$link 是一个 url,这意味着它需要 php.ini 设置 allow_url_fopentrue 才能检查 getimagesize它,并且您在 php5 的 php.ini 中将其设置为 true,在 php7 的 php.ini 中设置为 false - 这会导致你描述的问题。与 php 版本和 php.ini 设置兼容的替代方案是:

$tmp=tmpfile();
$file=stream_get_meta_data($tmp)['uri'];
$ch=curl_init($link);
curl_setopt_array($ch,array(
CURLOPT_FILE=>$tmp,
CURLOPT_ENCODING=>''
));
curl_exec($ch);
curl_close($ch);
list($width, $height) = getimagesize($file);
fclose($tmp); // << explicitly deletes the file, freeing up disk space etc~ - though php would do this automatically at the end of script execution anyway.

编辑:正如@marekful 所指出的,最初提出的解决方法代码会给出错误的结果。更新后的代码应该会给出正确的结果。

编辑:修复了一些破译的拼写错误(在变量名中)