From 08c5b35a535a1e6f346425f44fb9bdb4776b2d7c Mon Sep 17 00:00:00 2001 From: Harmit Mendapara Date: Sat, 21 Nov 2026 11:37:18 +0530 Subject: [PATCH 1/2] Media: Save sub-sizes with non-center crop positions to separate files. Image sub-sizes with the same dimensions but different crop positions, for example `add_image_size( 'top', 400, 400, array( 'center', 'top' ) )` and `add_image_size( 'bottom', 400, 400, array( 'center', 'bottom' ) )`, were saved to the same file name and overwrote each other. This appends a short hash of the crop position to the file name suffix when a sub-size is cropped from a position other than center. Center crops, including `crop => true`, keep their existing file names. See #40370. Co-Authored-By: Claude Opus 5.5 --- src/wp-includes/class-wp-image-editor-gd.php | 4 ++ .../class-wp-image-editor-imagick.php | 4 ++ src/wp-includes/class-wp-image-editor.php | 48 ++++++++++++++++- tests/phpunit/tests/image/editorGd.php | 51 +++++++++++++++++++ tests/phpunit/tests/image/editorImagick.php | 51 +++++++++++++++++++ 5 files changed, 157 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-image-editor-gd.php b/src/wp-includes/class-wp-image-editor-gd.php index 10eed8178b938..ea58fe42b5a3f 100644 --- a/src/wp-includes/class-wp-image-editor-gd.php +++ b/src/wp-includes/class-wp-image-editor-gd.php @@ -321,8 +321,12 @@ public function make_subsize( $size_data ) { if ( is_wp_error( $resized ) ) { $saved = $resized; } else { + $this->crop_position = $size_data['crop']; + $saved = $this->_save( $resized ); + $this->crop_position = false; + if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0. imagedestroy( $resized ); } diff --git a/src/wp-includes/class-wp-image-editor-imagick.php b/src/wp-includes/class-wp-image-editor-imagick.php index 0be31905eae81..f86785bff173d 100644 --- a/src/wp-includes/class-wp-image-editor-imagick.php +++ b/src/wp-includes/class-wp-image-editor-imagick.php @@ -888,8 +888,12 @@ public function make_subsize( $size_data ) { if ( is_wp_error( $resized ) ) { $saved = $resized; } else { + $this->crop_position = $size_data['crop']; + $saved = $this->_save( $this->image ); + $this->crop_position = false; + $this->image->clear(); $this->image->destroy(); $this->image = null; diff --git a/src/wp-includes/class-wp-image-editor.php b/src/wp-includes/class-wp-image-editor.php index df07c4229b623..41c42a39e35dc 100644 --- a/src/wp-includes/class-wp-image-editor.php +++ b/src/wp-includes/class-wp-image-editor.php @@ -20,6 +20,14 @@ abstract class WP_Image_Editor { protected $default_mime_type = 'image/jpeg'; protected $quality = false; + /** + * Crop position of the sub-size being created, used to build a unique file name suffix. + * + * @since 7.3.0 + * @var bool|array + */ + protected $crop_position = false; + // Deprecated since 5.8.1. See get_default_quality() below. protected $default_quality = 82; @@ -464,6 +472,7 @@ public function generate_filename( $suffix = null, $dest_path = null, $extension * Builds and returns proper suffix for file based on height and width. * * @since 3.5.0 + * @since 7.3.0 A crop position hash is appended for sub-sizes cropped from a non-center position. * * @return string|false suffix */ @@ -472,7 +481,44 @@ public function get_suffix() { return false; } - return "{$this->size['width']}x{$this->size['height']}"; + $suffix = "{$this->size['width']}x{$this->size['height']}"; + + $crop_hash = $this->get_crop_hash(); + + if ( $crop_hash ) { + $suffix .= "-{$crop_hash}"; + } + + return $suffix; + } + + /** + * Gets a short hash identifying the crop position of the sub-size being created. + * + * Sub-sizes with the same dimensions but different crop positions would otherwise + * be saved to the same file and overwrite each other. Center cropping is the default, + * so no hash is returned for it and existing file names remain unchanged. + * + * @since 7.3.0 + * + * @return string|false 8-character hash, or false if the crop position is the default. + */ + protected function get_crop_hash() { + if ( ! is_array( $this->crop_position ) || count( $this->crop_position ) !== 2 ) { + return false; + } + + list( $x, $y ) = array_values( $this->crop_position ); + + // Match the crop positions supported by image_resize_dimensions(), which treats anything else as center. + $x = in_array( $x, array( 'left', 'right' ), true ) ? $x : 'center'; + $y = in_array( $y, array( 'top', 'bottom' ), true ) ? $y : 'center'; + + if ( 'center' === $x && 'center' === $y ) { + return false; + } + + return substr( md5( "{$x}-{$y}" ), 0, 8 ); } /** diff --git a/tests/phpunit/tests/image/editorGd.php b/tests/phpunit/tests/image/editorGd.php index 705037836c5bc..3de0e5d44bee5 100644 --- a/tests/phpunit/tests/image/editorGd.php +++ b/tests/phpunit/tests/image/editorGd.php @@ -1200,6 +1200,57 @@ private function save_to_stream( $protocol, $wrapper_class ) { return $saved; } + /** + * Tests that sub-sizes with the same dimensions but different crop positions are saved to different files. + * + * @ticket 40370 + */ + public function test_multi_resize_with_same_dimensions_and_different_crop_positions() { + $file = DIR_TESTDATA . '/images/waffles.jpg'; + + $image_editor = new WP_Image_Editor_GD( $file ); + $image_editor->load(); + + $sizes = array( + 'cropped' => array( + 'width' => 50, + 'height' => 50, + 'crop' => true, + ), + 'center-center' => array( + 'width' => 50, + 'height' => 50, + 'crop' => array( 'center', 'center' ), + ), + 'center-top' => array( + 'width' => 50, + 'height' => 50, + 'crop' => array( 'center', 'top' ), + ), + 'center-bottom' => array( + 'width' => 50, + 'height' => 50, + 'crop' => array( 'center', 'bottom' ), + ), + ); + + $resized = $image_editor->multi_resize( $sizes ); + + $this->assertSame( 'waffles-50x50.jpg', $resized['cropped']['file'], 'Center cropped sub-sizes should keep the existing file name.' ); + $this->assertSame( 'waffles-50x50.jpg', $resized['center-center']['file'], 'An explicit center crop position should keep the existing file name.' ); + $this->assertMatchesRegularExpression( '/^waffles-50x50-[0-9a-f]{8}\.jpg$/', $resized['center-top']['file'], 'Non-center crop positions should have a hash in the file name.' ); + $this->assertMatchesRegularExpression( '/^waffles-50x50-[0-9a-f]{8}\.jpg$/', $resized['center-bottom']['file'], 'Non-center crop positions should have a hash in the file name.' ); + $this->assertNotSame( $resized['center-top']['file'], $resized['center-bottom']['file'], 'Different crop positions should be saved to different files.' ); + + foreach ( $resized as $size_data ) { + $this->assertFileExists( dirname( $file ) . '/' . $size_data['file'] ); + } + + // The editor should not carry the crop position over to images saved later. + $image_editor->resize( 25, 25, array( 'center', 'top' ) ); + $this->assertSame( '25x25', $image_editor->get_suffix() ); + } + } /** diff --git a/tests/phpunit/tests/image/editorImagick.php b/tests/phpunit/tests/image/editorImagick.php index 9fc3adb6f06fb..fc1a86f0609ee 100644 --- a/tests/phpunit/tests/image/editorImagick.php +++ b/tests/phpunit/tests/image/editorImagick.php @@ -951,4 +951,55 @@ public static function data_alpha_transparency_is_preserved_after_resize() { ), ); } + + /** + * Tests that sub-sizes with the same dimensions but different crop positions are saved to different files. + * + * @ticket 40370 + */ + public function test_multi_resize_with_same_dimensions_and_different_crop_positions() { + $file = DIR_TESTDATA . '/images/waffles.jpg'; + + $image_editor = new WP_Image_Editor_Imagick( $file ); + $image_editor->load(); + + $sizes = array( + 'cropped' => array( + 'width' => 50, + 'height' => 50, + 'crop' => true, + ), + 'center-center' => array( + 'width' => 50, + 'height' => 50, + 'crop' => array( 'center', 'center' ), + ), + 'center-top' => array( + 'width' => 50, + 'height' => 50, + 'crop' => array( 'center', 'top' ), + ), + 'center-bottom' => array( + 'width' => 50, + 'height' => 50, + 'crop' => array( 'center', 'bottom' ), + ), + ); + + $resized = $image_editor->multi_resize( $sizes ); + + $this->assertSame( 'waffles-50x50.jpg', $resized['cropped']['file'], 'Center cropped sub-sizes should keep the existing file name.' ); + $this->assertSame( 'waffles-50x50.jpg', $resized['center-center']['file'], 'An explicit center crop position should keep the existing file name.' ); + $this->assertMatchesRegularExpression( '/^waffles-50x50-[0-9a-f]{8}\.jpg$/', $resized['center-top']['file'], 'Non-center crop positions should have a hash in the file name.' ); + $this->assertMatchesRegularExpression( '/^waffles-50x50-[0-9a-f]{8}\.jpg$/', $resized['center-bottom']['file'], 'Non-center crop positions should have a hash in the file name.' ); + $this->assertNotSame( $resized['center-top']['file'], $resized['center-bottom']['file'], 'Different crop positions should be saved to different files.' ); + + foreach ( $resized as $size_data ) { + $this->assertFileExists( dirname( $file ) . '/' . $size_data['file'] ); + } + + // The editor should not carry the crop position over to images saved later. + $image_editor->resize( 25, 25, array( 'center', 'top' ) ); + $this->assertSame( '25x25', $image_editor->get_suffix() ); + } } From 07be54b2341437caf0e0e1acf9fe22ca83008a2e Mon Sep 17 00:00:00 2001 From: Apple Date: Sat, 21 Nov 2026 12:11:50 +0530 Subject: [PATCH 2/2] Tests: Remove blank line before class closing brace in editorGd.php. See #40370. Co-Authored-By: Claude Opus 5.5 --- tests/phpunit/tests/image/editorGd.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/phpunit/tests/image/editorGd.php b/tests/phpunit/tests/image/editorGd.php index 3de0e5d44bee5..9fdc06409e322 100644 --- a/tests/phpunit/tests/image/editorGd.php +++ b/tests/phpunit/tests/image/editorGd.php @@ -1250,7 +1250,6 @@ public function test_multi_resize_with_same_dimensions_and_different_crop_positi $image_editor->resize( 25, 25, array( 'center', 'top' ) ); $this->assertSame( '25x25', $image_editor->get_suffix() ); } - } /**