WP_Image_Editor_GD::_save( resource|GdImage $image, string|null $filename = null, string|null $mime_type = null ): array|WP_Error
- Since
- 3.5.0, 6.0.0
- Source
wp-includes/class-wp-image-editor-gd.php:523
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
$imageresource|GdImage$filenamestring|nulloptional- Default:
null $mime_typestring|nulloptional- 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_GD::_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
- 36–93
- 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 196.
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
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 52 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Image_Editor_GD::_save() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!function_exists() && $mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type !== "image/jpeg" && $mime_type !== "image/webp" && $mime_type !== "image/avif" | 36 | ->get_output_format(), function_exists(), __() |
!function_exists() && $mime_type === "image/gif" | 36 | ->get_output_format(), function_exists(), __(), ->make_image() |
!function_exists() && $mime_type !== "image/gif" | 38–42 | ->get_output_format(), function_exists(), function_exists(), __(), ->make_image() |
!function_exists() && $mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type !== "image/jpeg" && $mime_type !== "image/webp" && $mime_type !== "image/avif" | 42 | ->get_output_format(), ->generate_filename(), function_exists(), __() |
!function_exists() && $mime_type === "image/gif" | 42 | ->get_output_format(), ->generate_filename(), function_exists(), __(), ->make_image() |
!function_exists() && $mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type === "image/jpeg" | 43 | ->get_output_format(), function_exists(), ->get_quality(), __(), ->make_image() |
!function_exists() && $mime_type !== "image/gif" | 44–48 | ->get_output_format(), ->generate_filename(), function_exists(), function_exists(), __(), ->make_image() |
function_exists() && $mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type !== "image/jpeg" && $mime_type !== "image/webp" && $mime_type !== "image/avif" | 45 | ->get_output_format(), function_exists(), apply_filters(), imageinterlace(), __() |
function_exists() && $mime_type === "image/gif" | 45 | ->get_output_format(), function_exists(), apply_filters(), imageinterlace(), __(), ->make_image() |
$mime_type !== "image/gif" && $mime_type === "image/png" && imageistruecolor() | 46 | ->get_output_format(), function_exists(), function_exists(), imageistruecolor(), __(), ->make_image() |
$mime_type !== "image/gif" | 47–51 | ->get_output_format(), function_exists(), apply_filters(), imageinterlace(), function_exists(), __(), ->make_image() |
$mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type !== "image/jpeg" | 49–51 | ->get_output_format(), function_exists(), function_exists(), ->get_quality(), __(), ->make_image() |
40 further outcomes, up to 93 instructions
!function_exists() && $mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type === "image/jpeg" | 49 | ->get_output_format(), ->generate_filename(), function_exists(), ->get_quality(), __(), ->make_image() |
function_exists() && $mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type !== "image/jpeg" && $mime_type !== "image/webp" && $mime_type !== "image/avif" | 51 | ->get_output_format(), ->generate_filename(), function_exists(), apply_filters(), imageinterlace(), __() |
function_exists() && $mime_type === "image/gif" | 51 | ->get_output_format(), ->generate_filename(), function_exists(), apply_filters(), imageinterlace(), __(), ->make_image() |
function_exists() && $mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type === "image/jpeg" | 52 | ->get_output_format(), function_exists(), apply_filters(), imageinterlace(), ->get_quality(), __(), ->make_image() |
$mime_type !== "image/gif" && $mime_type === "image/png" && imageistruecolor() | 52 | ->get_output_format(), ->generate_filename(), function_exists(), function_exists(), imageistruecolor(), __(), ->make_image() |
$mime_type !== "image/gif" | 53–57 | ->get_output_format(), ->generate_filename(), function_exists(), apply_filters(), imageinterlace(), function_exists(), __(), ->make_image() |
$mime_type !== "image/gif" && $mime_type === "image/png" && !imageistruecolor() | 54 | ->get_output_format(), function_exists(), function_exists(), imageistruecolor(), imagecolorstotal(), imagetruecolortopalette(), __(), ->make_image() |
$mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type !== "image/jpeg" | 55–57 | ->get_output_format(), ->generate_filename(), function_exists(), function_exists(), ->get_quality(), __(), ->make_image() |
function_exists() && $mime_type !== "image/gif" && $mime_type === "image/png" && imageistruecolor() | 55 | ->get_output_format(), function_exists(), apply_filters(), imageinterlace(), function_exists(), imageistruecolor(), __(), ->make_image() |
function_exists() && $mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type !== "image/jpeg" | 58–60 | ->get_output_format(), function_exists(), apply_filters(), imageinterlace(), function_exists(), ->get_quality(), __(), ->make_image() |
function_exists() && $mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type === "image/jpeg" | 58 | ->get_output_format(), ->generate_filename(), function_exists(), apply_filters(), imageinterlace(), ->get_quality(), __(), ->make_image() |
!function_exists() && $mime_type === "image/gif" | 60 | ->get_output_format(), function_exists(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
$mime_type !== "image/gif" && $mime_type === "image/png" && !imageistruecolor() | 60 | ->get_output_format(), ->generate_filename(), function_exists(), function_exists(), imageistruecolor(), imagecolorstotal(), imagetruecolortopalette(), __(), ->make_image() |
function_exists() && $mime_type !== "image/gif" && $mime_type === "image/png" && imageistruecolor() | 61 | ->get_output_format(), ->generate_filename(), function_exists(), apply_filters(), imageinterlace(), function_exists(), imageistruecolor(), __(), ->make_image() |
function_exists() && $mime_type !== "image/gif" && $mime_type === "image/png" && !imageistruecolor() | 63 | ->get_output_format(), function_exists(), apply_filters(), imageinterlace(), function_exists(), imageistruecolor(), imagecolorstotal(), imagetruecolortopalette(), __(), ->make_image() |
function_exists() && $mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type !== "image/jpeg" | 64–66 | ->get_output_format(), ->generate_filename(), function_exists(), apply_filters(), imageinterlace(), function_exists(), ->get_quality(), __(), ->make_image() |
!function_exists() && $mime_type !== "image/gif" && $mime_type === "image/png" | 66 | ->get_output_format(), function_exists(), function_exists(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
!function_exists() && $mime_type === "image/gif" | 66 | ->get_output_format(), ->generate_filename(), function_exists(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
!function_exists() && $mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type === "image/jpeg" | 67 | ->get_output_format(), function_exists(), ->get_quality(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
function_exists() && $mime_type === "image/gif" | 69 | ->get_output_format(), function_exists(), apply_filters(), imageinterlace(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
function_exists() && $mime_type !== "image/gif" && $mime_type === "image/png" && !imageistruecolor() | 69 | ->get_output_format(), ->generate_filename(), function_exists(), apply_filters(), imageinterlace(), function_exists(), imageistruecolor(), imagecolorstotal(), imagetruecolortopalette(), __(), ->make_image() |
$mime_type !== "image/gif" && $mime_type === "image/png" && imageistruecolor() | 70 | ->get_output_format(), function_exists(), function_exists(), imageistruecolor(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
!function_exists() && $mime_type !== "image/gif" && $mime_type === "image/png" | 72 | ->get_output_format(), ->generate_filename(), function_exists(), function_exists(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
$mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type !== "image/jpeg" | 73–75 | ->get_output_format(), function_exists(), function_exists(), ->get_quality(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
!function_exists() && $mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type === "image/jpeg" | 73 | ->get_output_format(), ->generate_filename(), function_exists(), ->get_quality(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
$mime_type !== "image/gif" && $mime_type === "image/png" | 75 | ->get_output_format(), function_exists(), apply_filters(), imageinterlace(), function_exists(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
function_exists() && $mime_type === "image/gif" | 75 | ->get_output_format(), ->generate_filename(), function_exists(), apply_filters(), imageinterlace(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
function_exists() && $mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type === "image/jpeg" | 76 | ->get_output_format(), function_exists(), apply_filters(), imageinterlace(), ->get_quality(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
$mime_type !== "image/gif" && $mime_type === "image/png" && imageistruecolor() | 76 | ->get_output_format(), ->generate_filename(), function_exists(), function_exists(), imageistruecolor(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
$mime_type !== "image/gif" && $mime_type === "image/png" && !imageistruecolor() | 78 | ->get_output_format(), function_exists(), function_exists(), imageistruecolor(), imagecolorstotal(), imagetruecolortopalette(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
$mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type !== "image/jpeg" | 79–81 | ->get_output_format(), ->generate_filename(), function_exists(), function_exists(), ->get_quality(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
function_exists() && $mime_type !== "image/gif" && $mime_type === "image/png" && imageistruecolor() | 79 | ->get_output_format(), function_exists(), apply_filters(), imageinterlace(), function_exists(), imageistruecolor(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
$mime_type !== "image/gif" && $mime_type === "image/png" | 81 | ->get_output_format(), ->generate_filename(), function_exists(), apply_filters(), imageinterlace(), function_exists(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
function_exists() && $mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type !== "image/jpeg" | 82–84 | ->get_output_format(), function_exists(), apply_filters(), imageinterlace(), function_exists(), ->get_quality(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
function_exists() && $mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type === "image/jpeg" | 82 | ->get_output_format(), ->generate_filename(), function_exists(), apply_filters(), imageinterlace(), ->get_quality(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
$mime_type !== "image/gif" && $mime_type === "image/png" && !imageistruecolor() | 84 | ->get_output_format(), ->generate_filename(), function_exists(), function_exists(), imageistruecolor(), imagecolorstotal(), imagetruecolortopalette(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
function_exists() && $mime_type !== "image/gif" && $mime_type === "image/png" && imageistruecolor() | 85 | ->get_output_format(), ->generate_filename(), function_exists(), apply_filters(), imageinterlace(), function_exists(), imageistruecolor(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
function_exists() && $mime_type !== "image/gif" && $mime_type === "image/png" && !imageistruecolor() | 87 | ->get_output_format(), function_exists(), apply_filters(), imageinterlace(), function_exists(), imageistruecolor(), imagecolorstotal(), imagetruecolortopalette(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
function_exists() && $mime_type !== "image/gif" && $mime_type !== "image/png" && $mime_type !== "image/jpeg" | 88–90 | ->get_output_format(), ->generate_filename(), function_exists(), apply_filters(), imageinterlace(), function_exists(), ->get_quality(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
function_exists() && $mime_type !== "image/gif" && $mime_type === "image/png" && !imageistruecolor() | 93 | ->get_output_format(), ->generate_filename(), function_exists(), apply_filters(), imageinterlace(), function_exists(), imageistruecolor(), imagecolorstotal(), imagetruecolortopalette(), stat(), chmod(), apply_filters(), wp_basename(), wp_filesize() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 196 | 36–93 | 16 | |
| 8.5 | 196 | 36–93 | 16 | |
| 8.4 | 196 | 36–93 | 16 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 198 | 36–95 | 16 | |
| 8.2 | 198 | 36–95 | 16 | 1 fewer instruction than PHP 8.1 |
| 8.1 | 199 | 37–96 | 16 | |
| 7.4 | 199 | 37–96 | 16 |
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_GD::_save() runs, in this order:
- apply_filters( image_save_progressive )filterline 539 (+16 into the body)
Filters whether to output progressive images (if available).
- apply_filters( image_make_intermediate_size )filterline 589 (+66 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.
- __()Retrieves the translation of $text.
- 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_GD::get_output_format()
- WP_Image_Editor_GD::generate_filename()
- WP_Image_Editor_GD::make_image()Either calls editor's save function or handles file as a stream.
- WP_Error::__construct()Initializes the error.
- WP_Image_Editor_GD::get_quality()
Used by · 2
- WP_Image_Editor_GD::make_subsize()Create an image sub-size and return the image meta data value for it.
- WP_Image_Editor_GD::save()Saves current in-memory 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 ); } if ( function_exists( 'imageinterlace' ) ) { /** * Filters whether to output progressive images (if available). * * @since 6.5.0 * * @param bool $interlace Whether to use progressive images for output if available. Default false. * @param string $mime_type The mime type being saved. */ imageinterlace( $image, apply_filters( 'image_save_progressive', false, $mime_type ) ); } if ( 'image/gif' === $mime_type ) { if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) { return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); } } elseif ( 'image/png' === $mime_type ) { // Convert from full colors to index colors, like original PNG. if ( function_exists( 'imageistruecolor' ) && ! imageistruecolor( $image ) ) { imagetruecolortopalette( $image, false, imagecolorstotal( $image ) ); } if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) ) { return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); } } elseif ( 'image/jpeg' === $mime_type ) { if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) ) { return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); } } elseif ( 'image/webp' === $mime_type ) { if ( ! function_exists( 'imagewebp' ) || ! $this->make_image( $filename, 'imagewebp', array( $image, $filename, $this->get_quality() ) ) ) { return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); } } elseif ( 'image/avif' === $mime_type ) { if ( ! function_exists( 'imageavif' ) || ! $this->make_image( $filename, 'imageavif', array( $image, $filename, $this->get_quality() ) ) ) { return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); } } else { return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); } // 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, /** * Filters the name of the saved image file. * * @since 2.6.0 * * @param string $filename Name of the file. */ '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.9.7 tag, from
src/wp-includes/class-wp-image-editor-gd.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.