【问题标题】:PHP getimagesize empty outputPHP getimagesize 空输出
【发布时间】:2014-08-10 16:56:09
【问题描述】:
<?php
$URL="http://cor-forum.de/forum/images/smilies/zombie.png";
list($width, $height) = getimagesize($URL);

echo 'width: '.$width.'<br>
height: '.$height;
?>

这会产生以下输出:

width:
height:

编辑,我收到以下警告:

警告: 获取图像大小(http://cor-forum.de/forum/images/smilies/zombie.png): 无法打开流:HTTP 请求失败! HTTP/1.1 403 禁止在 /html/test.php 上 第 6 行

--而如果我使用另一张图片,它会显示正确的值

$URL='http://getfavicon.appspot.com/http://google.com?defaulticon=1pxgif';

编辑: 我想启用在论坛中包含外部图像,但我想先检查它们的大小。 那么,我该怎么做才能获取图像的大小,其服务器正在“阻止我”?

编辑: allow_url_fopen 设置为 ON,是的。

【问题讨论】:

  • allow_url_fopen 设置吗?
  • 开启正确的错误报告。
  • 又试了一次,还是不同的输出。不过,我现在能够收到警告。编辑了开始的帖子。这意味着什么?问候
  • @Blauhirn 好像服务器阻止了你。
  • 看起来该域正试图禁止热链接到图像(这可能会严重影响带宽)。为什么不使用自己的图像?

标签: php getimagesize


【解决方案1】:

伪造 HTTP 引用字段似乎适用于这个:

<?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的来源

【讨论】:

    【解决方案2】:

    看起来您提到的网址有问题,您可以试试下面的代码 我没有做任何事情只是改变了网址,

    URL = "http://forums.phpfreaks.com/uploads/profile/photo-thumb-68615.jpg";
    list($width, $height) = getimagesize($URL);
    echo 'width: ' . $width . '<br>height: ' . $height;
    

    【讨论】:

    • 是的,有效,问题肯定出在 URL 上
    • 我浏览了那个网站并尝试了所有的 url 没有任何工作实际上它被禁止通过其他方式访问。
    【解决方案3】:

    将 PHP 内存限制设置为最大 256 Mb 以修复它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 2011-01-26
      • 2012-09-26
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      相关资源
      最近更新 更多