WP_Image_Editor_Imagick::crop() WordPress Method

The WP_Image_Editor_Imagick::crop() function is used to crop an image. It takes the following parameters: $src_x - The x-coordinate of the top-left corner of the crop region. $src_y - The y-coordinate of the top-left corner of the crop region. $src_w - The width of the crop region. $src_h - The height of the crop region. $dst_w - The width of the resulting image. $dst_h - The height of the resulting image. The function returns a WP_Error on failure.

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 ) #

Crops Image.


Parameters

$src_x

(int)(Required)The start x position to crop from.

$src_y

(int)(Required)The start y position to crop from.

$src_w

(int)(Required)The width to crop.

$src_h

(int)(Required)The height to crop.

$dst_w

(int)(Optional) The destination width.

Default value: null

$dst_h

(int)(Optional) The destination height.

Default value: null

$src_abs

(bool)(Optional) If the source crop points are absolute.

Default value: false


Top ↑

Return

(true|WP_Error)


Top ↑

Source

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

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


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.