getimagesize()
getimagesize() — Get the size of an image.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
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.
Signature
getimagesize(string $filename, array &$image_info = null): array|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $filename | string | This parameter specifies the file you wish to retrieve information about. It can reference a local file or (configuration permitting) a remote file using one of the supported streams. |
| &$image_info | array | This optional parameter allows you to extract some extended information from the image file. Currently, this will return the different JPG APP markers as an associative array. Some programs use these APP markers to embed text information in images. A very common one is to embed IPTC information in the APP13 marker. You can use the iptcparse() function to parse the binary APP13 marker into something readable. |
Return value
Returns an array with up to 7 elements. Not all image types will include the channels and bits elements. Index 0 and 1 contains respectively the width and the height of the image. Note: Some formats may contain no image or may contain multiple images. In these cases, getimagesize() might not be able to properly determine the image size. getimagesize() will return zero for width and height in these cases. Some formats may contain no image or may contain multiple images. In these cases, getimagesize() might not be able to properly determine the image size. getimagesize() will return zero for width and height in these cases. Note: getimagesize() is agnostic of any image metadata. If e.g. the ...
Example
A runnable snippet:
list($width, $height, $type, $attr) = getimagesize("img/flag.jpg"); echo "<img src=\"img/flag.jpg\" $attr alt=\"getimagesize() example\" />";
Changelog
| Version | Change |
|---|---|
| 8.2.0 | Now returns the actual image dimensions, bits and channels of AVIF images; previously, the dimensions were reported as 0x0, and bits and channels were not reported at all. |
| 8.0.0 | A ValueError is now thrown if filename is empty; previously an E_WARNING was raised and the function returned false. |
| 7.1.0 | Added WebP support. |
See also
getimagesizefromstring(), image_type_to_mime_type()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page getimagesize() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.