Skip to content

Commit

Permalink
Merge branch 'trunk' into fix/62337-l10n-jit
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Nov 11, 2024
2 parents 0fae240 + 05e274b commit 8712b74
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/wp-admin/includes/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,18 @@ function wp_create_image_subsizes( $file, $attachment_id ) {
*/
if ( $scale_down ) {
$saved = $editor->save( $editor->generate_filename( 'scaled' ) );
} elseif ( $convert ) {
/*
* Generate a new file name for the converted image.
*
* 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();
}
Expand Down
7 changes: 7 additions & 0 deletions src/wp-signup.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?php
/**
* WordPress Signup Page
*
* Handles the user registration and site creation process for multisite installations.
*
* @package WordPress
*/

/** Sets up the WordPress Environment. */
require __DIR__ . '/wp-load.php';
Expand Down
Binary file modified tests/phpunit/data/images/test-image.heic
Binary file not shown.
6 changes: 3 additions & 3 deletions tests/phpunit/tests/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1564,10 +1564,10 @@ public function test_wp_getimagesize_heic() {
}

$expected = array(
50,
50,
1180,
1180,
IMAGETYPE_HEIC,
'width="50" height="50"',
'width="1180" height="1180"',
'mime' => 'image/heic',
);
$result = wp_getimagesize( $file );
Expand Down
99 changes: 99 additions & 0 deletions tests/phpunit/tests/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -6445,6 +6445,105 @@ public function test_wp_img_tag_add_auto_sizes( string $input, string $expected
);
}

/**
* Ensure an HEIC image is converted to a JPEG.
*
* @ticket 62305
* @ticket 62359
*
* @dataProvider data_image_converted_to_other_format_has_correct_filename
*
* @param bool $apply_big_image_size_threshold True if filter needs to apply, otherwise false.
*/
public function test_heic_image_upload_is_converted_to_jpeg( bool $apply_big_image_size_threshold ) {
$temp_dir = get_temp_dir();
$file = $temp_dir . '/test-image.heic';
$scaled_suffix = $apply_big_image_size_threshold ? '-scaled' : '';
copy( DIR_TESTDATA . '/images/test-image.heic', $file );

$editor = wp_get_image_editor( $file );

// Skip if the editor does not support HEIC.
if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/heic' ) ) {
$this->markTestSkipped( 'HEIC is not supported by the selected image editor.' );
}

$attachment_id = self::factory()->attachment->create_object(
array(
'post_mime_type' => 'image/heic',
'file' => $file,
)
);

if ( $apply_big_image_size_threshold ) {
add_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) );
}

$image_meta = wp_generate_attachment_metadata( $attachment_id, $file );

$this->assertStringEndsNotWith( '.heic', $image_meta['file'], 'The file extension is expected to change.' );
$this->assertSame( "test-image{$scaled_suffix}.jpg", basename( $image_meta['file'] ), "The file name is expected to be test-image{$scaled_suffix}.jpg" );
$this->assertSame( 'test-image.heic', $image_meta['original_image'], 'The original image name is expected to be stored in the meta data.' );
$this->assertSame( 'image/jpeg', wp_get_image_mime( $image_meta['file'] ), 'The image mime type is expected to be image/jpeg.' );
}

/**
* Ensure a JPEG is converted to WebP when applied via a filter.
*
* @ticket 62305
* @ticket 62359
*
* @dataProvider data_image_converted_to_other_format_has_correct_filename
*
* @param bool $apply_big_image_size_threshold True if filter needs to apply, otherwise false.
*/
public function test_jpeg_image_converts_to_webp_when_filtered( bool $apply_big_image_size_threshold ) {
$temp_dir = get_temp_dir();
$file = $temp_dir . '/33772.jpg';
$scaled_suffix = $apply_big_image_size_threshold ? '-scaled' : '';
copy( DIR_TESTDATA . '/images/33772.jpg', $file );

$editor = wp_get_image_editor( $file );

// Skip if the editor does not support WebP.
if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/webp' ) ) {
$this->markTestSkipped( 'WebP is not supported by the selected image editor.' );
}

$attachment_id = self::factory()->attachment->create_object(
array(
'post_mime_type' => 'image/jpeg',
'file' => $file,
)
);

if ( $apply_big_image_size_threshold ) {
add_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) );
}

// Generate all sizes as WebP.
add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_webp' ) );

$image_meta = wp_generate_attachment_metadata( $attachment_id, $file );

$this->assertStringEndsNotWith( '.jpg', $image_meta['file'], 'The file extension is expected to change.' );
$this->assertSame( "33772{$scaled_suffix}.webp", basename( $image_meta['file'] ), "The file name is expected to be 33772{$scaled_suffix}.webp." );
$this->assertSame( '33772.jpg', $image_meta['original_image'], 'The original image name is expected to be stored in the meta data.' );
$this->assertSame( 'image/webp', wp_get_image_mime( $image_meta['file'] ), 'The image mime type is expected to be image/webp.' );
}

/**
* Data provider for test_image_converted_to_other_format_has_correct_filename().
*
* @return array[]
*/
public function data_image_converted_to_other_format_has_correct_filename() {
return array(
'do not scale image' => array( false ),
'scale image' => array( true ),
);
}

/**
* Helper method to keep track of the last context returned by the 'wp_get_attachment_image_context' filter.
*
Expand Down

0 comments on commit 8712b74

Please sign in to comment.