WP_Image_Editor_GD::crop

Advertisement

Summery Summery

Crops Image.

Syntax Syntax

WP_Image_Editor_GD::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 )

Parameters Parameters

$src_x

(Required) The start x position to crop from.

$src_y

(Required) The start y position to crop from.

$src_w

(Required) The width to crop.

$src_h

(Required) The height to crop.

$dst_w

(Optional) The destination width.

Default value: null

$dst_h

(Optional) The destination height.

Default value: null

$src_abs

(Optional) If the source crop points are absolute.

Default value: false

Return Return

(bool|WP_Error)

Source Source

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

	public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
		// 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;
		}

		$dst = wp_imagecreatetruecolor( $dst_w, $dst_h );

		if ( $src_abs ) {
			$src_w -= $src_x;
			$src_h -= $src_y;
		}

		if ( function_exists( 'imageantialias' ) ) {
			imageantialias( $dst, true );
		}

		imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );

		if ( is_resource( $dst ) ) {
			imagedestroy( $this->image );
			$this->image = $dst;
			$this->update_size();
			return true;
		}

		return new WP_Error( 'image_crop_error', __( 'Image crop failed.' ), $this->file );
	}

Advertisement

Changelog Changelog

Changelog
Version Description
3.5.0 Introduced.

Advertisement

Leave a Reply