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:1018
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.
$pathstringPath to the image file.$filestringName of the image file.$widthintImage width.$heightintImage height.$mime-typestringThe mime type of the image.$filesizeintFile 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
- Scaling
- Constant
- Instructions
- 48–123
- Plugin surface
- 2 hooks
- Called by
- 2
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 147.
Third-party callbacks on 'image_save_progressive', 'image_make_intermediate_size' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls 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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 147 | 48–123 | 7 | |
| 8.5 | 147 | 48–123 | 7 | |
| 8.4 | 147 | 48–123 | 7 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 149 | 48–125 | 7 | |
| 8.2 | 149 | 48–125 | 7 | |
| 8.1 | 149 | 48–125 | 7 | |
| 7.4 | 149 | 48–125 | 7 |
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:
- apply_filters( image_save_progressive )filterline 1041 (+23 into the body)
Filters whether to output progressive images (if available).
- apply_filters( image_make_intermediate_size )filterline 1072 (+54 into the body)
Filters the name of the saved image file.
Uses · 9
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_basename()i18n-friendly version of basename().
- wp_filesize()Wrapper for PHP filesize with filters and casting the result as an integer.
- WP_Image_Editor_Imagick::get_output_format()
- WP_Image_Editor_Imagick::generate_filename()
- WP_Image_Editor_Imagick::get_extension()
- WP_Error::__construct()Initializes the error.
- WP_Image_Editor_Imagick::write_image()Writes an image to a file or stream.
Used by · 2
- WP_Image_Editor_Imagick::make_subsize()Create an image sub-size and return the image meta data value for it.
- WP_Image_Editor_Imagick::save()Saves current image to file.
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.
Signature, return type and hooks compared across 5 parsed releases.
$filesize value was added to the returned array.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.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.