Make WordPress Core

Changeset 59381


Ignore:
Timestamp:
11/11/2024 03:44:07 PM (4 weeks ago)
Author:
davidbaumwald
Message:

Media: Remove dimension suffix from full size converted HEIC images.

Removes the dimension suffix, eg -1000x1000 from the file name of full size images automatically converted from HEIC to JPEGs by WordPress. Introduces unit tests for the default conversion of images and customized conversion settings via the image_editor_output_format filter.

Follow up to [58849], [58942], [59317], [59346], [59366].

Reviewed by davidbaumwald.
Merges [59379] and [59380] to the 6.7 branch.

Props mukesh27, peterwilsoncc, azaozz, apermo, flixos90, ironprogrammer.
Fixes #62359.
See #53645, #62305.

Location:
branches/6.7
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/6.7

  • branches/6.7/src/wp-admin/includes/image.php

    r59367 r59381  
    342342                if ( $scale_down ) {
    343343                    $saved = $editor->save( $editor->generate_filename( 'scaled' ) );
     344                } elseif ( $convert ) {
     345                    /*
     346                     * Generate a new file name for the converted image.
     347                     *
     348                     * As the image file name will be unique due to the changed file extension,
     349                     * it does not need a suffix to be unique. However, the generate_filename method
     350                     * does not allow for an empty suffix, so the "-converted" suffix is required to
     351                     * be added and subsequently removed.
     352                     */
     353                    $converted_file_name = $editor->generate_filename( 'converted' );
     354                    $converted_file_name = preg_replace( '/(-converted\.)([a-z0-9]+)$/i', '.$2', $converted_file_name );
     355                    $saved               = $editor->save( $converted_file_name );
    344356                } else {
    345357                    $saved = $editor->save();
  • branches/6.7/tests/phpunit/tests/functions.php

    r58849 r59381  
    15651565
    15661566        $expected = array(
    1567             50,
    1568             50,
     1567            1180,
     1568            1180,
    15691569            IMAGETYPE_HEIC,
    1570             'width="50" height="50"',
     1570            'width="1180" height="1180"',
    15711571            'mime' => 'image/heic',
    15721572        );
  • branches/6.7/tests/phpunit/tests/media.php

    r59247 r59381  
    64476447
    64486448    /**
     6449     * Ensure an HEIC image is converted to a JPEG.
     6450     *
     6451     * @ticket 62305
     6452     * @ticket 62359
     6453     *
     6454     * @dataProvider data_image_converted_to_other_format_has_correct_filename
     6455     *
     6456     * @param bool $apply_big_image_size_threshold True if filter needs to apply, otherwise false.
     6457     */
     6458    public function test_heic_image_upload_is_converted_to_jpeg( bool $apply_big_image_size_threshold ) {
     6459        $temp_dir      = get_temp_dir();
     6460        $file          = $temp_dir . '/test-image.heic';
     6461        $scaled_suffix = $apply_big_image_size_threshold ? '-scaled' : '';
     6462        copy( DIR_TESTDATA . '/images/test-image.heic', $file );
     6463
     6464        $editor = wp_get_image_editor( $file );
     6465
     6466        // Skip if the editor does not support HEIC.
     6467        if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/heic' ) ) {
     6468            $this->markTestSkipped( 'HEIC is not supported by the selected image editor.' );
     6469        }
     6470
     6471        $attachment_id = self::factory()->attachment->create_object(
     6472            array(
     6473                'post_mime_type' => 'image/heic',
     6474                'file'           => $file,
     6475            )
     6476        );
     6477
     6478        if ( $apply_big_image_size_threshold ) {
     6479            add_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) );
     6480        }
     6481
     6482        $image_meta = wp_generate_attachment_metadata( $attachment_id, $file );
     6483
     6484        $this->assertStringEndsNotWith( '.heic', $image_meta['file'], 'The file extension is expected to change.' );
     6485        $this->assertSame( "test-image{$scaled_suffix}.jpg", basename( $image_meta['file'] ), "The file name is expected to be test-image{$scaled_suffix}.jpg" );
     6486        $this->assertSame( 'test-image.heic', $image_meta['original_image'], 'The original image name is expected to be stored in the meta data.' );
     6487        $this->assertSame( 'image/jpeg', wp_get_image_mime( $image_meta['file'] ), 'The image mime type is expected to be image/jpeg.' );
     6488    }
     6489
     6490    /**
     6491     * Ensure a JPEG is converted to WebP when applied via a filter.
     6492     *
     6493     * @ticket 62305
     6494     * @ticket 62359
     6495     *
     6496     * @dataProvider data_image_converted_to_other_format_has_correct_filename
     6497     *
     6498     * @param bool $apply_big_image_size_threshold True if filter needs to apply, otherwise false.
     6499     */
     6500    public function test_jpeg_image_converts_to_webp_when_filtered( bool $apply_big_image_size_threshold ) {
     6501        $temp_dir      = get_temp_dir();
     6502        $file          = $temp_dir . '/33772.jpg';
     6503        $scaled_suffix = $apply_big_image_size_threshold ? '-scaled' : '';
     6504        copy( DIR_TESTDATA . '/images/33772.jpg', $file );
     6505
     6506        $editor = wp_get_image_editor( $file );
     6507
     6508        // Skip if the editor does not support WebP.
     6509        if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/webp' ) ) {
     6510            $this->markTestSkipped( 'WebP is not supported by the selected image editor.' );
     6511        }
     6512
     6513        $attachment_id = self::factory()->attachment->create_object(
     6514            array(
     6515                'post_mime_type' => 'image/jpeg',
     6516                'file'           => $file,
     6517            )
     6518        );
     6519
     6520        if ( $apply_big_image_size_threshold ) {
     6521            add_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) );
     6522        }
     6523
     6524        // Generate all sizes as WebP.
     6525        add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_webp' ) );
     6526
     6527        $image_meta = wp_generate_attachment_metadata( $attachment_id, $file );
     6528
     6529        $this->assertStringEndsNotWith( '.jpg', $image_meta['file'], 'The file extension is expected to change.' );
     6530        $this->assertSame( "33772{$scaled_suffix}.webp", basename( $image_meta['file'] ), "The file name is expected to be 33772{$scaled_suffix}.webp." );
     6531        $this->assertSame( '33772.jpg', $image_meta['original_image'], 'The original image name is expected to be stored in the meta data.' );
     6532        $this->assertSame( 'image/webp', wp_get_image_mime( $image_meta['file'] ), 'The image mime type is expected to be image/webp.' );
     6533    }
     6534
     6535    /**
     6536     * Data provider for test_image_converted_to_other_format_has_correct_filename().
     6537     *
     6538     * @return array[]
     6539     */
     6540    public function data_image_converted_to_other_format_has_correct_filename() {
     6541        return array(
     6542            'do not scale image' => array( false ),
     6543            'scale image'        => array( true ),
     6544        );
     6545    }
     6546
     6547    /**
    64496548     * Helper method to keep track of the last context returned by the 'wp_get_attachment_image_context' filter.
    64506549     *
Note: See TracChangeset for help on using the changeset viewer.