簡體   English   中英

PHP getimagesize 空輸出

[英]PHP getimagesize empty output

<?php
$URL="http://cor-forum.de/forum/images/smilies/zombie.png";
list($width, $height) = getimagesize($URL);

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

這導致以下輸出:

width:
height:

編輯,我收到以下警告:

警告:getimagesize( http://cor-forum.de/forum/images/smilies/zombie.png ):無法打開流:HTTP 請求失敗! 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;
?>

代碼

看起來你提到的 URL 有問題,你可以試試下面的代碼我沒有做任何事情只是改變了 URL,

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

將 PHP 內存限制設置為最大 256 Mb 以修復它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM