Viewed   84 times
<?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.

 Answers

3

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

Tuesday, November 22, 2022
 
nispio
 
2

Execute this:

print_r(get_defined_constants());

And then look for constants prefixed with IMAGETYPE_. On my PHP 5.3 installation I got these values:

[IMAGETYPE_GIF] => 1
[IMAGETYPE_JPEG] => 2
[IMAGETYPE_PNG] => 3
[IMAGETYPE_SWF] => 4
[IMAGETYPE_PSD] => 5
[IMAGETYPE_BMP] => 6
[IMAGETYPE_TIFF_II] => 7
[IMAGETYPE_TIFF_MM] => 8
[IMAGETYPE_JPC] => 9
[IMAGETYPE_JP2] => 10
[IMAGETYPE_JPX] => 11
[IMAGETYPE_JB2] => 12
[IMAGETYPE_SWC] => 13
[IMAGETYPE_IFF] => 14
[IMAGETYPE_WBMP] => 15
[IMAGETYPE_JPEG2000] => 9
[IMAGETYPE_XBM] => 16
[IMAGETYPE_ICO] => 17
[IMAGETYPE_UNKNOWN] => 0
[IMAGETYPE_COUNT] => 18

As you can see Flash SWF are considered images, and actually getimagesize() is able to read the width and height of a SWF object. To me it seemed like a curiosity when I first discovered it, that's why mentioned it here.

Saturday, October 1, 2022
 
akuta
 
5

If file_get_contents works then definitely fopen would work

Curl would have been the best option since you are having permission issue but you can also use FastImage .. to read the image headers and get the information instead of having to save the whole file locally

Example

$img = new FastImage("http://gan.doubleclick.net/gan_impression?lid=41000000015155731&pubid=21000000000506299&lsrc=17");
var_dump($img->getSize(),$img->getType());

Output

array (size=2)
  0 => int 120
  1 => int 90
string 'jpeg' (length=4)

Simple Demo

Thursday, November 24, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :