Make WordPress Core

Ticket #48489: 48489.2.diff

File 48489.2.diff, 3.9 KB (added by f26d, 12 months ago)
  • src/wp-admin/includes/image.php

    diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php
    index 2bdcc505e4..c2a2a469cd 100644
    a b function wp_create_image_subsizes( $file, $attachment_id ) { 
    264264        // Do not scale (large) PNG images. May result in sub-sizes that have greater file size than the original. See #48736.
    265265        if ( 'image/png' !== $imagesize['mime'] ) {
    266266
     267                $threshold = 2560;
     268
     269                // If there are registered image sizes with a width greater than the default threshold,
     270                // use that value instead of the default. See https://core.trac.wordpress.org/ticket/48489.
     271                foreach ( wp_get_registered_image_subsizes() as $subsize ) {
     272                        if ( $subsize['width'] > $threshold ) {
     273                                $threshold = $subsize['width'];
     274                        }
     275                }
     276
    267277                /**
    268278                 * Filters the "BIG image" threshold value.
    269279                 *
    function wp_create_image_subsizes( $file, $attachment_id ) { 
    275285                 *
    276286                 * @since 5.3.0
    277287                 *
    278                  * @param int    $threshold     The threshold value in pixels. Default 2560.
     288                 * @param int    $threshold     The threshold value in pixels. Default 2560 or the maximum registered image size width.
    279289                 * @param array  $imagesize     {
    280290                 *     Indexed array of the image width and height in pixels.
    281291                 *
    function wp_create_image_subsizes( $file, $attachment_id ) { 
    285295                 * @param string $file          Full path to the uploaded image file.
    286296                 * @param int    $attachment_id Attachment post ID.
    287297                 */
    288                 $threshold = (int) apply_filters( 'big_image_size_threshold', 2560, $imagesize, $file, $attachment_id );
     298                $threshold = (int) apply_filters( 'big_image_size_threshold', $threshold, $imagesize, $file, $attachment_id );
    289299
    290300                /*
    291301                 * If the original image's dimensions are over the threshold,
  • new file tests/phpunit/tests/media/wpCreateImageSubsizes.php

    diff --git a/tests/phpunit/tests/media/wpCreateImageSubsizes.php b/tests/phpunit/tests/media/wpCreateImageSubsizes.php
    new file mode 100644
    index 0000000000..6cc8cf74b2
    - +  
     1<?php
     2
     3/**
     4 * Tests for the `wp_create_image_subsizes()` function.
     5 *
     6 * @group media
     7 * @covers ::wp_create_image_subsizes
     8 */
     9class Tests_Media_wpCreateImageSubsizes extends WP_UnitTestCase {
     10        protected const IMAGE_SIZE = 'test-wp-big-image-size-threshold';
     11
     12        protected static $attachment_id;
     13
     14        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     15                add_image_size( SELF::IMAGE_SIZE, 3000, 4500 );
     16
     17                self::$attachment_id = $factory->attachment->create_upload_object( DIR_TESTDATA . '/images/test-image-large.jpg' );
     18        }
     19
     20        public static function tear_down_after_class() {
     21                remove_image_size( SELF::IMAGE_SIZE );
     22
     23                wp_delete_attachment( self::$attachment_id, true );
     24
     25                parent::tear_down_after_class();
     26        }
     27
     28        /**
     29         * Checks that registered image sizes have an impact on the big image threshold.
     30         *
     31         * @ticket 48489
     32         */
     33        public function test_wp_big_image_size_threshold_respects_image_sizes() {
     34                $image_subsizes = wp_create_image_subsizes( get_attached_file( self::$attachment_id ), self::$attachment_id );
     35
     36                $this->assertSame( $image_subsizes[ 'width' ], 3000 );
     37        }
     38
     39        /**
     40         * Filter callback for `big_image_size_threshold` filter.
     41         */
     42        public function filter_big_image_size_threshold(): int {
     43                return 1200;
     44        }
     45
     46        /**
     47         * Checks that registered image sizes have an impact on the big image threshold.
     48         *
     49         * @ticket 48489
     50         */
     51        public function test_wp_big_image_size_threshold_filter() {
     52                add_filter( 'big_image_size_threshold', array($this, 'filter_big_image_size_threshold'), 10, 0 );
     53
     54                $image_subsizes = wp_create_image_subsizes( get_attached_file( self::$attachment_id ), self::$attachment_id );
     55
     56                $this->assertSame( $image_subsizes[ 'width' ], $this->filter_big_image_size_threshold() );
     57
     58                remove_filter( 'big_image_size_threshold', array($this, 'filter_big_image_size_threshold') );
     59        }
     60}