Viewed   169 times

I am trying to get image size (image dimensions, width and height) of hundreds of remote images and getimagesize is way too slow.

I have done some reading and found out the quickest way would be to use file_get_contents to read a certain amount of bytes from the images and examining the size within the binary data.

Anyone attempted this before? How would I examine different formats? Anyone has seen any library for this?


function ranger($url){
    $headers = array(
    "Range: bytes=0-32768"

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($curl);
    return $data;

$start = microtime(true);

$url = "";

$raw = ranger($url);
$im = imagecreatefromstring($raw);

$width = imagesx($im);
$height = imagesy($im);

$stop = round(microtime(true) - $start, 5);

echo $width." x ".$height." ({$stop}s)";


640 x 480 (0.20859s)

Loading 32kb of data worked for me.

Friday, September 2, 2022

I suggest you follow this approach:

// if you need the image type
$type = exif_imagetype($url);

// if you need the image mime type
$type = image_type_to_mime_type(exif_imagetype($url));

// if you need the image extension associated with the mime type
$type = image_type_to_extension(exif_imagetype($url));

// if you don't care about the image type ignore all the above code
$image = ImageCreateFromString(file_get_contents($url));

echo ImageSX($image); // width
echo ImageSY($image); // height

Using exif_imagetype() is a lot faster than getimagesize(), the same goes for ImageSX() / ImageSY(), plus they don't return arrays and can also return the correct image dimension after the image has been resized or cropped for instance.

Also, using getimagesize() on URLs isn't good because it'll consume much more bandwidth than the alternative exif_imagetype(), from the PHP Manual:

When a correct signature is found, the appropriate constant value will be returned otherwise the return value is FALSE. The return value is the same value that getimagesize() returns in index 2 but exif_imagetype() is much faster.

That's because exif_imagetype() will only read the first few bytes of data.

Monday, November 28, 2022

There are very rare cases where the error suppression operator is not evil, this is one of them.

            $mediapath = '';
        return $mediapath;

Note the @ prefixing the statement. It will cause any error, warning or notice be suppressed. Be advised that if a fatal error occurs, the script will terminate without giving indication!

Saturday, August 13, 2022

You can't use filesize() on a string, also the Content-Disposition: attachment header forces a download.

Change everything under the base64 to:

header( 'Pragma: public' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Cache-Control: private',false );
header( 'Content-Transfer-Encoding: binary' );
header('Content-Type: image/png');      // Becasue Base64 Image Is Of PNG Type Fixed
echo $imageFile

I'm not even sure you need most of those headers, when I tested it I just used Content-Type.

Saturday, October 15, 2022

Some code would help to understand what you are trying to do. You could try to send the image back to the client in base64 encoding. See How to convert an image to base64 encoding? for an example

Wednesday, August 3, 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 :