wppaste
WordPress

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:5676
Allows PHP's getimagesize() to be debuggable when necessary.

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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
18–88

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 138.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
14

14 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_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.

WhenInstructionsCalls it makes
!empty($info)18–26getimagesize()
empty($info) && $image_mime_type === false22–32getimagesize(), 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–40getimagesize(), 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–48getimagesize(), 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–50getimagesize(), 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–50getimagesize(), 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_info44–54getimagesize(), 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–58getimagesize(), 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–58getimagesize(), 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–60getimagesize(), 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_info48–58getimagesize(), 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–68getimagesize(), 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 88 instructions
empty($info) && $image_mime_type !== false && $image_mime_type === "image/webp" && $image_mime_type === "image/avif"55–66getimagesize(), 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 instanceof60–70getimagesize(), 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 instanceof67–78getimagesize(), 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 instanceof69–80getimagesize(), 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 instanceof76–88getimagesize(), 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

PHPCompiledExecutedBranchesNotes
8.6-dev13818–8818
8.513818–8818
8.413818–88186 more instructions than PHP 8.3
8.313218–8618
8.213218–8618
8.113218–86181 fewer instruction than PHP 7.4
7.413318–8618

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

Used by · 14

Show all 14

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.5.0
Added support for AVIF images.from the docblock
5.8.0
Added support for WebP images.from the docblock
5.7.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 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.