Friday, December 17, 2010

Get Width and Height of an Image in PHP

PHP provides a function to get the width and height of an image. "getimagesize()" This function will determine the size of image file and also will get size of flash file(swf).


Syntax


list($width, $height, $type, $attr) = getimagesize("image_name.jpg");


|---|--------------------- code --------------------|---|


This is a sample code, change image_name to your image name and test it!

{code type=codetype}
<?php

list($width, $height, $type, $attr) = getimagesize("image_name.jpg");


echo "Image width " .$width;
echo "<BR>";
echo "Image height " .$height;
echo "<BR>";
echo "Image type " .$type;
echo "<BR>";
echo "Attribute " .$attr;

?>
{/code}


When you run this script you will see the result like this

Image width 640
Image height 480
Image type 2
Image attribute width="640" height="480"

You will get the width, height, type of an image and also attribute of an image, I use this function in my image upload form.
Type of an image you can see from table below








































Type of the image

1 = GIF 9 = JPC
2 = JPG 10 = JP2
3 = PNG 11 = JPX
4 = SWF 12 = JB2
5 = PSD 13 = SWC
6 = BMP 14 = IFF
7 = TIFF(intel byte order) 15 = WBMP
8 = TIFF(motorola byte order) 16 = XBM

2 comments: