wppaste
WordPress

image_resize_dimensions( int $orig_w, int $orig_h, int $dest_w, int $dest_h, bool|array $crop = false ): array|false

Since
2.5.0
Source
wp-includes/media.php:555
Retrieves calculated resize dimensions for use in WP_Image_Editor.

Description

Calculates dimensions and coordinates for a resized image that fits within a specified width and height.

Compatibility

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

$orig_wint
Original width in pixels.
$orig_hint
Original height in pixels.
$dest_wint
New width in pixels.
$dest_hint
New height in pixels.
$cropbool|arrayoptional
Image cropping behavior. If false, the image will be scaled (default). If true, image will be cropped to the specified dimensions using center positions. If an array, the image will be cropped using the array to specify the crop location: @type string $0 The x crop position. Accepts 'left', 'center', or 'right'.Default: false
  • $1string

    The y crop position. Accepts 'top', 'center', or 'bottom'.

Return value

array|false
Returned array matches parameters for imagecopyresampled(). False on failure.

Performance profile

How much work a call to image_resize_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
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
8–124

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

Plugin surface
2 hooks

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

Called by
3

3 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 · 32 distinct outcomes

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

WhenInstructionsCalls it makes
always8–14none
!$orig_w && !$orig_h24–34apply_filters()
!$orig_w && !$orig_h && $output === null && wp_fuzzy_number_match()60–80apply_filters(), wp_constrain_dimensions(), wp_fuzzy_number_match(), wp_fuzzy_number_match(), apply_filters()
!$orig_w && !$orig_h && $output === null && !wp_fuzzy_number_match()61–67apply_filters(), wp_constrain_dimensions(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && $output === null66–72apply_filters(), wp_constrain_dimensions(), wp_fuzzy_number_match(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && $output === null && $dest_w && $dest_h && wp_fuzzy_number_match()78–105apply_filters(), round(), round(), wp_fuzzy_number_match(), wp_fuzzy_number_match(), apply_filters()
!$orig_w && !$orig_h && $output === null && $dest_w && $dest_h && !wp_fuzzy_number_match()79–92apply_filters(), round(), round(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && $output === null && $dest_w && $dest_h && wp_fuzzy_number_match()83–109apply_filters(), round(), round(), floor(), wp_fuzzy_number_match(), wp_fuzzy_number_match(), apply_filters()
!$orig_w && !$orig_h && $output === null && $dest_w && $dest_h && !wp_fuzzy_number_match()84–96apply_filters(), round(), round(), floor(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && $output === null && $dest_w && $dest_h84–97apply_filters(), round(), round(), wp_fuzzy_number_match(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && $output === null && wp_fuzzy_number_match()84–111apply_filters(), round(), round(), round(), wp_fuzzy_number_match(), wp_fuzzy_number_match(), apply_filters()
!$orig_w && !$orig_h && $output === null && !wp_fuzzy_number_match()85–98apply_filters(), round(), round(), round(), wp_fuzzy_number_match()
20 further outcomes, up to 116 instructions
!$orig_w && !$orig_h && $output === null && $dest_w && $dest_h89–101apply_filters(), round(), round(), floor(), wp_fuzzy_number_match(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && $output === null && $dest_w && $dest_h && $x !== "left" && $x !== "right" && $y !== "top" && $y !== "bottom" && wp_fuzzy_number_match()89–112apply_filters(), round(), round(), floor(), floor(), wp_fuzzy_number_match(), wp_fuzzy_number_match(), apply_filters()
!$orig_w && !$orig_h && $output === null && wp_fuzzy_number_match()89–115apply_filters(), round(), round(), round(), floor(), wp_fuzzy_number_match(), wp_fuzzy_number_match(), apply_filters()
!$orig_w && !$orig_h && $output === null && $dest_w && $dest_h && $x !== "left" && $x !== "right" && $y !== "top" && $y !== "bottom" && !wp_fuzzy_number_match()90–99apply_filters(), round(), round(), floor(), floor(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && $output === null && !wp_fuzzy_number_match()90–102apply_filters(), round(), round(), round(), floor(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && $output === null90–103apply_filters(), round(), round(), round(), wp_fuzzy_number_match(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && !$dest_w && $output === null && !$dest_h && wp_fuzzy_number_match()90–117apply_filters(), round(), round(), round(), round(), wp_fuzzy_number_match(), wp_fuzzy_number_match(), apply_filters()
!$orig_w && !$orig_h && !$dest_w && $output === null && !$dest_h && !wp_fuzzy_number_match()91–104apply_filters(), round(), round(), round(), round(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && $output === null && $dest_w && $dest_h && $x !== "left" && $x !== "right" && $y !== "top" && $y !== "bottom"95–104apply_filters(), round(), round(), floor(), floor(), wp_fuzzy_number_match(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && $output === null95–107apply_filters(), round(), round(), round(), floor(), wp_fuzzy_number_match(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && $output === null && $x !== "left" && $x !== "right" && $y !== "top" && $y !== "bottom" && wp_fuzzy_number_match()95–118apply_filters(), round(), round(), round(), floor(), floor(), wp_fuzzy_number_match(), wp_fuzzy_number_match(), apply_filters()
!$orig_w && !$orig_h && !$dest_w && $output === null && !$dest_h && wp_fuzzy_number_match()95–121apply_filters(), round(), round(), round(), round(), floor(), wp_fuzzy_number_match(), wp_fuzzy_number_match(), apply_filters()
!$orig_w && !$orig_h && $output === null && $x !== "left" && $x !== "right" && $y !== "top" && $y !== "bottom" && !wp_fuzzy_number_match()96–105apply_filters(), round(), round(), round(), floor(), floor(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && !$dest_w && $output === null && !$dest_h && !wp_fuzzy_number_match()96–108apply_filters(), round(), round(), round(), round(), floor(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && !$dest_w && $output === null && !$dest_h96–109apply_filters(), round(), round(), round(), round(), wp_fuzzy_number_match(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && $output === null && $x !== "left" && $x !== "right" && $y !== "top" && $y !== "bottom"101–110apply_filters(), round(), round(), round(), floor(), floor(), wp_fuzzy_number_match(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && !$dest_w && $output === null && !$dest_h101–113apply_filters(), round(), round(), round(), round(), floor(), wp_fuzzy_number_match(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && !$dest_w && $output === null && !$dest_h && $x !== "left" && $x !== "right" && $y !== "top" && $y !== "bottom" && wp_fuzzy_number_match()101–124apply_filters(), round(), round(), round(), round(), floor(), floor(), wp_fuzzy_number_match(), wp_fuzzy_number_match(), apply_filters()
!$orig_w && !$orig_h && !$dest_w && $output === null && !$dest_h && $x !== "left" && $x !== "right" && $y !== "top" && $y !== "bottom" && !wp_fuzzy_number_match()102–111apply_filters(), round(), round(), round(), round(), floor(), floor(), wp_fuzzy_number_match()
!$orig_w && !$orig_h && !$dest_w && $output === null && !$dest_h && $x !== "left" && $x !== "right" && $y !== "top" && $y !== "bottom"107–116apply_filters(), round(), round(), round(), round(), floor(), floor(), wp_fuzzy_number_match(), wp_fuzzy_number_match()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1568–12423
8.51568–12423
8.41568–1242312 fewer instructions than PHP 8.3
8.31688–13623
8.21688–136234 fewer instructions than PHP 8.1
8.11728–13823
7.41728–13823

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 · 2

2 hooks fire while image_resize_dimensions() runs, in this order:

  1. apply_filters( image_resize_dimensions )filterline 581 (+26 into the body)

    Filters whether to preempt calculating the image resize dimensions.

  2. apply_filters( wp_image_resize_identical_dimensions )filterline 673 (+118 into the body)

    Filters whether to proceed with making an image sub-size with identical dimensions with the original/source image. Differences of 1px may be due to rounding and are ignored.

Uses · 3

Used by · 3

Source code

function image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop = false ) { 	if ( $orig_w <= 0 || $orig_h <= 0 ) {		return false;	}	// At least one of $dest_w or $dest_h must be specific.	if ( $dest_w <= 0 && $dest_h <= 0 ) {		return false;	} 	/**	 * Filters whether to preempt calculating the image resize dimensions.	 *	 * Returning a non-null value from the filter will effectively short-circuit	 * image_resize_dimensions(), returning that value instead.	 *	 * @since 3.4.0	 *	 * @param null|mixed $null   Whether to preempt output of the resize dimensions.	 * @param int        $orig_w Original width in pixels.	 * @param int        $orig_h Original height in pixels.	 * @param int        $dest_w New width in pixels.	 * @param int        $dest_h New height in pixels.	 * @param bool|array $crop   Whether to crop image to specified width and height or resize.	 *                           An array can specify positioning of the crop area. Default false.	 */	$output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop ); 	if ( null !== $output ) {		return $output;	} 	// Stop if the destination size is larger than the original image dimensions.	if ( empty( $dest_h ) ) {		if ( $orig_w < $dest_w ) {			return false;		}	} elseif ( empty( $dest_w ) ) {		if ( $orig_h < $dest_h ) {			return false;		}	} else {		if ( $orig_w < $dest_w && $orig_h < $dest_h ) {			return false;		}	} 	if ( $crop ) {		/*		 * Crop the largest possible portion of the original image that we can size to $dest_w x $dest_h.		 * Note that the requested crop dimensions are used as a maximum bounding box for the original image.		 * If the original image's width or height is less than the requested width or height		 * only the greater one will be cropped.		 * For example when the original image is 600x300, and the requested crop dimensions are 400x400,		 * the resulting image will be 400x300.		 */		$aspect_ratio = $orig_w / $orig_h;		$new_w        = min( $dest_w, $orig_w );		$new_h        = min( $dest_h, $orig_h ); 		if ( ! $new_w ) {			$new_w = (int) round( $new_h * $aspect_ratio );		} 		if ( ! $new_h ) {			$new_h = (int) round( $new_w / $aspect_ratio );		} 		$size_ratio = max( $new_w / $orig_w, $new_h / $orig_h ); 		$crop_w = round( $new_w / $size_ratio );		$crop_h = round( $new_h / $size_ratio ); 		if ( ! is_array( $crop ) || count( $crop ) !== 2 ) {			$crop = array( 'center', 'center' );		} 		list( $x, $y ) = $crop; 		if ( 'left' === $x ) {

Changelog

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