wppaste
WordPress

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

Since
3.5.0, 6.0.0
Source
wp-includes/class-wp-image-editor-imagick.php:1119

Compatibility

WordPress
since 6.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$imageImagick
$filenamestringoptional
Default: null
$mime_typestringoptional
Default: null

Return value

array|WP_Error
Array on success or WP_Error if the file failed to save.
  • $pathstring

    Path to the image file.
  • $filestring

    Name of the image file.
  • $widthint

    Image width.
  • $heightint

    Image height.
  • $mime-typestring

    The mime type of the image.
  • $filesizeint

    File size of the image.

Performance profile

How much work a call to WP_Image_Editor_Imagick::_save() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
48–123

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 147.

Plugin surface
2 hooks

Third-party callbacks on 'image_save_progressive', 'image_make_intermediate_size' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

What one call costs · 24 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Image_Editor_Imagick::_save() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!method_exists() && is_wp_error()48->get_output_format(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), ->write_image(), is_wp_error()
is_wp_error()54->get_output_format(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), ->write_image(), is_wp_error()
!method_exists() && is_wp_error()54->get_output_format(), ->generate_filename(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), ->write_image(), is_wp_error()
method_exists() && !defined() && is_wp_error()58->get_output_format(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), defined(), ->write_image(), is_wp_error()
is_wp_error()60->get_output_format(), ->generate_filename(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), ->write_image(), is_wp_error()
method_exists() && !defined() && is_wp_error()64->get_output_format(), ->generate_filename(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), defined(), ->write_image(), is_wp_error()
method_exists() && defined() && is_wp_error()73–74->get_output_format(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), defined(), ->getInterlaceScheme(), apply_filters(), ->setInterlaceScheme(), ->write_image(), is_wp_error()
method_exists() && defined() && is_wp_error()79–80->get_output_format(), ->generate_filename(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), defined(), ->getInterlaceScheme(), apply_filters(), ->setInterlaceScheme(), ->write_image(), is_wp_error()
!method_exists() && !is_wp_error() && !isset($orig_interlace)86->get_output_format(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), ->write_image(), is_wp_error(), ->setImageFormat(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize()
!method_exists() && !is_wp_error() && isset($orig_interlace)91->get_output_format(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), ->write_image(), is_wp_error(), ->setImageFormat(), ->setInterlaceScheme(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize()
!is_wp_error() && !isset($orig_interlace)92->get_output_format(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), ->write_image(), is_wp_error(), ->setImageFormat(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize()
!method_exists() && !is_wp_error() && !isset($orig_interlace)92->get_output_format(), ->generate_filename(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), ->write_image(), is_wp_error(), ->setImageFormat(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize()
12 further outcomes, up to 123 instructions
method_exists() && !defined() && !is_wp_error() && !isset($orig_interlace)96->get_output_format(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), defined(), ->write_image(), is_wp_error(), ->setImageFormat(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize()
!is_wp_error() && isset($orig_interlace)97->get_output_format(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), ->write_image(), is_wp_error(), ->setImageFormat(), ->setInterlaceScheme(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize()
!method_exists() && !is_wp_error() && isset($orig_interlace)97->get_output_format(), ->generate_filename(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), ->write_image(), is_wp_error(), ->setImageFormat(), ->setInterlaceScheme(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize()
!is_wp_error() && !isset($orig_interlace)98->get_output_format(), ->generate_filename(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), ->write_image(), is_wp_error(), ->setImageFormat(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize()
method_exists() && !defined() && !is_wp_error() && isset($orig_interlace)101->get_output_format(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), defined(), ->write_image(), is_wp_error(), ->setImageFormat(), ->setInterlaceScheme(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize()
method_exists() && !defined() && !is_wp_error() && !isset($orig_interlace)102->get_output_format(), ->generate_filename(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), defined(), ->write_image(), is_wp_error(), ->setImageFormat(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize()
!is_wp_error() && isset($orig_interlace)103->get_output_format(), ->generate_filename(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), ->write_image(), is_wp_error(), ->setImageFormat(), ->setInterlaceScheme(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize()
method_exists() && !defined() && !is_wp_error() && isset($orig_interlace)107->get_output_format(), ->generate_filename(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), defined(), ->write_image(), is_wp_error(), ->setImageFormat(), ->setInterlaceScheme(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize()
method_exists() && defined() && !is_wp_error() && !isset($orig_interlace)111–112->get_output_format(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), defined(), ->getInterlaceScheme(), apply_filters(), ->setInterlaceScheme(), ->write_image(), is_wp_error(), ->setImageFormat(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize()
method_exists() && defined() && !is_wp_error() && isset($orig_interlace)116–117->get_output_format(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), defined(), ->getInterlaceScheme(), apply_filters(), ->setInterlaceScheme(), ->write_image(), is_wp_error(), ->setImageFormat(), ->setInterlaceScheme(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize()
method_exists() && defined() && !is_wp_error() && !isset($orig_interlace)117–118->get_output_format(), ->generate_filename(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), defined(), ->getInterlaceScheme(), apply_filters(), ->setInterlaceScheme(), ->write_image(), is_wp_error(), ->setImageFormat(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize()
method_exists() && defined() && !is_wp_error() && isset($orig_interlace)122–123->get_output_format(), ->generate_filename(), ->getImageFormat(), ->get_extension(), strtoupper(), ->setImageFormat(), method_exists(), method_exists(), defined(), ->getInterlaceScheme(), apply_filters(), ->setInterlaceScheme(), ->write_image(), is_wp_error(), ->setImageFormat(), ->setInterlaceScheme(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev14748–1237
8.514748–1237
8.414748–12372 fewer instructions than PHP 8.3
8.314948–1257
8.214948–1257
8.114948–1257
7.414948–1257

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while WP_Image_Editor_Imagick::_save() runs, in this order:

  1. apply_filters( image_save_progressive )filterline 1142 (+23 into the body)

    Filters whether to output progressive images (if available).

  2. apply_filters( image_make_intermediate_size )filterline 1173 (+54 into the body)

    Filters the name of the saved image file.

Uses · 9

Used by · 2

Source code

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

Changelog

Introduced in 3.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

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

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/class-wp-image-editor-imagick.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.