WP_Image_Editor_Imagick::make_subsize() WordPress Method

The WP_Image_Editor_Imagick::make_subsize() function is used to create a new image with the specified dimensions. This function is useful for creating thumbnails or for cropping an image.

WP_Image_Editor_Imagick::make_subsize( array $size_data ) #

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


Parameters

$size_data

(array)(Required)Array of size data.

  • 'width'
    (int) The maximum width in pixels.
  • 'height'
    (int) The maximum height in pixels.
  • 'crop'
    (bool) Whether to crop the image to exact dimensions.


Top ↑

Return

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


Top ↑

Source

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

	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;
		$orig_image = $this->image->getImage();

		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( $this->image );

			$this->image->clear();
			$this->image->destroy();
			$this->image = null;
		}

		$this->size  = $orig_size;
		$this->image = $orig_image;

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

		return $saved;
	}


Top ↑

Changelog

Changelog
VersionDescription
5.3.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.