Make WordPress Core

Ticket #15311: 15311.7.diff

File 15311.7.diff, 4.3 KB (added by ericlewis, 9 years ago)
  • src/wp-includes/media.php

     
    581581 * @return bool|array False on failure or array of file path, width, and height on success.
    582582 */
    583583function image_get_intermediate_size($post_id, $size='thumbnail') {
     584        global $_wp_additional_image_sizes;
     585
    584586        if ( !is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) )
    585587                return false;
    586588
     
    619621                }
    620622        }
    621623
    622         if ( is_array($size) || empty($size) || empty($imagedata['sizes'][$size]) )
     624        if ( is_array( $size ) || empty( $size ) ) {
    623625                return false;
     626        }
    624627
     628        /*
     629         * If a file for requested image size isn't found in attachment metadata,
     630         * but the size is registered, generate it on the fly.
     631         */
     632        if ( empty( $imagedata['sizes'][$size] ) && in_array( $size, get_intermediate_image_sizes() ) ) {
     633                $upload_dir = wp_upload_dir();
     634                $size_info = $_wp_additional_image_sizes[$size];
     635                $large = image_get_intermediate_size( $post_id, 'large' );
     636                /*
     637                 * To save resources, crop from the large version of the image
     638                 * if it is larger than the requested size.
     639                 */
     640                if ( $large && $large['width'] >= $size_info['width'] && $large['height'] >= $size_info['height'] ) {
     641                        $file_to_crop_from = $upload_dir['basedir'] . "/{$large['path']}";
     642                } else {
     643                        $file_to_crop_from = get_attached_file( $post_id );
     644                }
     645                if ( $intermediate_size = image_make_intermediate_size( $file_to_crop_from, $size_info['width'], $size_info['height'], $size_info['crop'] ) ) {
     646                        $imagedata['sizes'][$size] = $intermediate_size;
     647                        wp_update_attachment_metadata( $post_id, $imagedata );
     648                }
     649        }
     650
     651        if ( empty( $imagedata['sizes'][$size]) ) {
     652                return false;
     653        }
     654
    625655        $data = $imagedata['sizes'][$size];
    626656        // include the full filesystem path of the intermediate file
    627657        if ( empty($data['path']) && !empty($data['file']) ) {
  • tests/phpunit/tests/post/attachments.php

     
    278278                $this->assertEquals( $attachment->post_parent, $post_id );
    279279        }
    280280
     281        /**
     282         * Image generation on the fly should cut from the large image size when possible.
     283         */
     284        function test_generate_image_on_the_fly_with_large_size() {
     285                if ( !function_exists( 'imagejpeg' ) )
     286                        $this->markTestSkipped( 'jpeg support unavailable' );
     287
     288                $filename = ( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG' );
     289                $contents = file_get_contents( $filename );
     290
     291                $upload = wp_upload_bits( basename($filename), null, $contents );
     292                $this->assertTrue( empty($upload['error']) );
     293
     294                $attachment_id = $this->_make_attachment( $upload );
     295
     296                // After the attachment has been created,
     297                // add a new image size and request it.
     298                add_image_size( 'medium-small', 400, 400, true );
     299                $image = image_get_intermediate_size( $attachment_id, 'medium-small' );
     300                $this->assertNotFalse( $image );
     301                $this->assertEquals( $image['width'], 400 );
     302                $this->assertEquals( $image['height'], 400 );
     303        }
     304
     305        /**
     306         * Image generation on the fly should cut from the large image size when possible,
     307         * but if the large image is smaller than the requested size, use the original.
     308         */
     309        function test_generate_image_on_the_fly_with_original_size() {
     310                if ( !function_exists( 'imagejpeg' ) )
     311                        $this->markTestSkipped( 'jpeg support unavailable' );
     312
     313                // Make the "large" size too small to cut the `medium-small` size from,
     314                // forcing the original to be used for cutting instead.
     315                update_option( 'large_size_w', 100 );
     316                update_option( 'large_size_h', 100 );
     317
     318                $filename = ( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG' );
     319                $contents = file_get_contents( $filename );
     320
     321                $upload = wp_upload_bits( basename($filename), null, $contents );
     322                $this->assertTrue( empty($upload['error']) );
     323
     324                $attachment_id = $this->_make_attachment( $upload );
     325
     326                // After the attachment has been created,
     327                // add a new image size and request it.
     328                add_image_size( 'medium-small', 400, 400, true );
     329                $image = image_get_intermediate_size( $attachment_id, 'medium-small' );
     330                $this->assertNotFalse( $image );
     331                $this->assertEquals( $image['width'], 400 );
     332                $this->assertEquals( $image['height'], 400 );
     333        }
    281334}