Reputation: 16736
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.
Upvotes: 7
Views: 2613
Reputation: 140
the fastest way I know for getting images size
https://github.com/tommoor/fastimage
Upvotes: 0
Reputation: 3251
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.
Upvotes: 8
Reputation: 3960
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
Upvotes: 0
Reputation: 2387
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()
.
Upvotes: 2