wppaste
WordPress

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.
  • $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_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

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
36–93

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

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

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.

WhenInstructionsCalls 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

PHPCompiledExecutedBranchesNotes
8.6-dev19636–9316
8.519636–9316
8.419636–93162 fewer instructions than PHP 8.3
8.319836–9516
8.219836–95161 fewer instruction than PHP 8.1
8.119937–9616
7.419937–9616

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:

  1. apply_filters( image_save_progressive )filterline 539 (+16 into the body)

    Filters whether to output progressive images (if available).

  2. 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

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.

  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 7.1.0 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.