Media: fix full size image generation for PNG uploads.

Remove a limitation that prevented PNG uploads from generating the full sized image. Fixes a bug where using the image_editor_output_format filter would not generate full sized images as expected. The removed code was present to prevent overly large PNG image output, however this issue was resolved separately in #36477.

Props: adamsilverstein, pixlpirate, flixos90, mukesh27, azaozz.

Fixes #62900.



Built from https://develop.svn.wordpress.org/trunk@59844


git-svn-id: http://core.svn.wordpress.org/trunk@59186 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Adam Silverstein 2025-02-20 06:46:19 +00:00
parent 4d8b360601
commit da8f997ab6
2 changed files with 104 additions and 108 deletions

View File

@ -261,142 +261,138 @@ function wp_create_image_subsizes( $file, $attachment_id ) {
$image_meta['image_meta'] = $exif_meta; $image_meta['image_meta'] = $exif_meta;
} }
// Do not scale (large) PNG images. May result in sub-sizes that have greater file size than the original. See #48736. /**
if ( 'image/png' !== $imagesize['mime'] ) { * Filters the "BIG image" threshold value.
*
* If the original image width or height is above the threshold, it will be scaled down. The threshold is
* used as max width and max height. The scaled down image will be used as the largest available size, including
* the `_wp_attached_file` post meta value.
*
* Returning `false` from the filter callback will disable the scaling.
*
* @since 5.3.0
*
* @param int $threshold The threshold value in pixels. Default 2560.
* @param array $imagesize {
* Indexed array of the image width and height in pixels.
*
* @type int $0 The image width.
* @type int $1 The image height.
* }
* @param string $file Full path to the uploaded image file.
* @param int $attachment_id Attachment post ID.
*/
$threshold = (int) apply_filters( 'big_image_size_threshold', 2560, $imagesize, $file, $attachment_id );
/** /*
* Filters the "BIG image" threshold value. * If the original image's dimensions are over the threshold,
* * scale the image and use it as the "full" size.
* If the original image width or height is above the threshold, it will be scaled down. The threshold is */
* used as max width and max height. The scaled down image will be used as the largest available size, including $scale_down = false;
* the `_wp_attached_file` post meta value. $convert = false;
*
* Returning `false` from the filter callback will disable the scaling.
*
* @since 5.3.0
*
* @param int $threshold The threshold value in pixels. Default 2560.
* @param array $imagesize {
* Indexed array of the image width and height in pixels.
*
* @type int $0 The image width.
* @type int $1 The image height.
* }
* @param string $file Full path to the uploaded image file.
* @param int $attachment_id Attachment post ID.
*/
$threshold = (int) apply_filters( 'big_image_size_threshold', 2560, $imagesize, $file, $attachment_id );
/* if ( $threshold && ( $image_meta['width'] > $threshold || $image_meta['height'] > $threshold ) ) {
* If the original image's dimensions are over the threshold, // The image will be converted if needed on saving.
* scale the image and use it as the "full" size. $scale_down = true;
*/ } else {
$scale_down = false; // The image may need to be converted regardless of its dimensions.
$convert = false; $output_format = wp_get_image_editor_output_format( $file, $imagesize['mime'] );
if ( $threshold && ( $image_meta['width'] > $threshold || $image_meta['height'] > $threshold ) ) { if (
// The image will be converted if needed on saving. is_array( $output_format ) &&
$scale_down = true; array_key_exists( $imagesize['mime'], $output_format ) &&
} else { $output_format[ $imagesize['mime'] ] !== $imagesize['mime']
// The image may need to be converted regardless of its dimensions. ) {
$output_format = wp_get_image_editor_output_format( $file, $imagesize['mime'] ); $convert = true;
}
}
if ( if ( $scale_down || $convert ) {
is_array( $output_format ) && $editor = wp_get_image_editor( $file );
array_key_exists( $imagesize['mime'], $output_format ) &&
$output_format[ $imagesize['mime'] ] !== $imagesize['mime'] if ( is_wp_error( $editor ) ) {
) { // This image cannot be edited.
$convert = true; return $image_meta;
}
} }
if ( $scale_down || $convert ) { if ( $scale_down ) {
$editor = wp_get_image_editor( $file ); // Resize the image. This will also convet it if needed.
$resized = $editor->resize( $threshold, $threshold );
} elseif ( $convert ) {
// The image will be converted (if possible) when saved.
$resized = true;
}
if ( is_wp_error( $editor ) ) { $rotated = null;
// This image cannot be edited.
return $image_meta;
}
// If there is EXIF data, rotate according to EXIF Orientation.
if ( ! is_wp_error( $resized ) && is_array( $exif_meta ) ) {
$resized = $editor->maybe_exif_rotate();
$rotated = $resized; // bool true or WP_Error
}
if ( ! is_wp_error( $resized ) ) {
/*
* Append "-scaled" to the image file name. It will look like "my_image-scaled.jpg".
* This doesn't affect the sub-sizes names as they are generated from the original image (for best quality).
*/
if ( $scale_down ) { if ( $scale_down ) {
// Resize the image. This will also convet it if needed. $saved = $editor->save( $editor->generate_filename( 'scaled' ) );
$resized = $editor->resize( $threshold, $threshold );
} elseif ( $convert ) { } elseif ( $convert ) {
// The image will be converted (if possible) when saved.
$resized = true;
}
$rotated = null;
// If there is EXIF data, rotate according to EXIF Orientation.
if ( ! is_wp_error( $resized ) && is_array( $exif_meta ) ) {
$resized = $editor->maybe_exif_rotate();
$rotated = $resized; // bool true or WP_Error
}
if ( ! is_wp_error( $resized ) ) {
/* /*
* Append "-scaled" to the image file name. It will look like "my_image-scaled.jpg". * Generate a new file name for the converted image.
* This doesn't affect the sub-sizes names as they are generated from the original image (for best quality). *
* As the image file name will be unique due to the changed file extension,
* it does not need a suffix to be unique. However, the generate_filename method
* does not allow for an empty suffix, so the "-converted" suffix is required to
* be added and subsequently removed.
*/ */
if ( $scale_down ) { $converted_file_name = $editor->generate_filename( 'converted' );
$saved = $editor->save( $editor->generate_filename( 'scaled' ) ); $converted_file_name = preg_replace( '/(-converted\.)([a-z0-9]+)$/i', '.$2', $converted_file_name );
} elseif ( $convert ) { $saved = $editor->save( $converted_file_name );
/* } else {
* Generate a new file name for the converted image. $saved = $editor->save();
* }
* As the image file name will be unique due to the changed file extension,
* it does not need a suffix to be unique. However, the generate_filename method
* does not allow for an empty suffix, so the "-converted" suffix is required to
* be added and subsequently removed.
*/
$converted_file_name = $editor->generate_filename( 'converted' );
$converted_file_name = preg_replace( '/(-converted\.)([a-z0-9]+)$/i', '.$2', $converted_file_name );
$saved = $editor->save( $converted_file_name );
} else {
$saved = $editor->save();
}
if ( ! is_wp_error( $saved ) ) { if ( ! is_wp_error( $saved ) ) {
$image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id ); $image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id );
// If the image was rotated update the stored EXIF data. // If the image was rotated update the stored EXIF data.
if ( true === $rotated && ! empty( $image_meta['image_meta']['orientation'] ) ) { if ( true === $rotated && ! empty( $image_meta['image_meta']['orientation'] ) ) {
$image_meta['image_meta']['orientation'] = 1; $image_meta['image_meta']['orientation'] = 1;
}
} else {
// TODO: Log errors.
} }
} else { } else {
// TODO: Log errors. // TODO: Log errors.
} }
} elseif ( ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) { } else {
// TODO: Log errors.
}
} elseif ( ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) {
// Rotate the whole original image if there is EXIF data and "orientation" is not 1. // Rotate the whole original image if there is EXIF data and "orientation" is not 1.
$editor = wp_get_image_editor( $file ); $editor = wp_get_image_editor( $file );
if ( is_wp_error( $editor ) ) { if ( is_wp_error( $editor ) ) {
// This image cannot be edited. // This image cannot be edited.
return $image_meta; return $image_meta;
} }
// Rotate the image. // Rotate the image.
$rotated = $editor->maybe_exif_rotate(); $rotated = $editor->maybe_exif_rotate();
if ( true === $rotated ) { if ( true === $rotated ) {
// Append `-rotated` to the image file name. // Append `-rotated` to the image file name.
$saved = $editor->save( $editor->generate_filename( 'rotated' ) ); $saved = $editor->save( $editor->generate_filename( 'rotated' ) );
if ( ! is_wp_error( $saved ) ) { if ( ! is_wp_error( $saved ) ) {
$image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id ); $image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id );
// Update the stored EXIF data. // Update the stored EXIF data.
if ( ! empty( $image_meta['image_meta']['orientation'] ) ) { if ( ! empty( $image_meta['image_meta']['orientation'] ) ) {
$image_meta['image_meta']['orientation'] = 1; $image_meta['image_meta']['orientation'] = 1;
}
} else {
// TODO: Log errors.
} }
} else {
// TODO: Log errors.
} }
} }
} }

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.8-alpha-59843'; $wp_version = '6.8-alpha-59844';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.