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:543
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$1stringThe 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
- Scaling
- Constant
- Instructions
- 8–124
- Plugin surface
- 2 hooks
- Called by
- 3
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 156.
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.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8–14 | none |
!$orig_w && !$orig_h | 24–34 | apply_filters() |
!$orig_w && !$orig_h && $output === null && wp_fuzzy_number_match() | 60–80 | apply_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–67 | apply_filters(), wp_constrain_dimensions(), wp_fuzzy_number_match() |
!$orig_w && !$orig_h && $output === null | 66–72 | apply_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–105 | apply_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–92 | apply_filters(), round(), round(), wp_fuzzy_number_match() |
!$orig_w && !$orig_h && $output === null && $dest_w && $dest_h && wp_fuzzy_number_match() | 83–109 | apply_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–96 | apply_filters(), round(), round(), floor(), wp_fuzzy_number_match() |
!$orig_w && !$orig_h && $output === null && $dest_w && $dest_h | 84–97 | apply_filters(), round(), round(), wp_fuzzy_number_match(), wp_fuzzy_number_match() |
!$orig_w && !$orig_h && $output === null && wp_fuzzy_number_match() | 84–111 | apply_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–98 | apply_filters(), round(), round(), round(), wp_fuzzy_number_match() |
20 further outcomes, up to 116 instructions
!$orig_w && !$orig_h && $output === null && $dest_w && $dest_h | 89–101 | apply_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–112 | apply_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–115 | apply_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–99 | apply_filters(), round(), round(), floor(), floor(), wp_fuzzy_number_match() |
!$orig_w && !$orig_h && $output === null && !wp_fuzzy_number_match() | 90–102 | apply_filters(), round(), round(), round(), floor(), wp_fuzzy_number_match() |
!$orig_w && !$orig_h && $output === null | 90–103 | apply_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–117 | apply_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–104 | apply_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–104 | apply_filters(), round(), round(), floor(), floor(), wp_fuzzy_number_match(), wp_fuzzy_number_match() |
!$orig_w && !$orig_h && $output === null | 95–107 | apply_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–118 | apply_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–121 | apply_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–105 | apply_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–108 | apply_filters(), round(), round(), round(), round(), floor(), wp_fuzzy_number_match() |
!$orig_w && !$orig_h && !$dest_w && $output === null && !$dest_h | 96–109 | apply_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–110 | apply_filters(), round(), round(), round(), floor(), floor(), wp_fuzzy_number_match(), wp_fuzzy_number_match() |
!$orig_w && !$orig_h && !$dest_w && $output === null && !$dest_h | 101–113 | apply_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–124 | apply_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–111 | apply_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–116 | apply_filters(), round(), round(), round(), round(), floor(), floor(), wp_fuzzy_number_match(), wp_fuzzy_number_match() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 156 | 8–124 | 23 | |
| 8.5 | 156 | 8–124 | 23 | |
| 8.4 | 156 | 8–124 | 23 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 168 | 8–136 | 23 | |
| 8.2 | 168 | 8–136 | 23 | 4 fewer instructions than PHP 8.1 |
| 8.1 | 172 | 8–138 | 23 | |
| 7.4 | 172 | 8–138 | 23 |
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:
- apply_filters( image_resize_dimensions )filterline 569 (+26 into the body)
Filters whether to preempt calculating the image resize dimensions.
- apply_filters( wp_image_resize_identical_dimensions )filterline 661 (+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
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_constrain_dimensions()Calculates the new dimensions for a down-sampled image.
- wp_fuzzy_number_match()Checks if two numbers are nearly the same.
Used by · 3
- WP_Image_Editor_GD::_resize()
- WP_Image_Editor_Imagick::resize()Resizes current image.
- 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.
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.
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.