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?
I have created a PHP library for exactly this scenario, it works by downloading the absolute minimum of the remote file needed to determine the filesize. This is different for every image and particularly for JPEG depends on how many embedded thumbnails there are in the file.
It is available on GitHub here: https://github.com/tommoor/fastimage
Example usage:
I was looking for a better way to handle this situation, so I used a few different functions found around the internet.
Overall, when it worked, the fastest tended to be the
getjpegsize
function that James Relyea posted on the PHP page forgetimagesize
, beating theranger
function provided by Dejan above. http://php.net/manual/en/function.getimagesize.php#88793ranger
failed when grabbing 32768 bytes on Image #3, so I increase it to 65536 and it worked to grab the size successfully.There are problems, though, as both
ranger
andgetjpegsize
are limited in ways that make it not stable enough to use. Both failed when dealing with a large JPG image around 3MB, butranger
will work after changing the amount of bytes it grabs. Also, these alternates only deal with JPG images, which means that a conditional would need to be used to only use them on JPGs andgetimagesize
on the other image formats.Also, note that the first image was on an older server running an old version of PHP 5.3.2, where as the 4 other images came from a modern server (cloud based cPanel with MultiPHP dialed back to 5.4.45 for compatibility).
It's worth noting that the cloud based server did far better with
getimagesize
which beat outranger
, in fact for all 4 tests on the cloud server,ranger
was the slowest. Those 4 also were pulling the images from the same server as the code was running, though different accounts.This makes me wonder if the PHP core improved in 5.4 or if the Apache version factors in. Also, it might be down to availability from the server and server load. Let's not forget how networks are getting faster and faster each year, so maybe the speed issue is becoming less of a concern.
So, the end result and my answer is that for complete support for all web image formats, and to still achieve super fast image size, it might be best to suck it up and use
getimagesize
and then cache the image sizes (if these images will be checked more than once) in a database table. In that scenario, only the first check will incur a larger cost, but subsequent requests would be minimal and faster than any function that reads the image headers.As with any caching, it only works well if the content doesn't change and there is a way to check if has been a change. So, a possible solution is to check only the headers of a image URL when checking the cache, and if different, dump the cached version and grab it again with
getimagesize
.test...
Loading 32kb of data worked for me.