WP_Image_Editor_GD::crop()publicWP 3.5.0

Crops Image.

Метод класса: WP_Image_Editor_GD{}

Хуков нет.

Возвращает

true|WP_Error.

Использование

$WP_Image_Editor_GD = new WP_Image_Editor_GD();
$WP_Image_Editor_GD->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs );
$src_x(int) (обязательный)
The start x position to crop from.
$src_y(int) (обязательный)
The start y position to crop from.
$src_w(int) (обязательный)
The width to crop.
$src_h(int) (обязательный)
The height to crop.
$dst_w(int)
The destination width.
По умолчанию: null
$dst_h(int)
The destination height.
По умолчанию: null
$src_abs(true|false)
If the source crop points are absolute.
По умолчанию: false

Список изменений

С версии 3.5.0 Введена.

Код WP_Image_Editor_GD::crop() WP 6.6.2

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

	foreach ( array( $src_w, $src_h, $dst_w, $dst_h ) as $value ) {
		if ( ! is_numeric( $value ) || (int) $value <= 0 ) {
			return new WP_Error( 'image_crop_error', __( 'Image crop failed.' ), $this->file );
		}
	}

	$dst = wp_imagecreatetruecolor( (int) $dst_w, (int) $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, (int) $src_x, (int) $src_y, (int) $dst_w, (int) $dst_h, (int) $src_w, (int) $src_h );

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

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