Make WordPress Core


Ignore:
Timestamp:
08/05/2024 04:11:40 AM (15 months ago)
Author:
noisysocks
Message:

Media: Automatically convert HEIC images to JPEG

Automatically create a JPEG version of uploaded HEIC images if the server has
a version of Imagick that supports HEIC. Conversion is done silently through
the existing WP_Image_Editor infrastructure that creates multiple sizes of
uploaded images.

This allows users to view HEIC images in WP Admin and use them in their posts
and pages regardless of whether their browser supports HEIC. Browser support
for HEIC is relatively low (only Safari) while the occurrence of HEIC images is
relatively common. The original HEIC image can be downloaded via a link on
the attachment page.

Props adamsilverstein, noisysocks, swissspidy, spacedmonkey, peterwilsoncc.
Fixes #53645.

File:
1 edited

Legend:

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

    r58773 r58849  
    40654065    // Check and set the output mime type mapped to the input type.
    40664066    if ( isset( $args['mime_type'] ) ) {
    4067         /** This filter is documented in wp-includes/class-wp-image-editor.php */
    4068         $output_format = apply_filters( 'image_editor_output_format', array(), $path, $args['mime_type'] );
     4067        $output_format = wp_get_image_editor_output_format( $path, $args['mime_type'] );
    40694068        if ( isset( $output_format[ $args['mime_type'] ] ) ) {
    40704069            $args['output_mime_type'] = $output_format[ $args['mime_type'] ];
     
    42234222    if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/avif' ) ) ) {
    42244223        $defaults['avif_upload_error'] = true;
     4224    }
     4225
     4226    // Check if HEIC images can be edited.
     4227    if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/heic' ) ) ) {
     4228        $defaults['heic_upload_error'] = true;
    42254229    }
    42264230
     
    54845488 *
    54855489 * @since 5.5.0
     5490 * @since 6.7.0 The default behavior is to enable heic uplooads as long as the server
     5491 *              supports the format. The uploads are converted to JPEG's by default.
    54865492 *
    54875493 * @param array[] $plupload_settings The settings for Plupload.js.
     
    54895495 */
    54905496function wp_show_heic_upload_error( $plupload_settings ) {
    5491     $plupload_settings['heic_upload_error'] = true;
     5497    // Check if HEIC images can be edited.
     5498    if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/heic' ) ) ) {
     5499        $plupload_init['heic_upload_error'] = true;
     5500    }
    54925501    return $plupload_settings;
    54935502}
     
    55835592                ),
    55845593                'mime' => 'image/avif',
     5594            );
     5595        }
     5596    }
     5597
     5598    // For PHP versions that don't support HEIC images, extract the size info using Imagick when available.
     5599    if ( 'image/heic' === wp_get_image_mime( $filename ) ) {
     5600        $editor = wp_get_image_editor( $filename );
     5601        if ( is_wp_error( $editor ) ) {
     5602            return false;
     5603        }
     5604        // If the editor for HEICs is Imagick, use it to get the image size.
     5605        if ( $editor instanceof WP_Image_Editor_Imagick ) {
     5606            $size = $editor->get_size();
     5607            return array(
     5608                $size['width'],
     5609                $size['height'],
     5610                IMAGETYPE_HEIC,
     5611                sprintf(
     5612                    'width="%d" height="%d"',
     5613                    $size['width'],
     5614                    $size['height']
     5615                ),
     5616                'mime' => 'image/heic',
    55855617            );
    55865618        }
     
    60706102    return $high_priority_element;
    60716103}
     6104
     6105/**
     6106 * Determines the output format for the image editor.
     6107 *
     6108 * @since 6.7.0
     6109 * @access private
     6110 *
     6111 * @param string $filename  Path to the image.
     6112 * @param string $mime_type The source image mime type.
     6113 * @return string[] An array of mime type mappings.
     6114 */
     6115function wp_get_image_editor_output_format( $filename, $mime_type ) {
     6116    /**
     6117     * Filters the image editor output format mapping.
     6118     *
     6119     * Enables filtering the mime type used to save images. By default,
     6120     * the mapping array is empty, so the mime type matches the source image.
     6121     *
     6122     * @see WP_Image_Editor::get_output_format()
     6123     *
     6124     * @since 5.8.0
     6125     * @since 6.7.0 The default was changed from array() to array( 'image/heic' => 'image/jpeg' ).
     6126     *
     6127     * @param string[] $output_format {
     6128     *     An array of mime type mappings. Maps a source mime type to a new
     6129     *     destination mime type. Default maps uploaded HEIC images to JPEG output.
     6130     *
     6131     *     @type string ...$0 The new mime type.
     6132     * }
     6133     * @param string $filename  Path to the image.
     6134     * @param string $mime_type The source image mime type.
     6135     */
     6136    return apply_filters( 'image_editor_output_format', array( 'image/heic' => 'image/jpeg' ), $filename, $mime_type );
     6137}
Note: See TracChangeset for help on using the changeset viewer.