views:

70

answers:

4

One way to improve page loading is to specify image dimesions (hieght width). In PHP this can be done with getimagesize(), however I can imagine this would be quite slow to execute if you have alot of images.

What is the best way to dynamically get image dimensions of many images with minimal effect on page loading. We are talking about 50+ images.

+2  A: 

Yes, getimagesize() can be slow especially on large images, and it's also dependant of the server. If you are having a large image collection and you'd like to show the resolution of each image on the page, I'd probably use a database for that. Whenever an image is uploaded or modified getimagesize() is used.

This would also make is possible to create a more versatile image collection, as you can use SQL statements to group pictures however you like. For example "show pictures from today", "show pictures from last week" and so on.

If you collection is small and an image database isn't an option, you should first try how your site performs with a plain getimagesize().

vtorhonen
+4  A: 

I've just tested with 55 pcs of 5+ MB images:

Imagemagick's getImageGeometry took 5.3 seconds (because after each file you have to recreate the imagick object), while getimagesize went thru the images in 0.032 seconds. The latter is more than acceptable.

If not, store the dimensions in the database.

EDIT: Also, if you get the files through TCPIP, that slows down the process considerably. So, if you call it this way:

getimagesize('http://www.blabla.com/pic.jpg');

Instead of

getimagesize('localdir/hereiam/pic.jpg');

you get some network overhead.

Plus, if those pictures consistently have EXIF data (made with a digital camera), then you can use the PHP exif_ functions, like: exif_read_data.

Question: which PHP version you are using? Older 4.x versions had smaller problems regarding getimagesize on certain filesystems.

Jauzsika
A: 

I believe you are not talking about icons and design images, rather you are talking about dynamic images uploaded to the server by users and editors.

If that is the case, I'd resize the uploaded images when they are uploaded and know their size always

marvin
A: 

Probably not the most elegant solution, but this seems to get around to what you're trying to achieve:

http://www.php.net/manual/en/function.getimagesize.php#88793

Martin Bean