18.3 Getting the Size of an Image
TechniqueUse the GetImageSize() function, which returns an array of information about an image, including prebuilt width and height tags: <?php $image_info = GetImageSize("someimage.gif"); list($image_width, $image_height, $image_type, $image_width_height_string) = $image_info; print "<img src='someimage.gif' $image_width_height_string>"; ?> CommentsThe GetImageSize() function is part of the standard PHP library, meaning that you do not need GD support enabled to use this function. GetImageSize() returns a numerically indexed array with the first element equal to the width of the image in pixels, and the second element equal to the image's height. The third element contains the type of the image ( 1 is GIF, 2 is JPG, 3 is PNG), and the fourth and final element is the width and height string, which you can embed in image tags. If you pass it a JPEG, the GetImageSize() function also returns two elements in associative array form. One element is the number of bits in the image and the other is the number of channels. You can access the items of the associative array like so: <?php $image_info = GetImageSize("someimage.jpg"); print "someimage.jpg has {$image_info['channels']} channels "; print "as well as {$image_info['bits']} bits\n"; ?> |