Definition:
function wp_save_image($post_id) {}
Parameters
- $post_id
Defined filters
- admin_memory_limit
apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT )
Source code
function wp_save_image($post_id) { $return = new stdClass; $success = $delete = $scaled = $nocrop = false; $post = get_post($post_id); @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); $img = load_image_to_edit($post_id, $post->post_mime_type); if ( !is_resource($img) ) { $return->error = esc_js( __('Unable to create new image.') ); return $return; } $fwidth = !empty($_REQUEST['fwidth']) ? intval($_REQUEST['fwidth']) : 0; $fheight = !empty($_REQUEST['fheight']) ? intval($_REQUEST['fheight']) : 0; $target = !empty($_REQUEST['target']) ? preg_replace('/[^a-z0-9_-]+/i', '', $_REQUEST['target']) : ''; $scale = !empty($_REQUEST['do']) && 'scale' == $_REQUEST['do']; if ( $scale && $fwidth > 0 && $fheight > 0 ) { $sX = imagesx($img); $sY = imagesy($img); // check if it has roughly the same w / h ratio $diff = round($sX / $sY, 2) - round($fwidth / $fheight, 2); if ( -0.1 < $diff && $diff < 0.1 ) { // scale the full size image $dst = wp_imagecreatetruecolor($fwidth, $fheight); if ( imagecopyresampled( $dst, $img, 0, 0, 0, 0, $fwidth, $fheight, $sX, $sY ) ) { imagedestroy($img); $img = $dst; $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( stripslashes($_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); $path_parts = pathinfo( $path ); $filename = $path_parts['filename']; $suffix = time() . rand(100, 999); if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE && isset($backup_sizes['full-orig']) && $backup_sizes['full-orig']['file'] != $path_parts['basename'] ) { if ( 'thumbnail' == $target ) $new_path = "{$path_parts['dirname']}/{$filename}-temp.{$path_parts['extension']}";
4077
I don’t understand the purpose of the random number it generates. See Line 105 and the following if/else.
Why doesn’t WP just save over the existing file?
Is there a way for me to force it to?
This is an attempt to ensure that the uploaded file does not exist. If it exists a suffix is added. You should be able to override this behavior by defining IMAGE_EDIT_OVERWRITE = true
I tried this:
define(‘IMAGE_EDIT_OVERWRITE’, FALSE);
as the last line in wp-config. It didn’t change anything. 😦
You need to define it as TRUE not FALSE
I did. No difference. 😦
Having a weird issue where the cropped image is being created as a separate file, dumped into the uploads folder, and the post it’s attached to doesn’t get updated with the new image path. The post continues using the original size version of the file. If that sounds like a common isssue, I’d love to know the fix.
I’m using this guy’s code: http://wordpress.org/support/topic/hack-crop-custom-thumbnail-sizes?replies=11#post-2041694 for custom thumbnail sizes.
I’m not quite sure if this is a core issue. That sounds more like a bug in the theme or plugin you’re using, but it’s hard to tell without seeing/debugging this. Did you have a look at the core trac and tried to search for this issue?
oops, I meant to type true just now. ha.
but i also tried false just in case i misunderstood it.