PHP - Get information about image (Height and Width) without loading it

height, image, php, width

Solution

`getImageSize()` is the proper way to get this information in PHP

It does a minimal amount of work based on the type of image. For example, a GIF image's height/width are stored in a header. Very easy to access and read. So this is how the function most likely gets that information from the file. For a JPEG, it has to do a little more work, using the SOFn markers.

The fastest way to access this information would be to maintain a database of file dimensions every time a new one is uploaded.

Given your current situation. I recommend writing a PHP script that takes all of your current image files, gets the size with this function, and then inserts the info into a database for future use.

Problem

Is it possible to get image information without loading the actual image with PHP? In my case I want the Height and Width. I have this code to fetch images from a directory. I echo out the image's url and fetch it with JS. ``` <?php $directory = "./images/photos/"; $sub_dirs = glob($directory . "*"); $i = 0; $len = count($sub_dirs); foreach($sub_dirs as $sub_dir) { $images = glob($sub_dir . '/*.jpg'); $j = 0; $len_b = count($images); foreach ($images as $image) { if ($j == $len_b - 1) { echo $image; } else { echo $image . "|"; } $j++; } if ($i == $len - 1) { } else { echo "|"; } $i++; } ?> ```

Original source