Index: src/wp-includes/class-wp-image-editor.php
===================================================================
--- src/wp-includes/class-wp-image-editor.php	(revision 51447)
+++ src/wp-includes/class-wp-image-editor.php	(working copy)
@@ -591,13 +591,7 @@
 	 * @return string|false
 	 */
 	protected static function get_extension( $mime_type = null ) {
-		$extensions = explode( '|', array_search( $mime_type, wp_get_mime_types(), true ) );
-
-		if ( empty( $extensions[0] ) ) {
-			return false;
-		}
-
-		return $extensions[0];
+		return wp_default_extension_for_mine_type( $mime_type );
 	}
 }
 
Index: src/wp-includes/functions.php
===================================================================
--- src/wp-includes/functions.php	(revision 51447)
+++ src/wp-includes/functions.php	(working copy)
@@ -2594,6 +2594,9 @@
 				}
 			}
 		}
+
+		// If a different file type might be produced for an image, check filename uniqueness for that format.
+		$filename = _wp_check_alternate_output_format_uniqueness( $filename, $ext, $dir );
 	}
 
 	/**
@@ -2644,6 +2647,68 @@
 }
 
 /**
+ * Helper function for wp_unique_filename to check potential alternate output formats for images.
+ *
+ * @since 5.8.1
+ *
+ * @param string $filename
+ * @param string $ext
+ * @param string $dir
+ *
+ * @return string
+ */
+function _wp_check_alternate_output_format_uniqueness( $filename, $ext, $dir ) {
+	static $checking_alternates;
+
+	if ( empty( $checking_alternates ) ) {
+		$checking_alternates = true;
+		$file_type           = wp_check_filetype_and_ext( trailingslashit( $dir ) . $filename, $filename );
+		$mime_type           = $file_type['type'];
+
+		if ( ! empty( $mime_type ) && 0 === strpos( $mime_type, 'image/' ) ) {
+			$output_formats = apply_filters( 'image_editor_output_format', array(), trailingslashit( $dir ) . $filename, $mime_type );
+
+			if (
+				! empty( $output_formats ) &&
+				is_array( $output_formats ) &&
+				( ! empty( $output_formats[ $mime_type ] ) || in_array( $mime_type, $output_formats ) )
+			) {
+				$alt_mime_types = array();
+
+				foreach ( $output_formats as $source => $target ) {
+					if ( $source === $mime_type && $target === $mime_type ) {
+						continue;
+					} elseif ( $source === $mime_type ) {
+						$alt_mime_types[] = $target;
+					} elseif ( $target === $mime_type ) {
+						$alt_mime_types[] = $source;
+					}
+				}
+
+				$alt_mime_types = array_unique( $alt_mime_types );
+
+				foreach ( $alt_mime_types as $alt_mime_type ) {
+					$alt_ext = wp_default_extension_for_mine_type( $alt_mime_type );
+
+					if ( ! empty( $alt_ext ) && ".{$alt_ext}" !== $ext ) {
+						$alt_filename  = wp_basename( $filename, $ext ) . ".{$alt_ext}";
+						$alt_filename2 = wp_unique_filename( $dir, $alt_filename );
+
+						// If a potential clash was found for alternate format, use its unique filename.
+						if ( $alt_filename2 !== $alt_filename ) {
+							$filename = wp_basename( $alt_filename2, ".{$alt_ext}" ) . $ext;
+						}
+					}
+				}
+			}
+		}
+		$checking_alternates = false;
+	}
+
+	return $filename;
+}
+
+/**
  * Create a file in the upload folder with given content.
  *
  * If there is an error, then the key 'error' will exist with the error message.
@@ -3041,6 +3106,26 @@
 }
 
 /**
+ * Returns first matched extension from Mime-type,
+ * as mapped from wp_get_mime_types()
+ *
+ * @since 5.8.1
+ *
+ * @param string $mime_type
+ *
+ * @return string|false
+ */
+function wp_default_extension_for_mine_type( $mime_type = null ) {
+	$extensions = explode( '|', array_search( $mime_type, wp_get_mime_types(), true ) );
+
+	if ( empty( $extensions[0] ) ) {
+		return false;
+	}
+
+	return $extensions[0];
+}
+
+/**
  * Returns the real mime type of an image file.
  *
  * This depends on exif_imagetype() or getimagesize() to determine real mime types.
