Make WordPress Core

Ticket #39231: 39231.diff

File 39231.diff, 3.3 KB (added by joemcgill, 9 years ago)
  • src/wp-admin/includes/image.php

    diff --git src/wp-admin/includes/image.php src/wp-admin/includes/image.php
    index 5f7b583..4ae53b9 100644
    function wp_generate_attachment_metadata( $attachment_id, $file ) { 
    221221                $fallback_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata );
    222222
    223223                $sizes = array();
     224                $_wp_additional_image_sizes = wp_get_additional_image_sizes();
    224225
    225226                foreach ( $fallback_sizes as $s ) {
    226                         $sizes[ $s ]['width']  = get_option( "{$s}_size_w" );
    227                         $sizes[ $s ]['height'] = get_option( "{$s}_size_h" );
     227                        if ( isset( $_wp_additional_image_sizes[ $s ]['width'] ) ) {
     228                                $sizes[ $s ]['width'] = intval( $_wp_additional_image_sizes[ $s ]['width'] );
     229                        } else {
     230                                $sizes[ $s ]['width'] = get_option( "{$s}_size_w" );
     231                        }
     232
     233                        if ( isset( $_wp_additional_image_sizes[ $s ]['height'] ) ) {
     234                                $sizes[ $s ]['height'] = intval( $_wp_additional_image_sizes[ $s ]['height'] );
     235                        } else {
     236                                $sizes[ $s ]['height'] = get_option( "{$s}_size_h" );
     237                        }
    228238
    229                         // Force thumbnails to be soft crops.
    230                         if ( ! 'thumbnail' === $s ) {
    231                                 $sizes[ $s ]['crop'] = get_option( "{$s}_crop" );
     239                        if ( isset( $_wp_additional_image_sizes[ $s ]['crop'] ) ) {
     240                                $sizes[ $s ]['crop'] = $_wp_additional_image_sizes[ $s ]['crop'];
     241                        } else {
     242                                // Force thumbnails to be soft crops.
     243                                if ( ! 'thumbnail' === $s ) {
     244                                        $sizes[ $s ]['crop'] = get_option( "{$s}_crop" );
     245                                }
    232246                        }
    233247                }
    234248
  • tests/phpunit/tests/image/functions.php

    diff --git tests/phpunit/tests/image/functions.php tests/phpunit/tests/image/functions.php
    index f058560..f17cbb9 100644
    class Tests_Image_Functions extends WP_UnitTestCase { 
    404404
    405405                unlink( $test_file );
    406406        }
     407
     408        /**
     409         * @ticket 39231
     410         */
     411        public function test_fallback_intermediate_image_sizes() {
     412                if ( ! wp_image_editor_supports( array( 'mime_type' => 'application/pdf' ) ) ) {
     413                        $this->markTestSkipped( 'Rendering PDFs is not supported on this system.' );
     414                }
     415
     416                $orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf';
     417                $test_file = '/tmp/wordpress-gsoc-flyer.pdf';
     418                copy( $orig_file, $test_file );
     419
     420                $attachment_id = $this->factory->attachment->create_object( $test_file, 0, array(
     421                        'post_mime_type' => 'application/pdf',
     422                ) );
     423
     424                $this->assertNotEmpty( $attachment_id );
     425
     426                add_image_size( 'test-size', 100, 100 );
     427                add_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10, 2 );
     428
     429                $expected = array(
     430                        'file'      => 'wordpress-gsoc-flyer-77x100.jpg',
     431                        'width'     => 77,
     432                        'height'    => 100,
     433                        'mime-type' => 'image/jpeg',
     434                );
     435
     436                $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
     437                $this->assertTrue( isset( $metadata['sizes']['test-size'] ), 'The `test-size` was not added to the metadata.' );
     438                $this->assertSame( $metadata['sizes']['test-size'], $expected );
     439
     440                remove_image_size( 'test-size' );
     441                remove_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10 );
     442
     443                unlink( $test_file );
     444        }
     445
     446        function filter_fallback_intermediate_image_sizes( $fallback_sizes, $metadata ) {
     447                // Add the 'test-size' to the list of fallback sizes.
     448                $fallback_sizes[] = 'test-size';
     449
     450                return $fallback_sizes;
     451        }
    407452}