wp_getimagesize( string $filename, array $image_info = null ): array|false
- Since
- 5.7.0, 5.8.0, 6.5.0
- Source
wp-includes/media.php:5899
Compatibility
- WordPress
- since 6.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$filenamestring- The file path.
$image_infoarrayoptional- Extended image information (passed by reference).Default:
null
Return value
array|false- Array of image information or false on failure.
Performance profile
How much work a call to wp_getimagesize() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 18–87
- Plugin surface
- None
- Called by
- 15
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 137.
Nothing here hands control to plugin code.
15 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()one call below wp_getimagesize()
Further down the call graph this can also reach cache, query, option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 17 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_getimagesize() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($info) | 18–26 | getimagesize() |
empty($info) && $image_mime_type === false | 22–32 | getimagesize(), wp_get_image_mime() |
empty($info) && $image_mime_type !== false && $image_mime_type !== "image/webp" && $image_mime_type !== "image/avif" && !wp_is_heic_image_mime_type() | 30–40 | getimagesize(), wp_get_image_mime(), wp_is_heic_image_mime_type() |
empty($info) && $image_mime_type !== false && $image_mime_type === "image/webp" && !$webp_info && $image_mime_type !== "image/avif" && !wp_is_heic_image_mime_type() | 37–48 | getimagesize(), wp_get_image_mime(), wp_get_webp_info(), wp_is_heic_image_mime_type() |
empty($info) && $image_mime_type !== false && $image_mime_type !== "image/webp" && $image_mime_type !== "image/avif" && wp_is_heic_image_mime_type() | 38–50 | getimagesize(), wp_get_image_mime(), wp_is_heic_image_mime_type(), wp_get_image_editor(), is_wp_error() |
empty($info) && $image_mime_type !== false && $image_mime_type !== "image/webp" && $image_mime_type === "image/avif" && !$webp_info && !wp_is_heic_image_mime_type() | 39–50 | getimagesize(), wp_get_image_mime(), wp_get_avif_info(), wp_is_heic_image_mime_type() |
empty($info) && $image_mime_type !== false && $image_mime_type === "image/webp" && $webp_info | 44–54 | getimagesize(), wp_get_image_mime(), wp_get_webp_info() |
empty($info) && $image_mime_type !== false && $image_mime_type === "image/webp" && !$webp_info && $image_mime_type !== "image/avif" && wp_is_heic_image_mime_type() | 45–58 | getimagesize(), wp_get_image_mime(), wp_get_webp_info(), wp_is_heic_image_mime_type(), wp_get_image_editor(), is_wp_error() |
empty($info) && $image_mime_type !== false && $image_mime_type === "image/webp" && !$webp_info && $image_mime_type === "image/avif" && !wp_is_heic_image_mime_type() | 46–58 | getimagesize(), wp_get_image_mime(), wp_get_webp_info(), wp_get_avif_info(), wp_is_heic_image_mime_type() |
empty($info) && $image_mime_type !== false && $image_mime_type !== "image/webp" && $image_mime_type === "image/avif" && !$webp_info && wp_is_heic_image_mime_type() | 47–60 | getimagesize(), wp_get_image_mime(), wp_get_avif_info(), wp_is_heic_image_mime_type(), wp_get_image_editor(), is_wp_error() |
empty($info) && $image_mime_type !== false && $image_mime_type !== "image/webp" && $image_mime_type === "image/avif" && $webp_info | 48–58 | getimagesize(), wp_get_image_mime(), wp_get_avif_info() |
empty($info) && $image_mime_type !== false && $image_mime_type === "image/webp" && !$webp_info && $image_mime_type === "image/avif" && wp_is_heic_image_mime_type() | 54–68 | getimagesize(), wp_get_image_mime(), wp_get_webp_info(), wp_get_avif_info(), wp_is_heic_image_mime_type(), wp_get_image_editor(), is_wp_error() |
5 further outcomes, up to 87 instructions
empty($info) && $image_mime_type !== false && $image_mime_type === "image/webp" && $image_mime_type === "image/avif" | 55–66 | getimagesize(), wp_get_image_mime(), wp_get_webp_info(), wp_get_avif_info() |
empty($info) && $image_mime_type !== false && $image_mime_type !== "image/webp" && $image_mime_type !== "image/avif" && wp_is_heic_image_mime_type() && !is_wp_error() && $editor instanceof | 59–69 | getimagesize(), wp_get_image_mime(), wp_is_heic_image_mime_type(), wp_get_image_editor(), is_wp_error(), ->get_size() |
empty($info) && $image_mime_type !== false && $image_mime_type === "image/webp" && !$webp_info && $image_mime_type !== "image/avif" && wp_is_heic_image_mime_type() && !is_wp_error() && $editor instanceof | 66–77 | getimagesize(), wp_get_image_mime(), wp_get_webp_info(), wp_is_heic_image_mime_type(), wp_get_image_editor(), is_wp_error(), ->get_size() |
empty($info) && $image_mime_type !== false && $image_mime_type !== "image/webp" && $image_mime_type === "image/avif" && !$webp_info && wp_is_heic_image_mime_type() && !is_wp_error() && $editor instanceof | 68–79 | getimagesize(), wp_get_image_mime(), wp_get_avif_info(), wp_is_heic_image_mime_type(), wp_get_image_editor(), is_wp_error(), ->get_size() |
empty($info) && $image_mime_type !== false && $image_mime_type === "image/webp" && !$webp_info && $image_mime_type === "image/avif" && wp_is_heic_image_mime_type() && !is_wp_error() && $editor instanceof | 75–87 | getimagesize(), wp_get_image_mime(), wp_get_webp_info(), wp_get_avif_info(), wp_is_heic_image_mime_type(), wp_get_image_editor(), is_wp_error(), ->get_size() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 137 | 18–87 | 18 | |
| 8.5 | 137 | 18–87 | 18 | 1 fewer instruction than PHP 8.4 |
| 8.4 | 138 | 18–88 | 18 | 6 more instructions than PHP 8.3 |
| 8.3 | 132 | 18–86 | 18 | |
| 8.2 | 132 | 18–86 | 18 | |
| 8.1 | 132 | 18–86 | 18 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 133 | 18–86 | 18 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 6
- wp_get_image_mime()Returns the real mime type of an image file.
- wp_get_webp_info()Extracts meta information about a WebP file: width, height, and type.
- wp_get_avif_info()Extracts meta information about an AVIF file: width, height, bit depth, and number of channels.
- wp_is_heic_image_mime_type()Checks if a mime type is for a HEIC/HEIF image.
- wp_get_image_editor()Returns a WP_Image_Editor instance and loads file into it.
- is_wp_error()Checks whether the given variable is a WordPress Error.
Used by · 15
- Custom_Image_Header::create_attachment_object()Creates an attachment 'object'.
- Custom_Image_Header::step_2()Displays second step of custom header image page.
- WP_Image_Editor_GD::load()Loads image from $this->file into new GD Resource.
- WP_Image_Editor_Imagick::update_size()Sets or updates current image size.
- WP_REST_Attachments_Controller::sideload_item()Side-loads a media file without creating a new attachment.
- WP_Site_Icon::create_attachment_object()Creates an attachment 'object'.
- file_is_displayable_image()Validates that file is suitable for displaying within a web page.
- file_is_valid_image()Validates that file is an image.
- get_attachment_icon()Retrieve HTML content of icon attachment image element.
- image_downsize()Scales an image to fit a particular size (such as 'thumb' or 'medium').
- wp_copy_parent_attachment_properties()Copy parent attachment properties to newly cropped image.
- wp_create_image_subsizes()Creates image sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata.
Show all 15
- wp_get_attachment_image_src()Retrieves an image to represent an attachment.
- wp_get_missing_image_subsizes()Compare the existing image sub-sizes (as saved in the attachment meta) to the currently registered image sub-sizes, and return the difference.
- wp_read_image_metadata()Gets extended image metadata, exif or iptc as available.
Source code
function wp_getimagesize( $filename, ?array &$image_info = null ) { // Don't silence errors when in debug mode, unless running unit tests. if ( defined( 'WP_DEBUG' ) && WP_DEBUG && ! defined( 'WP_RUN_CORE_TESTS' ) ) { if ( 2 === func_num_args() ) { $info = getimagesize( $filename, $image_info ); } else { $info = getimagesize( $filename ); } } else { /* * Silencing notice and warning is intentional. * * getimagesize() has a tendency to generate errors, such as * "corrupt JPEG data: 7191 extraneous bytes before marker", * even when it's able to provide image size information. * * See https://core.trac.wordpress.org/ticket/42480 */ if ( 2 === func_num_args() ) { $info = @getimagesize( $filename, $image_info ); } else { $info = @getimagesize( $filename ); } } if ( ! empty( $info ) && // Some PHP versions return 0x0 sizes from `getimagesize` for unrecognized image formats, including AVIFs. ! ( empty( $info[0] ) && empty( $info[1] ) ) ) { return $info; } $image_mime_type = wp_get_image_mime( $filename ); // Not an image? if ( false === $image_mime_type ) { return false; } /* * For PHP versions that don't support WebP images, * extract the image size info from the file headers. */ if ( 'image/webp' === $image_mime_type ) { $webp_info = wp_get_webp_info( $filename ); $width = $webp_info['width']; $height = $webp_info['height']; // Mimic the native return format. if ( $width && $height ) { return array( $width, $height, IMAGETYPE_WEBP, sprintf( 'width="%d" height="%d"', $width, $height ), 'mime' => 'image/webp', ); } } // For PHP versions that don't support AVIF images, extract the image size info from the file headers. if ( 'image/avif' === $image_mime_type ) { $avif_info = wp_get_avif_info( $filename ); $width = $avif_info['width']; $height = $avif_info['height']; // Mimic the native return format. if ( $width && $height ) { return array( $width, $height, IMAGETYPE_AVIF, sprintf( 'width="%d" height="%d"',Changelog
Introduced in 5.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/media.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.