WP_Image_Editor_Imagick::resize PHP Méthode

resize() public méthode

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.
Since: 3.5.0
public resize ( integer | null $max_w, integer | null $max_h, boolean $crop = false ) : boolean | WP_Error
$max_w integer | null Image width.
$max_h integer | null Image height.
$crop boolean
Résultat boolean | WP_Error
    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);
    }

Usage Example

	/**
	 * Test resizing an image including cropping
	 * 
	 */
	public function test_resize_and_crop() {

		$file = DIR_TESTDATA . '/images/gradient-square.jpg';

		$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
		$imagick_image_editor->load();

		$imagick_image_editor->resize( 100, 50, true );

		$this->assertEquals( array( 'width' => 100, 'height' => 50 ), $imagick_image_editor->get_size() );
	}