Skip to content

Commit f41c83f

Browse files
When the mime is different from source, add the extension to the suffix to ensure uniqueness
1 parent 8b9247d commit f41c83f

File tree

4 files changed

+8
-26
lines changed

4 files changed

+8
-26
lines changed

‎src/wp-admin/includes/image.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,9 +703,10 @@ function _wp_make_additional_mime_types( $new_mime_types, $file, $image_meta, $a
703703
continue;
704704
}
705705

706-
$suffix = _wp_get_image_suffix( $resized, $rotated );
706+
$suffix = _wp_get_image_suffix( $resized, $rotated );
707+
$extension = wp_get_default_extension_for_mime_type( $mime_type );
707708

708-
$saved = $editor->save( $editor->generate_filename( $suffix ) );
709+
$saved = $editor->save( $editor->generate_filename( $suffix, null, $extension ) );
709710

710711
if ( is_wp_error( $saved ) ) {
711712
// TODO: Log errors.

‎src/wp-includes/class-wp-image-editor-gd.php‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -476,18 +476,6 @@ protected function _save( $image, $filename = null, $mime_type = null ) {
476476
$filename = $this->generate_filename( null, null, $extension );
477477
}
478478

479-
// Skip already existing files that don't belong to this image attachment.
480-
if ( file_exists( $filename ) ) {
481-
// Get the url of the image file.
482-
$uploads_dir = wp_upload_dir();
483-
$file_url = str_replace( $uploads_dir['basedir'], $uploads_dir()['baseurl'], $this->file );
484-
$attachment_id = attachment_url_to_postid( $file_url );
485-
486-
if ( $attachment_id && ! _wp_image_belongs_to_attachment( wp_basename( $filename ), $attachment_id ) ) {
487-
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
488-
}
489-
}
490-
491479
if ( 'image/gif' === $mime_type ) {
492480
if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) {
493481
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );

‎src/wp-includes/class-wp-image-editor-imagick.php‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -718,18 +718,6 @@ protected function _save( $image, $filename = null, $mime_type = null ) {
718718
$filename = $this->generate_filename( null, null, $extension );
719719
}
720720

721-
// Skip already existing files that don't belong to this image attachment.
722-
if ( file_exists( $filename ) ) {
723-
// Get the url of the image file.
724-
$uploads_dir = wp_upload_dir();
725-
$file_url = str_replace( $uploads_dir['basedir'], $uploads_dir()['baseurl'], $this->file );
726-
$attachment_id = attachment_url_to_postid( $file_url );
727-
728-
if ( $attachment_id && ! _wp_image_belongs_to_attachment( wp_basename( $filename ), $attachment_id ) ) {
729-
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
730-
}
731-
}
732-
733721
try {
734722
// Store initial format.
735723
$orig_format = $this->image->getImageFormat();

‎src/wp-includes/class-wp-image-editor.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,11 @@ public function generate_filename( $suffix = null, $dest_path = null, $extension
471471
$suffix = "-{$suffix}";
472472
}
473473

474+
// When the mime type being generated is different from the source, add the extension to the suffix to ensure uniqueness.
475+
if ( ! empty( $extension ) && $extension !== $ext ) {
476+
$suffix .= "-{$ext}";
477+
}
478+
474479
return trailingslashit( $dir ) . "{$name}{$suffix}.{$new_ext}";
475480
}
476481

0 commit comments

Comments
 (0)