Make WordPress Core

Changeset 46459


Ignore:
Timestamp:
10/11/2019 07:52:12 AM (7 years ago)
Author:
mikeschroder
Message:

Media: Update fallback previews to support _wp_make_subsizes()

Allows fallback previews (PDF by default, in core) to save image meta
after each size is generated to allow for resuming, and make sizes
immediately available.

Props azaozz, antpb, progremzion, mikeschroder.
See #48111.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/image.php

    r46421 r46459  
    582582                                // Resize based on the full size image, rather than the source.
    583583                                if ( ! is_wp_error( $uploaded ) ) {
    584                                         $editor = wp_get_image_editor( $uploaded['path'] );
     584                                        $image_file = $uploaded['path'];
    585585                                        unset( $uploaded['path'] );
    586586
    587                                         if ( ! is_wp_error( $editor ) ) {
    588                                                 $metadata['sizes']         = $editor->multi_resize( $merged_sizes );
    589                                                 $metadata['sizes']['full'] = $uploaded;
    590                                         }
     587                                        $metadata['sizes'] = array(
     588                                                'full' => $uploaded,
     589                                        );
     590
     591                                        // Save the meta data before any image post-processing errors could happen.
     592                                        wp_update_attachment_metadata( $attachment_id, $metadata );
     593
     594                                        // Create sub-sizes saving the image meta after each.
     595                                        $metadata = _wp_make_subsizes( $merged_sizes, $image_file, $metadata, $attachment_id );
    591596                                }
    592597                        }
  • trunk/tests/phpunit/tests/image/functions.php

    r43571 r46459  
    436436                $expected = array(
    437437                        'sizes' => array(
     438                                'full'      => array(
     439                                        'file'      => 'wordpress-gsoc-flyer-pdf.jpg',
     440                                        'width'     => 1088,
     441                                        'height'    => 1408,
     442                                        'mime-type' => 'image/jpeg',
     443                                ),
     444                                'medium'    => array(
     445                                        'file'      => 'wordpress-gsoc-flyer-pdf-232x300.jpg',
     446                                        'width'     => 232,
     447                                        'height'    => 300,
     448                                        'mime-type' => 'image/jpeg',
     449                                ),
     450                                'large'     => array(
     451                                        'file'      => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',
     452                                        'width'     => 791,
     453                                        'height'    => 1024,
     454                                        'mime-type' => 'image/jpeg',
     455                                ),
    438456                                'thumbnail' => array(
    439457                                        'file'      => 'wordpress-gsoc-flyer-pdf-116x150.jpg',
     
    442460                                        'mime-type' => 'image/jpeg',
    443461                                ),
     462                        ),
     463                );
     464
     465                $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
     466                $this->assertSame( $expected, $metadata );
     467
     468                unlink( $test_file );
     469                foreach ( $metadata['sizes'] as $size ) {
     470                        unlink( '/tmp/' . $size['file'] );
     471                }
     472        }
     473
     474        /**
     475         * Crop setting for PDF.
     476         *
     477         * @ticket 43226
     478         */
     479        public function test_crop_setting_for_pdf() {
     480
     481                if ( ! wp_image_editor_supports( array( 'mime_type' => 'application/pdf' ) ) ) {
     482                        $this->markTestSkipped( 'Rendering PDFs is not supported on this system.' );
     483                }
     484
     485                update_option( 'medium_crop', 1 );
     486
     487                $orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf';
     488                $test_file = get_temp_dir() . 'wordpress-gsoc-flyer.pdf';
     489                copy( $orig_file, $test_file );
     490
     491                $attachment_id = $this->factory->attachment->create_object(
     492                        $test_file,
     493                        0,
     494                        array(
     495                                'post_mime_type' => 'application/pdf',
     496                        )
     497                );
     498
     499                $this->assertNotEmpty( $attachment_id );
     500
     501                $expected = array(
     502                        'sizes' => array(
     503                                'full'      => array(
     504                                        'file'      => 'wordpress-gsoc-flyer-pdf.jpg',
     505                                        'width'     => 1088,
     506                                        'height'    => 1408,
     507                                        'mime-type' => 'image/jpeg',
     508                                ),
    444509                                'medium'    => array(
    445                                         'file'      => 'wordpress-gsoc-flyer-pdf-232x300.jpg',
    446                                         'width'     => 232,
     510                                        'file'      => 'wordpress-gsoc-flyer-pdf-300x300.jpg',
     511                                        'width'     => 300,
    447512                                        'height'    => 300,
    448513                                        'mime-type' => 'image/jpeg',
     
    454519                                        'mime-type' => 'image/jpeg',
    455520                                ),
    456                                 'full'      => array(
    457                                         'file'      => 'wordpress-gsoc-flyer-pdf.jpg',
    458                                         'width'     => 1088,
    459                                         'height'    => 1408,
    460                                         'mime-type' => 'image/jpeg',
    461                                 ),
    462                         ),
    463                 );
    464 
    465                 $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
    466                 $this->assertSame( $expected, $metadata );
    467 
    468                 unlink( $test_file );
    469                 foreach ( $metadata['sizes'] as $size ) {
    470                         unlink( '/tmp/' . $size['file'] );
    471                 }
    472         }
    473 
    474         /**
    475          * Crop setting for PDF.
    476          *
    477          * @ticket 43226
    478          */
    479         public function test_crop_setting_for_pdf() {
    480 
    481                 if ( ! wp_image_editor_supports( array( 'mime_type' => 'application/pdf' ) ) ) {
    482                         $this->markTestSkipped( 'Rendering PDFs is not supported on this system.' );
    483                 }
    484 
    485                 update_option( 'medium_crop', 1 );
    486 
    487                 $orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf';
    488                 $test_file = get_temp_dir() . 'wordpress-gsoc-flyer.pdf';
    489                 copy( $orig_file, $test_file );
    490 
    491                 $attachment_id = $this->factory->attachment->create_object(
    492                         $test_file,
    493                         0,
    494                         array(
    495                                 'post_mime_type' => 'application/pdf',
    496                         )
    497                 );
    498 
    499                 $this->assertNotEmpty( $attachment_id );
    500 
    501                 $expected = array(
    502                         'sizes' => array(
    503521                                'thumbnail' => array(
    504522                                        'file'      => 'wordpress-gsoc-flyer-pdf-116x150.jpg',
    505523                                        'width'     => 116,
    506524                                        'height'    => 150,
    507                                         'mime-type' => 'image/jpeg',
    508                                 ),
    509                                 'medium'    => array(
    510                                         'file'      => 'wordpress-gsoc-flyer-pdf-300x300.jpg',
    511                                         'width'     => 300,
    512                                         'height'    => 300,
    513                                         'mime-type' => 'image/jpeg',
    514                                 ),
    515                                 'large'     => array(
    516                                         'file'      => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',
    517                                         'width'     => 791,
    518                                         'height'    => 1024,
    519                                         'mime-type' => 'image/jpeg',
    520                                 ),
    521                                 'full'      => array(
    522                                         'file'      => 'wordpress-gsoc-flyer-pdf.jpg',
    523                                         'width'     => 1088,
    524                                         'height'    => 1408,
    525525                                        'mime-type' => 'image/jpeg',
    526526                                ),
Note: See TracChangeset for help on using the changeset viewer.