image_resize_dimensions()
Calculates dimensions and coordinates to resize an image so that it fits within a given width and height.
Used when creating smaller copies of images. The function computes which part of the source image to take and to what size it should be reduced or cropped.
If cropping is disabled, the image is scaled preserving proportions. If cropping is enabled, the image is fit to the specified size and the excess part is cut off.
The function does not change the image file by itself. It only calculates the coordinates and sizes that can then be used when resizing the image.
If the new size is larger than the source image, the function will usually return false to avoid creating an enlarged copy.
Hooks from the function
Returns
Array|false.
array— an array with sizes and coordinates in a format suitable forimagecopyresampled():[dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h].false— if the source dimensions are incorrect, no new size specified, the new size is larger than the source, or the new size is practically identical to the original.
Usage
image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop );
- $orig_w(int) (required)
- The width of the source image in pixels.
- $orig_h(int) (required)
- The height of the source image in pixels.
- $dest_w(int) (required)
- The new width of the image in pixels. You can pass
0if the size should be calculated from the height. - $dest_h(int) (required)
- The new height of the image in pixels. You can pass
0if the size should be calculated from the width. - $crop(bool|array)
Determines whether the image should be cropped.
If
false, the image is scaled preserving proportions.If
true, the image is cropped from the center.If an array is passed, you can specify the crop position:
the first value for the X axis (left,center,right),
the second — for the Y axis (top,center,bottom).Default:
false
Examples
#1 Getting dimensions for standard scaling
The image will be scaled down to fit within a 300x300 area without cropping.
$dimensions = image_resize_dimensions( 1200, 800, 300, 300 );
if ( false !== $dimensions ) {
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dimensions;
echo 'New size: ' . $dst_w . 'x' . $dst_h;
} #2 Getting dimensions for center crop
The image will be cropped and fitted to 300x300.
$dimensions = image_resize_dimensions( 1200, 800, 300, 300, true );
if ( false !== $dimensions ) {
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dimensions;
echo 'Cropping starts at point: ' . $src_x . 'x' . $src_y;
} #3 Cropping an image from the top-left corner
You can specify where to take the image area for cropping from.
$dimensions = image_resize_dimensions( 1200, 800, 300, 300, [ 'left', 'top' ] );
if ( false !== $dimensions ) {
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dimensions;
echo 'Source: ' . $src_x . 'x' . $src_y;
} #4 Changing the result via a filter
The image_resize_dimensions filter allows you to completely replace the standard size calculation.
add_filter( 'image_resize_dimensions', 'my_image_resize_dimensions', 10, 6 );
function my_image_resize_dimensions( $output, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) {
if ( 300 === $dest_w && 300 === $dest_h ) {
return [ 0, 0, 0, 0, 300, 300, min( 300, $orig_w ), min( 300, $orig_h ) ];
}
return $output;
}
Changelog
| Since 2.5.0 | Introduced. |