WP_Image_Editor_GD::resize() WordPress Method

The WP_Image_Editor_GD::resize() function allows you to resize an image. You can specify the width and height of the new image, or you can specify the percentage by which to resize the image.

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

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.


Top ↑

Parameters

$max_w

(int|null)(Required)Image width.

$max_h

(int|null)(Required)Image height.

$crop

(bool)(Optional)

Default value: false


Top ↑

Return

(true|WP_Error)


Top ↑

Source

File: wp-includes/class-wp-image-editor-gd.php

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


Top ↑

Changelog

Changelog
VersionDescription
3.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.