wp_getimagesize()
Gets the image dimensions and other related information.
It is a wrapper for the php function getimagesize(), so for all the nuances of using the function, read there.
The wrapper does the following:
-
Allows you to "catch" errors of the getimagesize() function in debug mode, except for scenarios when the code is run in tests, see the constant WP_RUN_CORE_TESTS.
-
In normal mode, suppresses errors of the getimagesize() function, such as "corrupt JPEG data: 7191 extraneous bytes before marker," even if it is able to provide image size information, and others. Learn more about this in ticket #42480.
-
For PHP versions that do not support WebP images, extracts image size information from file headers.
PHP Version Description 8.2.0 Now returns actual dimensions, bits, and channels of AVIF images; previously dimensions were reported as 0x0, and bits and channels were not reported at all. 7.1.0 Added support for WebP.
No Hooks.
Returns
Array|false. Information about the image or false on failure.
Example return value:
Array ( [0] => 250 [1] => 250 [2] => 3 [3] => width="250" height="250" [bits] => 8 // May not be present [channels] => 3 // May not be present [mime] => image/png )
[0]- Width in pixels.[1]- Height in pixels.[2]- Contains one of the image type constants IMAGETYPE_XXX, for example, IMAGETYPE_PNG, IMAGETYPE_JPC. This value is usually used in the functions image_type_to_mime_type() and image_type_to_extension().[3]- Contains a string with the width and height values of the image height="yyy" width="xxx", which can be used inside the IMG tag.[bits]- Takes the value 3 for RGB images and 4 for CMYK.[channels]- Color depth, number of bits for each color.[mime]- Corresponding MIME type of the image. This information is used for proper processing of the image based on the Content-type header.
For some types of images, the presence of channels and bits values can be confusing. For example, GIF always uses 3 channels per pixel, but color depth cannot be computed for animated GIF images with a global color table.
Some file formats may store multiple images or may not contain an image at all. In such cases, wp_getimagesize() will not be able to determine the image size. getimagesize() will return zeros as the height and width values.
Usage
wp_getimagesize( $filename, $image_info );
- $filename(string) (required)
The file from which information will be extracted. This can be a local full path to the file or a remote file.
Data from a remote file must be passed using one of the supported streams.
- $image_info(array)
This optional argument allows you to extract some extended information from the image file.
Currently, various
JPG,APPmarkers can be obtained as an associative array.Some programs use these markers to embed text into the image. Most commonly, » IPTC information is embedded in the APP13 marker. To convert the binary data of the APP13 marker into something human-readable, you can use the function iptcparse().
image_info only supports JFIF files.
Default: null
Examples
#1 What does the function output
$attach_id = 16837; $path = get_attached_file( $attach_id ); $data = wp_getimagesize( $path ); print_r( $data );
We will get:
Array ( [0] => 757 [1] => 234 [2] => 3 [3] => width="757" height="234" [bits] => 8 [mime] => image/png )
#2 How to get data by URL of an image uploaded to the WordPress media library
$src = 'https://example.com/wp-content/uploads/2024/02/clipboard-image-516943.png'; $upload_dir = wp_upload_dir(); $image_path = str_replace( wp_make_link_relative( $upload_dir['baseurl'] ), $upload_dir['basedir'], wp_make_link_relative( $src ) ); $image_data = wp_getimagesize( $image_path ); print_r( $image_data ); /* Array ( [0] => 754 [1] => 243 [2] => 3 [3] => width="754" height="243" [bits] => 8 [mime] => image/png ) */
You can also get data directly by URL, but in this case, an HTTP request will be made, and such code will work significantly slower. Below is just an illustrative example (this is not recommended!):
$image_data = wp_getimagesize( 'https://example.com/uploads/image.png' ); print_r( $image_data ); /* Array ( [0] => 754 [1] => 243 [2] => 3 [3] => width="754" height="243" [bits] => 8 [mime] => image/png ) */
Changelog
| Since 5.7.0 | Introduced. |
| Since 5.8.0 | Added support for WebP images. |
| Since 6.5.0 | Added support for AVIF images. |