Make WordPress Core


Ignore:
Timestamp:
02/02/2024 05:46:50 PM (8 months ago)
Author:
adamsilverstein
Message:

Media: enable AVIF support.

Add support for uploading, editing and saving AVIF images when supported by the server.

Add 'image/avif' to supported mime types. Correctly identify AVIF images and sizes even when PHP doesn't support AVIF. Resize uploaded AVIF files (when supported) and use for front end markup.

Props adamsilverstein, lukefiretoss, ayeshrajans, navjotjsingh, Tyrannous, jb510, gregbenz, nickpagz, JavierCasares, mukesh27, yguyon, swissspidy.
Fixes #51228.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-image-editor-gd.php

    r56418 r57524  
    7272            case 'image/webp':
    7373                return ( $image_types & IMG_WEBP ) != 0;
     74            case 'image/avif':
     75                return ( $image_types & IMG_AVIF ) != 0;
    7476        }
    7577
     
    112114        }
    113115
     116        // AVIF may not work with imagecreatefromstring().
     117        if (
     118            function_exists( 'imagecreatefromavif' ) &&
     119            ( 'image/avif' === wp_get_image_mime( $this->file ) )
     120        ) {
     121            $this->image = @imagecreatefromavif( $this->file );
     122        } else {
     123            $this->image = @imagecreatefromstring( $file_contents );
     124        }
     125
    114126        if ( ! is_gd_image( $this->image ) ) {
    115127            return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file );
     
    512524        } elseif ( 'image/webp' == $mime_type ) {
    513525            if ( ! function_exists( 'imagewebp' ) || ! $this->make_image( $filename, 'imagewebp', array( $image, $filename, $this->get_quality() ) ) ) {
     526                return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
     527            }
     528        } elseif ( 'image/avif' == $mime_type ) {
     529            if ( ! function_exists( 'imageavif' ) || ! $this->make_image( $filename, 'imageavif', array( $image, $filename, $this->get_quality() ) ) ) {
    514530                return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
    515531            }
     
    562578                    header( 'Content-Type: image/webp' );
    563579                    return imagewebp( $this->image, null, $this->get_quality() );
     580                } else {
     581                    // Fall back to JPEG.
     582                    header( 'Content-Type: image/jpeg' );
     583                    return imagejpeg( $this->image, null, $this->get_quality() );
    564584                }
    565                 // Fall back to the default if webp isn't supported.
     585            case 'image/avif':
     586                if ( function_exists( 'imageavif' ) ) {
     587                    header( 'Content-Type: image/avif' );
     588                    return imageavif( $this->image, null, $this->get_quality() );
     589                }
     590                // Fall back to JPEG.
    566591            default:
    567592                header( 'Content-Type: image/jpeg' );
Note: See TracChangeset for help on using the changeset viewer.