18.3 Getting the Size of an Image


18.3 Getting the Size of an Image

You want to get the size of an image. For example, you might want to generate width and height tags for images automatically.

Technique

Use 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>"; ?> 

Comments

The 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"; ?> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net