WP_Image_Editor_Imagick::crop PHP Method

crop() public method

Crops Image.
Since: 3.5.0
public crop ( integer $src_x, integer $src_y, integer $src_w, integer $src_h, integer $dst_w = null, integer $dst_h = null, boolean $src_abs = false ) : boolean | WP_Error
$src_x integer The start x position to crop from.
$src_y integer The start y position to crop from.
$src_w integer The width to crop.
$src_h integer The height to crop.
$dst_w integer Optional. The destination width.
$dst_h integer Optional. The destination height.
$src_abs boolean Optional. If the source crop points are absolute.
return boolean | WP_Error
    public function crop($src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false)
    {
        if ($src_abs) {
            $src_w -= $src_x;
            $src_h -= $src_y;
        }
        try {
            $this->image->cropImage($src_w, $src_h, $src_x, $src_y);
            $this->image->setImagePage($src_w, $src_h, 0, 0);
            if ($dst_w || $dst_h) {
                // If destination width/height isn't specified, use same as
                // width/height from source.
                if (!$dst_w) {
                    $dst_w = $src_w;
                }
                if (!$dst_h) {
                    $dst_h = $src_h;
                }
                $thumb_result = $this->thumbnail_image($dst_w, $dst_h);
                if (is_wp_error($thumb_result)) {
                    return $thumb_result;
                }
                return $this->update_size();
            }
        } catch (Exception $e) {
            return new WP_Error('image_crop_error', $e->getMessage());
        }
        return $this->update_size();
    }

Usage Example

	/**
	 * Test cropping an image
	 */
	public function test_crop() {

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

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

		$imagick_image_editor->crop( 0, 0, 50, 50 );

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

	}
All Usage Examples Of WP_Image_Editor_Imagick::crop