wppaste
WordPress

wp_save_image( int $post_id ): stdClass

Since
2.9.0
Source
wp-admin/includes/image-edit.php:904
Saves image to post, along with enqueued changes in $_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

Reads or writes the filesystem via file_exists().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
65–259

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

Plugin surface
1 hook

Third-party callbacks on 'image_edit_thumbnails_separately' 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

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()called directly
  • serializeserialisationjson_decode()called directly
  • filesystemfilesystem accessfile_exists()called directly
  • cacheobject cachewp_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.

WhenInstructionsCalls it makes
!is_wp_error() && empty($value) && $value65wp_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–203wp_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–206wp_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–217wp_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–217wp_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–220wp_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–220wp_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–235wp_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–234wp_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–238wp_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–231wp_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–237wp_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–233wp_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–243wp_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–246wp_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–248wp_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–247wp_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–251wp_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–250wp_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–256wp_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–259wp_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

PHPCompiledExecutedBranchesNotes
8.6-dev54565–259701 fewer instruction than PHP 8.5
8.554665–25970
8.454665–2597012 fewer instructions than PHP 8.3
8.355865–26470
8.255865–26470
8.155865–264703 fewer instructions than PHP 7.4
7.456165–26470

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:

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

Show all 25

Used by · 1

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.

  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.

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.