Skip to content

Commit

Permalink
When the mime is different from source, add the extension to the suff…
Browse files Browse the repository at this point in the history
…ix to ensure uniqueness
  • Loading branch information
adamsilverstein committed Jul 14, 2022
1 parent 8b9247d commit f41c83f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 26 deletions.
5 changes: 3 additions & 2 deletions src/wp-admin/includes/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,9 +703,10 @@ function _wp_make_additional_mime_types( $new_mime_types, $file, $image_meta, $a
continue;
}

$suffix = _wp_get_image_suffix( $resized, $rotated );
$suffix = _wp_get_image_suffix( $resized, $rotated );
$extension = wp_get_default_extension_for_mime_type( $mime_type );

$saved = $editor->save( $editor->generate_filename( $suffix ) );
$saved = $editor->save( $editor->generate_filename( $suffix, null, $extension ) );

if ( is_wp_error( $saved ) ) {
// TODO: Log errors.
Expand Down
12 changes: 0 additions & 12 deletions src/wp-includes/class-wp-image-editor-gd.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,18 +476,6 @@ protected function _save( $image, $filename = null, $mime_type = null ) {
$filename = $this->generate_filename( null, null, $extension );
}

// Skip already existing files that don't belong to this image attachment.
if ( file_exists( $filename ) ) {
// Get the url of the image file.
$uploads_dir = wp_upload_dir();
$file_url = str_replace( $uploads_dir['basedir'], $uploads_dir()['baseurl'], $this->file );
$attachment_id = attachment_url_to_postid( $file_url );

if ( $attachment_id && ! _wp_image_belongs_to_attachment( wp_basename( $filename ), $attachment_id ) ) {
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
}
}

if ( 'image/gif' === $mime_type ) {
if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) {
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
Expand Down
12 changes: 0 additions & 12 deletions src/wp-includes/class-wp-image-editor-imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -718,18 +718,6 @@ protected function _save( $image, $filename = null, $mime_type = null ) {
$filename = $this->generate_filename( null, null, $extension );
}

// Skip already existing files that don't belong to this image attachment.
if ( file_exists( $filename ) ) {
// Get the url of the image file.
$uploads_dir = wp_upload_dir();
$file_url = str_replace( $uploads_dir['basedir'], $uploads_dir()['baseurl'], $this->file );
$attachment_id = attachment_url_to_postid( $file_url );

if ( $attachment_id && ! _wp_image_belongs_to_attachment( wp_basename( $filename ), $attachment_id ) ) {
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
}
}

try {
// Store initial format.
$orig_format = $this->image->getImageFormat();
Expand Down
5 changes: 5 additions & 0 deletions src/wp-includes/class-wp-image-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@ public function generate_filename( $suffix = null, $dest_path = null, $extension
$suffix = "-{$suffix}";
}

// When the mime type being generated is different from the source, add the extension to the suffix to ensure uniqueness.
if ( ! empty( $extension ) && $extension !== $ext ) {
$suffix .= "-{$ext}";
}

return trailingslashit( $dir ) . "{$name}{$suffix}.{$new_ext}";
}

Expand Down

0 comments on commit f41c83f

Please sign in to comment.