wppaste
WordPress

wp_save_image_file( string $filename, WP_Image_Editor $image, string $mime_type, int $post_id ): array|WP_Error|bool

Since
2.9.0, 3.5.0, 6.0.0
Source
wp-admin/includes/image-edit.php:430
Saves image to file.

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

$filenamestring
Name of the file to be saved.
$imageWP_Image_Editor
The image editor instance.
$mime_typestring
The mime type of the image.
$post_idint
Attachment post ID.

Return value

array|WP_Error|bool
Array on success or WP_Error if the file failed to save. When called with a deprecated value for the $image parameter, i.e. a non-WP_Image_Editor image resource or GdImage instance, the function will return true on success, false on failure.
  • $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_save_image_file() 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
24–62

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

Plugin surface
5 hooks

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

Called by
1

1 place 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 · 9 distinct outcomes

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

WhenInstructionsCalls it makes
$image instanceof && $saved !== null24apply_filters(), apply_filters()
$image instanceof && $saved === null28apply_filters(), apply_filters(), ->save()
!($image instanceof)43–54__(), sprintf(), _deprecated_argument()
!($image instanceof) && $saved === null48–52__(), sprintf(), _deprecated_argument(), imagepng()
!($image instanceof) && $saved === null48–54__(), sprintf(), _deprecated_argument(), imagegif()
!($image instanceof) && $saved === null && !function_exists()48–58__(), sprintf(), _deprecated_argument(), function_exists()
!($image instanceof) && $saved === null && function_exists()52–60__(), sprintf(), _deprecated_argument(), function_exists(), imagewebp()
!($image instanceof) && $saved === null && function_exists()52–62__(), sprintf(), _deprecated_argument(), function_exists(), imageavif()
!($image instanceof) && $saved === null54–56__(), sprintf(), _deprecated_argument(), apply_filters(), imagejpeg()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12024–6211
8.512024–6211
8.412024–6211
8.312024–6211
8.212024–6211
8.112024–62111 more instruction than PHP 7.4
7.411924–6411

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 · 5

5 hooks fire while wp_save_image_file() runs, in this order:

  1. apply_filters( image_editor_save_pre )filterline 434 (+4 into the body)

    Filters the WP_Image_Editor instance for the image to be streamed to the browser.

  2. apply_filters( wp_save_image_editor_file )filterline 450 (+20 into the body)

    Filters whether to skip saving the image file.

  3. do_action( image_save_pre )filter_deprecatedline 462 (+32 into the body)

    Filters the GD image resource to be streamed to the browser.

  4. do_action( wp_save_image_file )filter_deprecatedline 479 (+49 into the body)

    Filters whether to skip saving the image file.

  5. apply_filters( jpeg_quality )filterline 493 (+63 into the body)

    Filters the JPEG compression quality for backward-compatibility.

Uses · 4

Used by · 1

  • wp_save_image()Saves image to post, along with enqueued changes in `$_REQUEST['history']`.

Source code

function wp_save_image_file( $filename, $image, $mime_type, $post_id ) {	if ( $image instanceof WP_Image_Editor ) { 		/** This filter is documented in wp-admin/includes/image-edit.php */		$image = apply_filters( 'image_editor_save_pre', $image, $post_id ); 		/**		 * Filters whether to skip saving the image file.		 *		 * Returning a non-null value will short-circuit the save method,		 * returning that value instead.		 *		 * @since 3.5.0		 *		 * @param bool|null       $override  Value to return instead of saving. Default null.		 * @param string          $filename  Name of the file to be saved.		 * @param WP_Image_Editor $image     The image editor instance.		 * @param string          $mime_type The mime type of the image.		 * @param int             $post_id   Attachment post ID.		 */		$saved = apply_filters( 'wp_save_image_editor_file', null, $filename, $image, $mime_type, $post_id ); 		if ( null !== $saved ) {			return $saved;		} 		return $image->save( $filename, $mime_type );	} else {		/* translators: 1: $image, 2: WP_Image_Editor */		_deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) ); 		/** This filter is documented in wp-admin/includes/image-edit.php */		$image = apply_filters_deprecated( 'image_save_pre', array( $image, $post_id ), '3.5.0', 'image_editor_save_pre' ); 		/**		 * Filters whether to skip saving the image file.		 *		 * Returning a non-null value will short-circuit the save method,		 * returning that value instead.		 *		 * @since 2.9.0		 * @deprecated 3.5.0 Use {@see 'wp_save_image_editor_file'} instead.		 *		 * @param bool|null        $override  Value to return instead of saving. Default null.		 * @param string           $filename  Name of the file to be saved.		 * @param resource|GdImage $image     Image resource or GdImage instance.		 * @param string           $mime_type The mime type of the image.		 * @param int              $post_id   Attachment post ID.		 */		$saved = apply_filters_deprecated(			'wp_save_image_file',			array( null, $filename, $image, $mime_type, $post_id ),			'3.5.0',			'wp_save_image_editor_file'		); 		if ( null !== $saved ) {			return $saved;		} 		switch ( $mime_type ) {			case 'image/jpeg':				/** This filter is documented in wp-includes/class-wp-image-editor.php */				return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) );			case 'image/png':				return imagepng( $image, $filename );			case 'image/gif':				return imagegif( $image, $filename );			case 'image/webp':				if ( function_exists( 'imagewebp' ) ) {					return imagewebp( $image, $filename );				}				return false;			case 'image/avif':				if ( function_exists( 'imageavif' ) ) {					return imageavif( $image, $filename );				}				return false;			default:				return false;

Changelog

Introduced in 2.9.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
The $image parameter expects a WP_Image_Editor instance.from the docblock
2.9.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-admin/includes/image-edit.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.