Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/wp-includes/class-wp-image-editor-gd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
4 changes: 4 additions & 0 deletions src/wp-includes/class-wp-image-editor-imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
48 changes: 47 additions & 1 deletion src/wp-includes/class-wp-image-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
*/
Expand All @@ -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 );
}

/**
Expand Down
50 changes: 50 additions & 0 deletions tests/phpunit/tests/image/editorGd.php
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,56 @@ 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() );
}
}

/**
Expand Down
51 changes: 51 additions & 0 deletions tests/phpunit/tests/image/editorImagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}
}
Loading