WP_Image_Editor_GD::make_subsize()publicWP 5.3.0

Create an image sub-size and return the image meta data value for it.

Method of the class: WP_Image_Editor_GD{}

No Hooks.

Return

Array|WP_Error. The image data array for inclusion in the sizes array in the image meta, WP_Error object on error.

Usage

$WP_Image_Editor_GD = new WP_Image_Editor_GD();
$WP_Image_Editor_GD->make_subsize( $size_data );
$size_data(array) (required)

Array of size data.

  • width(int)
    The maximum width in pixels.

  • height(int)
    The maximum height in pixels.

  • crop(true|false|array)
    Whether to crop the image to exact dimensions.

Changelog

Since 5.3.0 Introduced.

WP_Image_Editor_GD::make_subsize() code WP 6.4.3

public function make_subsize( $size_data ) {
	if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
		return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) );
	}

	$orig_size = $this->size;

	if ( ! isset( $size_data['width'] ) ) {
		$size_data['width'] = null;
	}

	if ( ! isset( $size_data['height'] ) ) {
		$size_data['height'] = null;
	}

	if ( ! isset( $size_data['crop'] ) ) {
		$size_data['crop'] = false;
	}

	$resized = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );

	if ( is_wp_error( $resized ) ) {
		$saved = $resized;
	} else {
		$saved = $this->_save( $resized );
		imagedestroy( $resized );
	}

	$this->size = $orig_size;

	if ( ! is_wp_error( $saved ) ) {
		unset( $saved['path'] );
	}

	return $saved;
}