WP_Image_Editor_Imagick::resize( int|null $max_w, int|null $max_h, bool|array $crop = false ): true|WP_Error
- Since
- 3.5.0
- Source
wp-includes/class-wp-image-editor-imagick.php:554
Description
At minimum, either a height or width must be provided.
If one of the two is set to null, the resize will maintain aspect ratio according to the provided dimension.
Compatibility
- WordPress
- since 3.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
$max_wint|null- Image width.
$max_hint|null- Image height.
$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
true|WP_Error
Performance profile
How much work a call to WP_Image_Editor_Imagick::resize() 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
- 12–51
- Plugin surface
- None
- Called by
- 1
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 70.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below WP_Image_Editor_Imagick::resize()
Further down the call graph this can also reach query, option, cache, 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 · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Image_Editor_Imagick::resize() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$max_w === false && $max_h === false | 12 | none |
| always | 30–34 | image_resize_dimensions(), __() |
| always | 42–46 | image_resize_dimensions(), ->crop() |
is_wp_error() | 43–47 | image_resize_dimensions(), ->thumbnail_image(), is_wp_error() |
!is_wp_error() | 47–51 | image_resize_dimensions(), ->thumbnail_image(), is_wp_error(), ->update_size() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 70 | 12–51 | 5 | |
| 8.5 | 70 | 12–51 | 5 | |
| 8.4 | 70 | 12–51 | 5 | |
| 8.3 | 70 | 12–51 | 5 | |
| 8.2 | 70 | 12–51 | 5 | 8 fewer instructions than PHP 8.1 |
| 8.1 | 78 | 12–59 | 5 | |
| 7.4 | 78 | 12–59 | 5 |
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 · 7
- image_resize_dimensions()Retrieves calculated resize dimensions for use in WP_Image_Editor.
- __()Retrieves the translation of $text.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- WP_Error::__construct()Initializes the error.
- WP_Image_Editor_Imagick::crop()Crops Image.
- WP_Image_Editor_Imagick::thumbnail_image()Efficiently resize the current image
- WP_Image_Editor_Imagick::update_size()Sets or updates current image size.
Used by · 1
- WP_Image_Editor_Imagick::make_subsize()Create an image sub-size and return the image meta data value for it.
Source code
public function resize( $max_w, $max_h, $crop = false ) { if ( ( $this->size['width'] === $max_w ) && ( $this->size['height'] === $max_h ) ) { return true; } $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); if ( ! $dims ) { return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ) ); } list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; if ( $crop ) { return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h ); } // Execute the resize. $thumb_result = $this->thumbnail_image( $dst_w, $dst_h ); if ( is_wp_error( $thumb_result ) ) { return $thumb_result; } return $this->update_size( $dst_w, $dst_h ); }Changelog
Introduced in 3.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.7.7 tag, from
src/wp-includes/class-wp-image-editor-imagick.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.