<?php
$URL="http://cor-forum.de/forum/images/smilies/zombie.png";
list($width, $height) = getimagesize($URL);
echo 'width: '.$width.'<br>
height: '.$height;
?>
这会产生以下输出:
width:
height:
编辑,我收到以下警告:
Warning: getimagesize(http://cor-forum.de/forum/images/smilies/zombie.png): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /html/test.php on line 6
--而如果我使用另一张图片,它会显示正确的值
$URL='http://getfavicon.appspot.com/http://google.com?defaulticon=1pxgif';
编辑: 我想在论坛中启用外部图像的包含,但我想先检查它们的大小。 那么,如果服务器“阻止我”,我该怎么做才能获得图像的大小?
编辑: allow_url_fopen 设置为 ON,是的。
最佳答案
伪造 HTTP referer 字段似乎对这个有效:
<?php
function getimgsize($url, $referer = '')
{
$headers = array(
'Range: bytes=0-32768'
);
/* Hint: you could extract the referer from the url */
if (!empty($referer)) array_push($headers, 'Referer: '.$referer);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
$image = imagecreatefromstring($data);
$return = array(imagesx($image), imagesy($image));
imagedestroy($image);
return $return;
}
list($width, $heigth) = getimgsize('http://cor-forum.de/forum/images/smilies/zombie.png', 'http://cor-forum.de/forum/');
echo $width.' x '.$heigth;
?>
来源code
关于PHP getimagesize 空输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25230949/