image_constrain_size_for_editor( int $width, int $height, string|int[] $size = 'medium', string $context = null ): int[]
- Since
- 2.5.0
- Source
wp-includes/media.php:65
Description
This is so that the image is a better fit for the editor and theme.
The $size parameter accepts either an array or a string. The supported string values are 'thumb' or 'thumbnail' for the given thumbnail size or defaults at 128 width and 96 height in pixels. Also supported for the string value is 'medium', 'medium_large' and 'full'. The 'full' isn't actually supported, but any value other than the supported will result in the content_width size or 500 if that is not set.
Finally, there is a filter named 'editor_max_image_size', that will be called on the calculated array for width and height, respectively.
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
$widthint- Width of the image in pixels.
$heightint- Height of the image in pixels.
$sizestring|int[]optional- Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default 'medium'.Default:
'medium' $contextstringoptional- Could be 'display' (like in a theme) or 'edit' (like inserting into an editor). Default null.Default:
null
Return value
int[]- An array of width and height values.
$0intThe maximum width in pixels.$1intThe maximum height in pixels.
Performance profile
How much work a call to image_constrain_size_for_editor() 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
- Moderate
- Scaling
- Constant
- Instructions
- 34–70
- Plugin surface
- 1 hook
- Called by
- 3
Reads stored settings via get_option(), cached per request but not free on a cold cache.
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 128.
Third-party callbacks on 'editor_max_image_size' 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
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get()one call below image_constrain_size_for_editor() - serializeserialisation
maybe_unserialize()one call below image_constrain_size_for_editor()
Further down the call graph this can also reach query 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 · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost image_constrain_size_for_editor() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 34–45 | wp_get_additional_image_sizes(), wp_constrain_dimensions() |
| always | 39–51 | wp_get_additional_image_sizes(), is_admin(), wp_constrain_dimensions() |
!is_array($size) | 42–56 | wp_get_additional_image_sizes(), get_option(), get_option(), wp_constrain_dimensions() |
!is_array($size) | 47–62 | wp_get_additional_image_sizes(), is_admin(), get_option(), get_option(), wp_constrain_dimensions() |
!is_array($size) && $size !== "thumb" && $size !== "thumbnail" && $size !== "medium" && $size !== "medium_large" && $size !== "large" && !empty($_wp_additional_image_sizes) | 51–64 | wp_get_additional_image_sizes(), array_keys(), wp_constrain_dimensions() |
!is_array($size) && $size !== "thumb" && $size !== "thumbnail" && $size !== "medium" && $size !== "medium_large" && $size !== "large" && !empty($_wp_additional_image_sizes) | 56–70 | wp_get_additional_image_sizes(), is_admin(), array_keys(), wp_constrain_dimensions() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 128 | 34–70 | 16 | |
| 8.5 | 128 | 34–70 | 16 | |
| 8.4 | 128 | 34–70 | 16 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 140 | 34–76 | 16 | |
| 8.2 | 140 | 34–76 | 16 | |
| 8.1 | 140 | 34–76 | 16 | |
| 7.4 | 140 | 34–76 | 16 |
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 image_constrain_size_for_editor() runs, in this order:
- apply_filters( editor_max_image_size )filterline 138 (+73 into the body)
Filters the maximum image size dimensions for the editor.
Uses · 5
- wp_get_additional_image_sizes()Retrieves additional image sizes.
- is_admin()Determines whether the current request is for an administrative interface page.
- get_option()Retrieves an option value based on an option name.
- 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.
Used by · 3
- image_downsize()Scales an image to fit a particular size (such as 'thumb' or 'medium').
- image_get_intermediate_size()Retrieves the image's intermediate size (resized) path, width, and height.
- wp_prepare_attachment_for_js()Prepares an attachment post object for JS, where it is expected to be JSON-encoded and fit into an Attachment model.
Source code
function image_constrain_size_for_editor( $width, $height, $size = 'medium', $context = null ) { global $content_width; $_wp_additional_image_sizes = wp_get_additional_image_sizes(); if ( ! $context ) { $context = is_admin() ? 'edit' : 'display'; } if ( is_array( $size ) ) { $max_width = $size[0]; $max_height = $size[1]; } elseif ( 'thumb' === $size || 'thumbnail' === $size ) { $max_width = (int) get_option( 'thumbnail_size_w' ); $max_height = (int) get_option( 'thumbnail_size_h' ); // Last chance thumbnail size defaults. if ( ! $max_width && ! $max_height ) { $max_width = 128; $max_height = 96; } } elseif ( 'medium' === $size ) { $max_width = (int) get_option( 'medium_size_w' ); $max_height = (int) get_option( 'medium_size_h' ); } elseif ( 'medium_large' === $size ) { $max_width = (int) get_option( 'medium_large_size_w' ); $max_height = (int) get_option( 'medium_large_size_h' ); if ( (int) $content_width > 0 ) { $max_width = min( (int) $content_width, $max_width ); } } elseif ( 'large' === $size ) { /* * We're inserting a large size image into the editor. If it's a really * big image we'll scale it down to fit reasonably within the editor * itself, and within the theme's content width if it's known. The user * can resize it in the editor if they wish. */ $max_width = (int) get_option( 'large_size_w' ); $max_height = (int) get_option( 'large_size_h' ); if ( (int) $content_width > 0 ) { $max_width = min( (int) $content_width, $max_width ); } } elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ), true ) ) { $max_width = (int) $_wp_additional_image_sizes[ $size ]['width']; $max_height = (int) $_wp_additional_image_sizes[ $size ]['height']; // Only in admin. Assume that theme authors know what they're doing. if ( (int) $content_width > 0 && 'edit' === $context ) { $max_width = min( (int) $content_width, $max_width ); } } else { // $size === 'full' has no constraint. $max_width = $width; $max_height = $height; } /** * Filters the maximum image size dimensions for the editor. * * @since 2.5.0 * * @param int[] $max_image_size { * An array of width and height values. * * @type int $0 The maximum width in pixels. * @type int $1 The maximum height in pixels. * } * @param string|int[] $size Requested image size. Can be any registered image size name, or * an array of width and height values in pixels (in that order). * @param string $context The context the image is being resized for. * Possible values are 'display' (like in a theme) * or 'edit' (like inserting into an editor). */ list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size, $context ); return wp_constrain_dimensions( $width, $height, $max_width, $max_height );}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.