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
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
$imageparameter, i.e. a non-WP_Image_Editorimage resource orGdImageinstance, the function will return true on success, false on failure.$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_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
- Scaling
- Constant
- Instructions
- 24–62
- Plugin surface
- 5 hooks
- Called by
- 1
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 120.
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.
1 place 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 · 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.
| When | Instructions | Calls it makes |
|---|---|---|
$image instanceof && $saved !== null | 24 | apply_filters(), apply_filters() |
$image instanceof && $saved === null | 28 | apply_filters(), apply_filters(), ->save() |
!($image instanceof) | 43–54 | __(), sprintf(), _deprecated_argument() |
!($image instanceof) && $saved === null | 48–52 | __(), sprintf(), _deprecated_argument(), imagepng() |
!($image instanceof) && $saved === null | 48–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 === null | 54–56 | __(), sprintf(), _deprecated_argument(), apply_filters(), imagejpeg() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 120 | 24–62 | 11 | |
| 8.5 | 120 | 24–62 | 11 | |
| 8.4 | 120 | 24–62 | 11 | |
| 8.3 | 120 | 24–62 | 11 | |
| 8.2 | 120 | 24–62 | 11 | |
| 8.1 | 120 | 24–62 | 11 | 1 more instruction than PHP 7.4 |
| 7.4 | 119 | 24–64 | 11 |
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:
- 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.
- apply_filters( wp_save_image_editor_file )filterline 450 (+20 into the body)
Filters whether to skip saving the image file.
- do_action( image_save_pre )filter_deprecatedline 462 (+32 into the body)
Filters the GD image resource to be streamed to the browser.
- do_action( wp_save_image_file )filter_deprecatedline 479 (+49 into the body)
Filters whether to skip saving the image file.
- apply_filters( jpeg_quality )filterline 493 (+63 into the body)
Filters the JPEG compression quality for backward-compatibility.
Uses · 4
- apply_filters()Calls the callback functions that have been added to a filter hook.
- _deprecated_argument()Marks a function argument as deprecated and inform when it has been used.
- __()Retrieves the translation of $text.
- apply_filters_deprecated()Fires functions attached to a deprecated filter hook.
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.
Signature, return type and hooks compared across 5 parsed releases.
$filesize value was added to the returned array.from the docblock$image parameter expects a WP_Image_Editor instance.from the docblockAbout 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.