Get the size of an image using a PHP function
PHP provides a function to get the width and height of an image.
getimagesize()
Get the size of an image.
Description
The getimagesize() function will determine the size of any supported given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag and the correspondent HTTP content type.
getimagesize() can also return some more information in imageinfo parameter.
Example:
This is a sample code, change img_name to your image name and see result!
1 2 3 4 |
<?php list($width, $height, $type, $attr) = getimagesize("img/flag.jpg"); echo "<img src=\"img/flag.jpg\" $attr alt=\"getimagesize() example\" />"; ?> |
1 2 3 4 5 6 7 |
<?php $size = getimagesize("https://webtaxonomy.com/wp-content/uploads/2017/05/webtaxonomy-logo.png"); // if the file name has space in it, encode it properly $size = getimagesize("https://webtaxonomy.com/wp-content/uploads/2017/05/webtaxonomy-lo%20go.png"); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?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; ?> |
When you run this script the result you will see like this
Image width 379
Image height 344
Image type 2
Image attribute width=”379″ height=”344″
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 Image
1 = GIF | 5 = PSD | 9 = JPC | 13 = SWC |
2 = JPG | 6 = BMP | 10 = JP2 | 14 = IFF |
3 = PNG | 7 = TIFF(intel byte order) | 11 = JPX | 15 = WBMP |
4 = SWF | 8 = TIFF(motorola byte order) | 12 = JB2 | 16 = XBM |