wp_save_image( int $post_id ): stdClass
- Since
- 2.9.0
- Source
wp-admin/includes/image-edit.php:904
$_REQUEST['history'].Compatibility
- WordPress
- since 2.9.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
$post_idint- Attachment post ID.
Return value
stdClass
Performance profile
How much work a call to wp_save_image() 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
- Heavy
- Scaling
- Scales with input
- Instructions
- 65–259
- Plugin surface
- 1 hook
- Called by
- 1
Reads or writes the filesystem via file_exists().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 546.
Third-party callbacks on 'image_edit_thumbnails_separately' 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
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()called directly - serializeserialisation
json_decode()called directly - filesystemfilesystem access
file_exists()called directly - cacheobject cache
wp_cache_get()one call below wp_save_image()
Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 21 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_save_image() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_wp_error() && empty($value) && $value | 65 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && !isset($sizes) | 195–203 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && !isset($sizes) | 198–206 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), wp_delete_file(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && isset($sizes) | 208–217 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), ->multi_resize(), array_merge(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && !isset($sizes) && $target !== "thumbnail" && $target !== "all" && $target !== "full" | 209–217 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), wp_update_attachment_metadata(), update_post_meta(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && isset($sizes) | 211–220 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), ->multi_resize(), array_merge(), wp_delete_file(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && !isset($sizes) && $target !== "thumbnail" && $target !== "all" && $target !== "full" | 212–220 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), wp_update_attachment_metadata(), update_post_meta(), wp_delete_file(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && !isset($sizes) | 219–235 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), wp_update_attachment_metadata(), update_post_meta(), wp_get_attachment_url(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && !isset($sizes) | 222–234 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), wp_update_attachment_metadata(), update_post_meta(), wp_get_attachment_image_src(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && !isset($sizes) | 222–238 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), wp_update_attachment_metadata(), update_post_meta(), wp_get_attachment_url(), wp_delete_file(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && isset($sizes) && $target !== "thumbnail" && $target !== "all" && $target !== "full" | 222–231 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), ->multi_resize(), array_merge(), wp_update_attachment_metadata(), update_post_meta(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && !isset($sizes) | 225–237 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), wp_update_attachment_metadata(), update_post_meta(), wp_get_attachment_image_src(), wp_delete_file(), __(), esc_js() |
9 further outcomes, up to 259 instructions
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && isset($sizes) && $target !== "thumbnail" && $target !== "all" && $target !== "full" | 225–233 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), ->multi_resize(), array_merge(), wp_update_attachment_metadata(), update_post_meta(), wp_delete_file(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && !isset($sizes) | 227–243 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), wp_update_attachment_metadata(), update_post_meta(), wp_get_attachment_url(), path_join(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && !isset($sizes) | 230–246 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), wp_update_attachment_metadata(), update_post_meta(), wp_get_attachment_url(), path_join(), wp_delete_file(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && isset($sizes) | 232–248 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), ->multi_resize(), array_merge(), wp_update_attachment_metadata(), update_post_meta(), wp_get_attachment_url(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && isset($sizes) | 235–247 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), ->multi_resize(), array_merge(), wp_update_attachment_metadata(), update_post_meta(), wp_get_attachment_image_src(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && isset($sizes) | 235–251 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), ->multi_resize(), array_merge(), wp_update_attachment_metadata(), update_post_meta(), wp_get_attachment_url(), wp_delete_file(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && isset($sizes) | 238–250 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), ->multi_resize(), array_merge(), wp_update_attachment_metadata(), update_post_meta(), wp_get_attachment_image_src(), wp_delete_file(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && isset($sizes) | 240–256 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), ->multi_resize(), array_merge(), wp_update_attachment_metadata(), update_post_meta(), wp_get_attachment_url(), path_join(), __(), esc_js() |
!is_wp_error() && $value && is_array($meta) && is_array($backup_sizes) && !defined('IMAGE_EDIT_OVERWRITE') && !file_exists() && $target === "nothumb" && !isset($backup_sizes) && $path === false && isset($sizes) | 243–259 | wp_get_additional_image_sizes(), get_post(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), apply_filters(), wp_unslash(), json_decode(), wp_get_attachment_metadata(), get_post_meta(), get_attached_file(), pathinfo(), pathinfo(), pathinfo(), pathinfo(), time(), rand(), file_exists(), wp_save_image_file(), _wp_relative_upload_path(), ->get_size(), ->multi_resize(), array_merge(), wp_update_attachment_metadata(), update_post_meta(), wp_get_attachment_url(), path_join(), wp_delete_file(), __(), esc_js() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 545 | 65–259 | 70 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 546 | 65–259 | 70 | |
| 8.4 | 546 | 65–259 | 70 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 558 | 65–264 | 70 | |
| 8.2 | 558 | 65–264 | 70 | |
| 8.1 | 558 | 65–264 | 70 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 561 | 65–264 | 70 |
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 · 1
One hook fires while wp_save_image() runs, in this order:
- apply_filters( image_edit_thumbnails_separately )filterline 927 (+23 into the body)
Shows the settings in the Image Editor that allow selecting to edit only the thumbnail of an image.
Uses · 25
- wp_get_additional_image_sizes()Retrieves additional image sizes.
- get_post()Retrieves post data given a post ID or post object.
- wp_get_image_editor()Returns a WP_Image_Editor instance and loads file into it.
- _load_image_to_edit_path()Retrieves the path or URL of an attachment's attached file.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- esc_js()Escapes single quotes, `"`, ``, `&`, and fixes line endings.
- __()Retrieves the translation of $text.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- image_edit_apply_changes()Performs group of changes on Editor specified.
- wp_get_attachment_metadata()Retrieves attachment metadata for attachment ID.
- get_post_meta()Retrieves a post meta field for the given post ID.
Show all 25
- get_attached_file()Retrieves attached file path based on attachment ID.
- wp_save_image_file()Saves image to file.
- update_attached_file()Updates attachment file path based on attachment ID.
- _wp_relative_upload_path()Returns relative path to an uploaded file.
- get_intermediate_image_sizes()Gets the available intermediate image size names.
- path_join()Joins two filesystem paths together.
- wp_delete_file()Deletes a file.
- get_option()Retrieves an option value based on an option name.
- wp_update_attachment_metadata()Updates metadata for an attachment.
- update_post_meta()Updates a post meta field based on the given post ID.
- wp_get_attachment_image_src()Retrieves an image to represent an attachment.
- wp_get_attachment_url()Retrieves the URL for an attachment.
- stdClass::__construct()
Used by · 1
- wp_ajax_image_editor()Handles image editing via AJAX.
Source code
function wp_save_image( $post_id ) { $_wp_additional_image_sizes = wp_get_additional_image_sizes(); $return = new stdClass(); $success = false; $delete = false; $scaled = false; $nocrop = false; $post = get_post( $post_id ); $img = wp_get_image_editor( _load_image_to_edit_path( $post_id, 'full' ) ); if ( is_wp_error( $img ) ) { $return->error = esc_js( __( 'Unable to create new image.' ) ); return $return; } $full_width = ! empty( $_REQUEST['fwidth'] ) ? (int) $_REQUEST['fwidth'] : 0; $full_height = ! empty( $_REQUEST['fheight'] ) ? (int) $_REQUEST['fheight'] : 0; $target = ! empty( $_REQUEST['target'] ) ? preg_replace( '/[^a-z0-9_-]+/i', '', $_REQUEST['target'] ) : ''; $scale = ! empty( $_REQUEST['do'] ) && 'scale' === $_REQUEST['do']; /** This filter is documented in wp-admin/includes/image-edit.php */ $edit_thumbnails_separately = (bool) apply_filters( 'image_edit_thumbnails_separately', false ); if ( $scale ) { $size = $img->get_size(); $original_width = $size['width']; $original_height = $size['height']; if ( $full_width > $original_width || $full_height > $original_height ) { $return->error = esc_js( __( 'Images cannot be scaled to a size larger than the original.' ) ); return $return; } if ( $full_width > 0 && $full_height > 0 ) { // Check if it has roughly the same w / h ratio. $diff = round( $original_width / $original_height, 2 ) - round( $full_width / $full_height, 2 ); if ( -0.1 < $diff && $diff < 0.1 ) { // Scale the full size image. if ( $img->resize( $full_width, $full_height ) ) { $scaled = true; } } if ( ! $scaled ) { $return->error = esc_js( __( 'Error while saving the scaled image. Please reload the page and try again.' ) ); return $return; } } } elseif ( ! empty( $_REQUEST['history'] ) ) { $changes = json_decode( wp_unslash( $_REQUEST['history'] ) ); if ( $changes ) { $img = image_edit_apply_changes( $img, $changes ); } } else { $return->error = esc_js( __( 'Nothing to save, the image has not changed.' ) ); return $return; } $meta = wp_get_attachment_metadata( $post_id ); $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true ); if ( ! is_array( $meta ) ) { $return->error = esc_js( __( 'Image data does not exist. Please re-upload the image.' ) ); return $return; } if ( ! is_array( $backup_sizes ) ) { $backup_sizes = array(); } // Generate new filename. $path = get_attached_file( $post_id ); $basename = pathinfo( $path, PATHINFO_BASENAME ); $dirname = pathinfo( $path, PATHINFO_DIRNAME ); $ext = pathinfo( $path, PATHINFO_EXTENSION ); $filename = pathinfo( $path, PATHINFO_FILENAME ); $suffix = time() . rand( 100, 999 );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.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.