Don’t use getimagesize() – PHP

Over the years I have been involved in fixing numerous websites that are performing slow. The PHP execution time on some were insane, into the many minutes.

Nearly every time it has been down to the getimagesize() function.

This function is terribly slow and should only be used if absolutely necessary.

One thing people seem to like doing is, say in a gallery, loop through every single image getting the properties of the image using getimagesize then generating thumbnails, or even worse, just setting the HTML width and height of images.

Setting the width and height of images in HTML means the huge images are sent to the browser, then resized by the browser. It adds lots of load time and bandwidth usage.

If you are doing something like this, and need to get the image size, do it once, when the image is uploaded and create thumbnails. This way you are only delayed once when adding the images, and your visitors are not delayed.

I should also add that I have seen people recommend using alternative methods such as reading in the first 32bytes of the image, then creating an image from that data, then reading that as the size.
In my testing, this was actually slower, and has issues with different file types.
It may work in some situations, but I recommend doing some speed testing to see if it will suit your situation.