WP_Image_Editor_Imagick::resize()publicWP 3.5.0

Resizes current image.

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.

Method of the class: WP_Image_Editor_Imagick{}

No Hooks.

Return

true|WP_Error.

Usage

$WP_Image_Editor_Imagick = new WP_Image_Editor_Imagick();
$WP_Image_Editor_Imagick->resize( $max_w, $max_h, $crop );
$max_w(int|null) (required)
Image width.
$max_h(int|null) (required)
Image height.
$crop(true|false|array)

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:

Default: false

  • 0(string)
    The x crop position. Accepts 'left' 'center', or 'right'.

  • 1(string)
    The y crop position. Accepts 'top', 'center', or 'bottom'.

Changelog

Since 3.5.0 Introduced.

WP_Image_Editor_Imagick::resize() code WP 6.5.2

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 );
}