In this tutorial, I will be showing you how to get the width and height of an image using PHP. Thankfully, PHP has a handy built-in function called getimagesize, which will do most of the tough work for us.
Let’s look at a simple example:
//The path and filename of our image. $imagePath = 'image.png'; //Use getimagesize to get size info about the image. $sizeInfo = getimagesize($imagePath); //Check to see if it was successful. if($sizeInfo === false){ //Throw an exception if the function failed. throw new Exception('Could not get image size of: ' . $imagePath); } //var_dump the array that getimagesize returned. var_dump($sizeInfo);
In the code snippet above:
- We specified the path to our image file. In this case, the code assumes that the file is in the same directory as our PHP script. If it is in a different location, you will need to specify the full or relative path.
- We retrieved the size of the image using the function getimagesize. This function will return an array or a boolean FALSE value on failure.
- After that, we check to see if the function has failed or not. If it fails, we throw an Exception.
- If the function was successful, we var_dump the array that was returned so that we can get a better look at its structure.
Getting the dimensions.
In my case, the array looked like this:
As you can see, there are six elements in the array:
- 0: This is the width of the image in pixels.
- 1: This is the height of the image in pixels.
- 2: An integer value that tells us what type of image it is. The integer in question corresponds to one of the IMAGETYPE_* constants. In the case above, the type of image corresponds to IMAGETYPE_PNG (3). Similarly, if it were a JPEG, this element would contain 2, which corresponds to IMAGETYPE_JPEG.
- 3: This is a HTML string that contains the inline width and height of the image. To be honest, this isn’t really that useful as we can build the string ourselves using the dimensions in the first two elements.
- bits: The number of bits for each color.
- mime: The mime type of the image.
Taking the above information into account, you can print out the dimensions of the image like so:
//Printing out width X height. echo $sizeInfo[0] . ' X ' . $sizeInfo[1];
In my case, PHP printed out “272 X 92” onto the page.
WebP files.
Support for Google’s WebP format was not added until PHP version 7.1. As a result, the getimagesize function will return a boolean FALSE value if you are using an earlier version than that.
ico files.
Furthermore, .ico files may also fail as PHP only added support for icon files version 5.3.
Can getimagesize be used to validate image files?
This is not recommended, as the getimagesize function does not attempt to validate the format of the image. In the past, I have come across examples that showed how a GIF file containing PHP code was accepted by the function.
Failed to open stream.
If you receive a “failed to open stream” warning, then it means that geimagesize was unable to locate the file that you gave it. To fix this, you will need to make sure that the path you specified is correct.