WP_Image_Editor_Imagick::_save( Imagick $image, string $filename = null, string $mime_type = null ): array|WP_Error

In this article

Parameters

$imageImagickrequired
$filenamestringoptional

Default:null

$mime_typestringoptional

Default:null

Return

array|WP_Error Array on success or WP_Error if the file failed to save.
  • path string
    Path to the image file.
  • file string
    Name of the image file.
  • width int
    Image width.
  • height int
    Image height.
  • mime-type string
    The mime type of the image.
  • filesize int
    File size of the image.

Source

protected function _save( $image, $filename = null, $mime_type = null ) {
	list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );

	if ( ! $filename ) {
		$filename = $this->generate_filename( null, null, $extension );
	}

	try {
		// Store initial format.
		$orig_format = $this->image->getImageFormat();

		$this->image->setImageFormat( strtoupper( $this->get_extension( $mime_type ) ) );
	} catch ( Exception $e ) {
		return new WP_Error( 'image_save_error', $e->getMessage(), $filename );
	}

	if ( method_exists( $this->image, 'setInterlaceScheme' )
		&& method_exists( $this->image, 'getInterlaceScheme' )
		&& defined( 'Imagick::INTERLACE_PLANE' )
	) {
		$orig_interlace = $this->image->getInterlaceScheme();

		/** This filter is documented in wp-includes/class-wp-image-editor-gd.php */
		if ( apply_filters( 'image_save_progressive', false, $mime_type ) ) {
			$this->image->setInterlaceScheme( Imagick::INTERLACE_PLANE ); // True - line interlace output.
		} else {
			$this->image->setInterlaceScheme( Imagick::INTERLACE_NO ); // False - no interlace output.
		}
	}

	$write_image_result = $this->write_image( $this->image, $filename );
	if ( is_wp_error( $write_image_result ) ) {
		return $write_image_result;
	}

	try {
		// Reset original format.
		$this->image->setImageFormat( $orig_format );

		if ( isset( $orig_interlace ) ) {
			$this->image->setInterlaceScheme( $orig_interlace );
		}
	} catch ( Exception $e ) {
		return new WP_Error( 'image_save_error', $e->getMessage(), $filename );
	}

	// Set correct file permissions.
	$stat  = stat( dirname( $filename ) );
	$perms = $stat['mode'] & 0000666; // Same permissions as parent folder, strip off the executable bits.
	chmod( $filename, $perms );

	return array(
		'path'      => $filename,
		/** This filter is documented in wp-includes/class-wp-image-editor-gd.php */
		'file'      => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
		'width'     => $this->size['width'],
		'height'    => $this->size['height'],
		'mime-type' => $mime_type,
		'filesize'  => wp_filesize( $filename ),
	);
}

Hooks

apply_filters( ‘image_make_intermediate_size’, string $filename )

Filters the name of the saved image file.

apply_filters( ‘image_save_progressive’, bool $interlace, string $mime_type )

Filters whether to output progressive images (if available).

Changelog

VersionDescription
6.0.0The $filesize value was added to the returned array.
3.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.