Get image width and height in PHP

Get image width and height in PHP

In this quick tutorial, i am going to show you a simple way to get image width and height in PHP using a function getimagesize().

This is a very useful function in PHP, it provides function to get the width and height of an image. The getimagesize() function will determine the size of image file including flash file(swf).

Syntax

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

All you need is the URL of an image or location to any image, then you can find the dimensions of an image like this:

Code

<?php

list($width, $height, $type, $attr) = getimagesize("url/to/image.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 you will see the result like this

Result

Image width: 379
Image height 344
Image type: 2
Image Attribute: width=”379″ height=”344″

For Type of image

1 = GIF5 = PSD9 = JPC13 = SWC
2 = JPG6 = BMP10 = JP214 = IFF
3 = PNG7 = TIFF(intel byte order)11 = JPX15 = WBMP
4 = SWF8 = TIFF(motorola byte order) 12 = JB216 = XBM

Functions Used

Reference URL