Make WordPress Core


Ignore:
Timestamp:
03/10/2022 01:08:19 PM (3 years ago)
Author:
spacedmonkey
Message:

Media: Store attachment’s file size in metadata.

Store the file size of all newly uploaded attachments, as part of the metadata stored in post meta. Storing file size means, developers will not have to resort to doing filesize function calls, that can be time consuming on assets on offloaded to services like Amazon’s S3.

This change also introduces a new helper function called, wp_filesize. This is a wrapper around the filesize php function, that adds some helpful filters and ensures the return value is an integer.

Props Cybr, Spacedmonkey, SergeyBiryukov, johnwatkins0, swissspidy, desrosj, joemcgill, azaozz, antpb, adamsilverstein, uday17035.
Fixes #49412.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/image/functions.php

    r52269 r52837  
    480480        $this->assertNotEmpty( $attachment_id );
    481481
     482        $temp_dir = get_temp_dir();
     483
     484        $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
     485
    482486        $expected = array(
    483             'sizes' => array(
     487            'sizes'    => array(
    484488                'full'      => array(
    485489                    'file'      => 'wordpress-gsoc-flyer-pdf.jpg',
     
    487491                    'height'    => 1408,
    488492                    'mime-type' => 'image/jpeg',
     493                    'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf.jpg' ),
    489494                ),
    490495                'medium'    => array(
     
    493498                    'height'    => 300,
    494499                    'mime-type' => 'image/jpeg',
     500                    'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-232x300.jpg' ),
    495501                ),
    496502                'large'     => array(
     
    499505                    'height'    => 1024,
    500506                    'mime-type' => 'image/jpeg',
     507                    'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),
    501508                ),
    502509                'thumbnail' => array(
     
    505512                    'height'    => 150,
    506513                    'mime-type' => 'image/jpeg',
     514                    'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),
    507515                ),
    508516            ),
    509         );
    510 
    511         $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
     517            'filesize' => wp_filesize( $test_file ),
     518        );
     519
    512520        $this->assertSame( $expected, $metadata );
    513521
    514522        unlink( $test_file );
    515         $temp_dir = get_temp_dir();
    516523        foreach ( $metadata['sizes'] as $size ) {
    517524            unlink( $temp_dir . $size['file'] );
     
    550557        $this->assertNotEmpty( $attachment_id );
    551558
     559        $temp_dir = get_temp_dir();
     560
     561        $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
     562
    552563        $expected = array(
    553             'sizes' => array(
     564            'sizes'    => array(
    554565                'full'      => array(
    555566                    'file'      => 'wordpress-gsoc-flyer-pdf.jpg',
     
    557568                    'height'    => 1408,
    558569                    'mime-type' => 'image/jpeg',
     570                    'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf.jpg' ),
    559571                ),
    560572                'medium'    => array(
     
    563575                    'height'    => 300,
    564576                    'mime-type' => 'image/jpeg',
     577                    'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-300x300.jpg' ),
    565578                ),
    566579                'large'     => array(
     
    569582                    'height'    => 1024,
    570583                    'mime-type' => 'image/jpeg',
     584                    'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),
    571585                ),
    572586                'thumbnail' => array(
     
    575589                    'height'    => 150,
    576590                    'mime-type' => 'image/jpeg',
     591                    'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),
    577592                ),
    578593            ),
    579         );
    580 
    581         $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
     594            'filesize' => wp_filesize( $test_file ),
     595        );
     596
    582597        $this->assertSame( $expected, $metadata );
    583598
    584599        unlink( $test_file );
    585600        foreach ( $metadata['sizes'] as $size ) {
    586             unlink( get_temp_dir() . $size['file'] );
     601            unlink( $temp_dir . $size['file'] );
    587602        }
    588603    }
     
    617632        add_image_size( 'test-size', 100, 100 );
    618633        add_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10, 2 );
     634
     635        $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
     636
     637        $temp_dir = get_temp_dir();
    619638
    620639        $expected = array(
     
    623642            'height'    => 100,
    624643            'mime-type' => 'image/jpeg',
    625         );
    626 
    627         $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
     644            'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-77x100.jpg' ),
     645        );
     646
     647        // Different environments produce slightly different filesize results.
     648        $this->assertSame( $metadata['sizes']['test-size'], $expected );
     649
    628650        $this->assertArrayHasKey( 'test-size', $metadata['sizes'], 'The `test-size` was not added to the metadata.' );
    629651        $this->assertSame( $expected, $metadata['sizes']['test-size'] );
     
    633655
    634656        unlink( $test_file );
    635         $temp_dir = get_temp_dir();
    636657        foreach ( $metadata['sizes'] as $size ) {
    637658            unlink( $temp_dir . $size['file'] );
Note: See TracChangeset for help on using the changeset viewer.