quickest way to dynamically retrieve image dimensions

安稳与你 提交于 2019-12-30 18:32:51

问题


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.


回答1:


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.




回答2:


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().




回答3:


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




回答4:


the fastest way I know for getting images size

https://github.com/tommoor/fastimage



来源:https://stackoverflow.com/questions/3334097/quickest-way-to-dynamically-retrieve-image-dimensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!