W3cubDocs

/WordPress

WP_Image_Editor_GD::resize( int|null $max_w, int|null $max_h, bool|array $crop = false ): true|WP_Error

Resizes current image.

Description

Wraps ::_resize() which returns a GD resource or GdImage instance.

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.

Parameters

$max_wint|nullrequired
Image width.
$max_hint|nullrequired
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:
  • 0 string
    The x crop position. Accepts 'left' 'center', or 'right'.
  • 1 string
    The y crop position. Accepts 'top', 'center', or 'bottom'.

Default:false

Return

true|WP_Error

Source

public function resize( $max_w, $max_h, $crop = false ) {
	if ( ( $this->size['width'] === $max_w ) && ( $this->size['height'] === $max_h ) ) {
		return true;
	}

	$resized = $this->_resize( $max_w, $max_h, $crop );

	if ( is_gd_image( $resized ) ) {
		imagedestroy( $this->image );
		$this->image = $resized;
		return true;

	} elseif ( is_wp_error( $resized ) ) {
		return $resized;
	}

	return new WP_Error( 'image_resize_error', __( 'Image resize failed.' ), $this->file );
}

Changelog

Version Description
3.5.0 Introduced.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_image_editor_gd/resize