Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

quickest way to dynamically retrieve image dimensions

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.

like image 418
David Avatar asked Jul 26 '10 10:07

David


People also ask

How to get the dimensions of image with react?

How to get the dimensions of image with React? To get the dimensions of image with React, we can get it from the load event handler of the image.

How to get the original size of an image using JavaScript?

You can easily get the original size (width and height) of the image using JavaScript. Assume that you have an image which is originally 250px wide and you make it 550px wide by CSS style or HTML “width” property.

How to get the original width of an image using CSS?

Assume that you have an image which is originally 250px wide and you make it 550px wide by CSS style or HTML “width” property. The naturalWidth will return the original width 250 although the display width is 550. Assume that you have an image which is originally 100px high and you make it 250px high by CSS style or HTML “height” property.

How do I make an image 250px high?

naturalHeight Assume that you have an image which is originally 100px high and you make it 250px high by CSS style or HTML “height” property. The naturalHeight will return the original height 100 although the display height is 250.


1 Answers

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.

like image 105
Jauzsika Avatar answered Nov 15 '22 07:11

Jauzsika