WP_Image_Editor_Imagick::crop( int $src_x, int $src_y, int $src_w, int $src_h, int $dst_w = null, int $dst_h = null, bool $src_abs = false ): true|WP_Error

In this article

Crops Image.

Parameters

$src_xintrequired
The start x position to crop from.
$src_yintrequired
The start y position to crop from.
$src_wintrequired
The width to crop.
$src_hintrequired
The height to crop.
$dst_wintoptional
The destination width.

Default:null

$dst_hintoptional
The destination height.

Default:null

$src_absbooloptional
If the source crop points are absolute.

Default:false

Return

true|WP_Error

Source

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

Changelog

VersionDescription
3.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.