Add 'IMAGE_EDIT_OVERWRITE' constant to control edited image save or replace, most useful for setups that have dynamic image resizing, see #10528

git-svn-id: http://svn.automattic.com/wordpress/trunk@12155 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
azaozz 2009-11-07 11:20:09 +00:00
parent 0893cbd9a2
commit 73a0105b1a

View File

@ -24,30 +24,6 @@ function wp_image_editor($post_id, $msg = false) {
$can_restore = !empty($backup_sizes) && isset($backup_sizes['full-orig']) $can_restore = !empty($backup_sizes) && isset($backup_sizes['full-orig'])
&& $backup_sizes['full-orig']['file'] != basename($meta['file']); && $backup_sizes['full-orig']['file'] != basename($meta['file']);
// temp convert backup sizes
if ( $sub_sizes ) {
$update = false;
foreach ( $meta['sizes'] as $name => $val ) {
if ( strpos($name, 'backup-') === 0 ) {
$m = array();
preg_match('/backup-([0-9]+)-(.*)/', $name, $m);
if ( !isset($backup_sizes["{$m[2]}-orig"]) )
$n = "{$m[2]}-orig";
else
$n = "{$m[2]}-{$m[1]}";
$backup_sizes[$n] = $val;
unset($meta['sizes'][$name]);
$update = true;
}
}
if ( $update ) {
wp_update_attachment_metadata( $post_id, $meta );
update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes);
}
}
// end temp
if ( $msg ) { if ( $msg ) {
if ( isset($msg->error) ) if ( isset($msg->error) )
$note = "<div class='error'><p>$msg->error</p></div>"; $note = "<div class='error'><p>$msg->error</p></div>";
@ -451,8 +427,17 @@ function wp_restore_image($post_id) {
if ( isset($backup_sizes["$default_size-orig"]) ) { if ( isset($backup_sizes["$default_size-orig"]) ) {
$data = $backup_sizes["$default_size-orig"]; $data = $backup_sizes["$default_size-orig"];
if ( 'full' == $default_size ) { if ( 'full' == $default_size ) {
if ( $parts['basename'] != $data['file'] ) if ( $parts['basename'] != $data['file'] ) {
if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE ) {
// delete only if it's edited image
if ( preg_match('/-e[0-9]{13}\./', $parts['basename']) ) {
$delpath = apply_filters('wp_delete_file', $meta['file']);
@unlink($delpath);
}
} else {
$backup_sizes["full-$suffix"] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $parts['basename']); $backup_sizes["full-$suffix"] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $parts['basename']);
}
}
$meta['file'] = path_join($parts['dirname'], $data['file']); $meta['file'] = path_join($parts['dirname'], $data['file']);
$meta['width'] = $data['width']; $meta['width'] = $data['width'];
@ -462,8 +447,17 @@ function wp_restore_image($post_id) {
$meta['hwstring_small'] = "height='$uheight' width='$uwidth'"; $meta['hwstring_small'] = "height='$uheight' width='$uwidth'";
$restored = update_attached_file($post_id, $meta['file']); $restored = update_attached_file($post_id, $meta['file']);
} else { } else {
if ( isset($meta['sizes'][$default_size]) && $meta['sizes'][$default_size]['file'] != $data['file'] ) if ( isset($meta['sizes'][$default_size]) && $meta['sizes'][$default_size]['file'] != $data['file'] ) {
if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE ) {
// delete only if it's edited image
if ( preg_match('/-e[0-9]{13}-/', $meta['sizes'][$default_size]['file']) ) {
$delpath = apply_filters( 'wp_delete_file', path_join($parts['dirname'], $meta['sizes'][$default_size]['file']) );
@unlink($delpath);
}
} else {
$backup_sizes["$default_size-{$suffix}"] = $meta['sizes'][$default_size]; $backup_sizes["$default_size-{$suffix}"] = $meta['sizes'][$default_size];
}
}
$meta['sizes'][$default_size] = $data; $meta['sizes'][$default_size] = $data;
} }
@ -547,6 +541,14 @@ function wp_save_image($post_id) {
$filename = $path_parts['filename']; $filename = $path_parts['filename'];
$suffix = time() . rand(100, 999); $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']}";
else
$new_path = $path;
} else {
while( true ) { while( true ) {
$filename = preg_replace( '/-e([0-9]+)$/', '', $filename ); $filename = preg_replace( '/-e([0-9]+)$/', '', $filename );
$filename .= "-e{$suffix}"; $filename .= "-e{$suffix}";
@ -557,6 +559,7 @@ function wp_save_image($post_id) {
else else
break; break;
} }
}
// save the full-size file, also needed to create sub-sizes // save the full-size file, also needed to create sub-sizes
if ( !wp_save_image_file($new_path, $img, $post->post_mime_type, $post_id) ) { if ( !wp_save_image_file($new_path, $img, $post->post_mime_type, $post_id) ) {
@ -567,7 +570,7 @@ function wp_save_image($post_id) {
if ( 'nothumb' == $target || 'all' == $target || 'full' == $target || $scaled ) { if ( 'nothumb' == $target || 'all' == $target || 'full' == $target || $scaled ) {
$tag = false; $tag = false;
if ( isset($backup_sizes['full-orig']) ) { if ( isset($backup_sizes['full-orig']) ) {
if ( $backup_sizes['full-orig']['file'] != $path_parts['basename'] ) if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) && $backup_sizes['full-orig']['file'] != $path_parts['basename'] )
$tag = "full-$suffix"; $tag = "full-$suffix";
} else { } else {
$tag = 'full-orig'; $tag = 'full-orig';
@ -602,7 +605,7 @@ function wp_save_image($post_id) {
$tag = false; $tag = false;
if ( isset($meta['sizes'][$size]) ) { if ( isset($meta['sizes'][$size]) ) {
if ( isset($backup_sizes["$size-orig"]) ) { if ( isset($backup_sizes["$size-orig"]) ) {
if ( $backup_sizes["$size-orig"]['file'] != $meta['sizes'][$size]['file'] ) if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) && $backup_sizes["$size-orig"]['file'] != $meta['sizes'][$size]['file'] )
$tag = "$size-$suffix"; $tag = "$size-$suffix";
} else { } else {
$tag = "$size-orig"; $tag = "$size-orig";