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

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

This results in the following output:

width:
height:

EDIT and I get the following warning:

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

--whereas it displays the right values if I use another picture like

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

EDIT: I'd like to enable the inclusion of external images in a forum, but I want to check their size first. So, what can I do to get the size of an image, whose server is "blocking me"?

EDIT: allow_url_fopen is set to ON, yes.

phil294
  • 10,038
  • 8
  • 65
  • 98
  • 1
    is `allow_url_fopen` set ? – Marco Aug 10 '14 at 16:59
  • 1
    Turn on proper error reporting. – CBroe Aug 10 '14 at 17:03
  • tried it again, still different outputs. I was able to get a warning now, though. Edited the starting post. What does that mean? regards – phil294 Aug 10 '14 at 17:43
  • @Blauhirn seems like the server is blocking you. – Marco Aug 10 '14 at 17:45
  • Looks like the domain is trying to ban hotlinking to images (which can hit bandwidth pretty hard). Why don't you use your own image? – Jared Farrish Aug 10 '14 at 17:48
  • that doesn't sound good. I'd like to enable the inclusion of external images in a forum, but I want to check their size first. What can I do to get the size of an image, whose server is "blocking me", anyway? (gonna write the same sentence in the starting post) – phil294 Aug 10 '14 at 17:54
  • @Blauhirn It depends on how the server is blocking you... most servers use your http referer as an indicator. – Marco Aug 10 '14 at 17:58
  • Inner URL needs `urlencode()` – Daniel W. Jul 14 '20 at 16:10

3 Answers3

7

Faking the HTTP referer field seems to work on this one:

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

Source of code

Community
  • 1
  • 1
Marco
  • 7,007
  • 2
  • 19
  • 49
-1

Looks like your having problem with mentioned URL can you try the below code I have not done anything just changed the URL,

URL = "http://forums.phpfreaks.com/uploads/profile/photo-thumb-68615.jpg";
list($width, $height) = getimagesize($URL);
echo 'width: ' . $width . '<br>height: ' . $height;
prashant thakre
  • 5,061
  • 3
  • 26
  • 39
-3

set PHP memory limit to maximum 256 Mb to fix it