wppaste
WordPress

wp_image_src_get_dimensions( string $image_src, array $image_meta, int $attachment_id = 0 ): array|false

Since
5.5.0
Source
wp-includes/media.php:1701
Determines an image's width and height dimensions based on the source file.

Compatibility

WordPress
since 5.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

$image_srcstring
The image source file.
$image_metaarray
The image meta data as returned by 'wp_get_attachment_metadata()'.
$attachment_idintoptional
The image attachment ID. Default 0.Default: 0

Return value

array|false
Array with first element being the width and second element being the height, or false if dimensions cannot be determined.

Performance profile

How much work a call to wp_image_src_get_dimensions() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
15–49

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

Plugin surface
1 hook

Third-party callbacks on 'wp_image_src_get_dimensions' run inside this call, and their cost is not bounded by anything here.

Called by
2

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

What it touches

  • hookthird-party callbacksapply_filters()called directly

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_image_src_get_dimensions() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!isset($image_meta)15–17apply_filters()
always22–35wp_basename(), apply_filters()
isset($image_meta) && !empty($image_meta)31–49wp_basename(), wp_basename(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev4915–497
8.54915–497
8.44915–4973 fewer instructions than PHP 8.3
8.35215–527
8.25215–527
8.15215–527
7.45215–527

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.

Hooks and filters fired · 1

One hook fires while wp_image_src_get_dimensions() runs, in this order:

  1. apply_filters( wp_image_src_get_dimensions )filterline 1743 (+42 into the body)

    Filters the 'wp_image_src_get_dimensions' value.

Uses · 3

Used by · 2

Source code

function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = 0 ) {	$dimensions = false; 	// Is it a full size image?	if (		isset( $image_meta['file'] ) &&		str_contains( $image_src, wp_basename( $image_meta['file'] ) )	) {		$dimensions = array(			(int) $image_meta['width'],			(int) $image_meta['height'],		);	} 	if ( ! $dimensions && ! empty( $image_meta['sizes'] ) ) {		$src_filename = wp_basename( $image_src ); 		foreach ( $image_meta['sizes'] as $image_size_data ) {			if ( $src_filename === $image_size_data['file'] ) {				$dimensions = array(					(int) $image_size_data['width'],					(int) $image_size_data['height'],				); 				break;			}		}	} 	/**	 * Filters the 'wp_image_src_get_dimensions' value.	 *	 * @since 5.7.0	 *	 * @param array|false $dimensions    Array with first element being the width	 *                                   and second element being the height, or	 *                                   false if dimensions could not be determined.	 * @param string      $image_src     The image source file.	 * @param array       $image_meta    The image meta data as returned by	 *                                   'wp_get_attachment_metadata()'.	 * @param int         $attachment_id The image attachment ID. Default 0.	 */	return apply_filters( 'wp_image_src_get_dimensions', $dimensions, $image_src, $image_meta, $attachment_id );}

Changelog

Introduced in 5.5.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.

About this page

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