PHP getimagesize 空输出

标签 php getimagesize

<?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/

相关文章:

PHP文件上传和覆盖同名文件

php - 网络服务器上的本地数据

php - 比较 2 个数组 Php 中的 2 个数组值

当图像存储为字节数组时PHP获取图像大小

PHP getimagesize() 抛出 "SSL: The specified procedure could not be found"

php - SQL 的日期函数未按预期工作

Android:API 中的 bitmap.getByteCount() 小于 12

php - 如何在 PHP 中修复 "getimagesize(): Read error!"

PHP-getimagesize 函数

PHP - While 和包装 div