Media: Fix uploading of .heic images.

- Adds support for all HEIC/HEIF mime types: `image/heic`, `image/heif`, `image/heic-sequence`, and `image/heif-sequence`.
- Introduces `wp_is_heic_image_mime_type()`.

This backport includes a subsequent fix of a typo.

Reviewed by peterwilsoncc.
Merges [59315,59358] to the 6.7 branch.

Props swissspidy, adamsilverstein, debarghyabanerjee, ironprogrammer, peterwilsoncc, apermo, azaozz, mosne.
Fixes #62272.



Built from https://develop.svn.wordpress.org/branches/6.7@59360


git-svn-id: http://core.svn.wordpress.org/branches/6.7@58746 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2024-11-06 00:47:16 +00:00
parent ea7e427fbd
commit 1abe32ce7c
6 changed files with 83 additions and 30 deletions

View File

@ -271,7 +271,7 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
* If we still don't have the image size, fall back to `wp_getimagesize`. This ensures AVIF and HEIC images * If we still don't have the image size, fall back to `wp_getimagesize`. This ensures AVIF and HEIC images
* are properly sized without affecting previous `getImageGeometry` behavior. * are properly sized without affecting previous `getImageGeometry` behavior.
*/ */
if ( ( ! $width || ! $height ) && ( 'image/avif' === $this->mime_type || 'image/heic' === $this->mime_type ) ) { if ( ( ! $width || ! $height ) && ( 'image/avif' === $this->mime_type || wp_is_heic_image_mime_type( $this->mime_type ) ) ) {
$size = wp_getimagesize( $this->file ); $size = wp_getimagesize( $this->file );
$width = $size[0]; $width = $size[0];
$height = $size[1]; $height = $size[1];

View File

@ -3101,7 +3101,13 @@ function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) {
// Attempt to figure out what type of image it actually is. // Attempt to figure out what type of image it actually is.
$real_mime = wp_get_image_mime( $file ); $real_mime = wp_get_image_mime( $file );
if ( $real_mime && $real_mime !== $type ) { $heic_images_extensions = array(
'heif',
'heics',
'heifs',
);
if ( $real_mime && ( $real_mime !== $type || in_array( $ext, $heic_images_extensions, true ) ) ) {
/** /**
* Filters the list mapping image mime types to their respective extensions. * Filters the list mapping image mime types to their respective extensions.
* *
@ -3119,13 +3125,24 @@ function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) {
'image/tiff' => 'tif', 'image/tiff' => 'tif',
'image/webp' => 'webp', 'image/webp' => 'webp',
'image/avif' => 'avif', 'image/avif' => 'avif',
/*
* In theory there are/should be file extensions that correspond to the
* mime types: .heif, .heics and .heifs. However it seems that HEIC images
* with any of the mime types commonly have a .heic file extension.
* Seems keeping the status quo here is best for compatibility.
*/
'image/heic' => 'heic', 'image/heic' => 'heic',
'image/heif' => 'heic',
'image/heic-sequence' => 'heic',
'image/heif-sequence' => 'heic',
) )
); );
// Replace whatever is after the last period in the filename with the correct extension. // Replace whatever is after the last period in the filename with the correct extension.
if ( ! empty( $mime_to_ext[ $real_mime ] ) ) { if ( ! empty( $mime_to_ext[ $real_mime ] ) ) {
$filename_parts = explode( '.', $filename ); $filename_parts = explode( '.', $filename );
array_pop( $filename_parts ); array_pop( $filename_parts );
$filename_parts[] = $mime_to_ext[ $real_mime ]; $filename_parts[] = $mime_to_ext[ $real_mime ];
$new_filename = implode( '.', $filename_parts ); $new_filename = implode( '.', $filename_parts );
@ -3316,9 +3333,7 @@ function wp_get_image_mime( $file ) {
$mime = ( $imagetype ) ? image_type_to_mime_type( $imagetype ) : false; $mime = ( $imagetype ) ? image_type_to_mime_type( $imagetype ) : false;
} elseif ( function_exists( 'getimagesize' ) ) { } elseif ( function_exists( 'getimagesize' ) ) {
// Don't silence errors when in debug mode, unless running unit tests. // Don't silence errors when in debug mode, unless running unit tests.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG if ( defined( 'WP_DEBUG' ) && WP_DEBUG && ! defined( 'WP_RUN_CORE_TESTS' ) ) {
&& ! defined( 'WP_RUN_CORE_TESTS' )
) {
// Not using wp_getimagesize() here to avoid an infinite loop. // Not using wp_getimagesize() here to avoid an infinite loop.
$imagesize = getimagesize( $file ); $imagesize = getimagesize( $file );
} else { } else {
@ -3365,22 +3380,28 @@ function wp_get_image_mime( $file ) {
// Divide the header string into 4 byte groups. // Divide the header string into 4 byte groups.
$magic = str_split( $magic, 8 ); $magic = str_split( $magic, 8 );
if ( if ( isset( $magic[1] ) && isset( $magic[2] ) && 'ftyp' === hex2bin( $magic[1] ) ) {
isset( $magic[1] ) && if ( 'avif' === hex2bin( $magic[2] ) || 'avis' === hex2bin( $magic[2] ) ) {
isset( $magic[2] ) &&
'ftyp' === hex2bin( $magic[1] ) &&
( 'avif' === hex2bin( $magic[2] ) || 'avis' === hex2bin( $magic[2] ) )
) {
$mime = 'image/avif'; $mime = 'image/avif';
} } elseif ( 'heic' === hex2bin( $magic[2] ) ) {
if (
isset( $magic[1] ) &&
isset( $magic[2] ) &&
'ftyp' === hex2bin( $magic[1] ) &&
( 'heic' === hex2bin( $magic[2] ) || 'heif' === hex2bin( $magic[2] ) )
) {
$mime = 'image/heic'; $mime = 'image/heic';
} elseif ( 'heif' === hex2bin( $magic[2] ) ) {
$mime = 'image/heif';
} else {
/*
* HEIC/HEIF images and image sequences/animations may have other strings here
* like mif1, msf1, etc. For now fall back to using finfo_file() to detect these.
*/
if ( extension_loaded( 'fileinfo' ) ) {
$fileinfo = finfo_open( FILEINFO_MIME_TYPE );
$mime_type = finfo_file( $fileinfo, $file );
finfo_close( $fileinfo );
if ( wp_is_heic_image_mime_type( $mime_type ) ) {
$mime = $mime_type;
}
}
}
} }
} catch ( Exception $e ) { } catch ( Exception $e ) {
$mime = false; $mime = false;
@ -3423,7 +3444,13 @@ function wp_get_mime_types() {
'webp' => 'image/webp', 'webp' => 'image/webp',
'avif' => 'image/avif', 'avif' => 'image/avif',
'ico' => 'image/x-icon', 'ico' => 'image/x-icon',
// TODO: Needs improvement. All images with the following mime types seem to have .heic file extension.
'heic' => 'image/heic', 'heic' => 'image/heic',
'heif' => 'image/heif',
'heics' => 'image/heic-sequence',
'heifs' => 'image/heif-sequence',
// Video formats. // Video formats.
'asf|asx' => 'video/x-ms-asf', 'asf|asx' => 'video/x-ms-asf',
'wmv' => 'video/x-ms-wmv', 'wmv' => 'video/x-ms-wmv',
@ -3543,7 +3570,7 @@ function wp_get_ext_types() {
return apply_filters( return apply_filters(
'ext2type', 'ext2type',
array( array(
'image' => array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico', 'heic', 'webp', 'avif' ), 'image' => array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico', 'heic', 'heif', 'webp', 'avif' ),
'audio' => array( 'aac', 'ac3', 'aif', 'aiff', 'flac', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ), 'audio' => array( 'aac', 'ac3', 'aif', 'aiff', 'flac', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ),
'video' => array( '3g2', '3gp', '3gpp', 'asf', 'avi', 'divx', 'dv', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt', 'rm', 'vob', 'wmv' ), 'video' => array( '3g2', '3gp', '3gpp', 'asf', 'avi', 'divx', 'dv', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt', 'rm', 'vob', 'wmv' ),
'document' => array( 'doc', 'docx', 'docm', 'dotm', 'odt', 'pages', 'pdf', 'xps', 'oxps', 'rtf', 'wp', 'wpd', 'psd', 'xcf' ), 'document' => array( 'doc', 'docx', 'docm', 'dotm', 'odt', 'pages', 'pdf', 'xps', 'oxps', 'rtf', 'wp', 'wpd', 'psd', 'xcf' ),
@ -9037,3 +9064,22 @@ function wp_admin_notice( $message, $args = array() ) {
echo wp_kses_post( wp_get_admin_notice( $message, $args ) ); echo wp_kses_post( wp_get_admin_notice( $message, $args ) );
} }
/**
* Checks if a mime type is for a HEIC/HEIF image.
*
* @since 6.7.0
*
* @param string $mime_type The mime type to check.
* @return bool Whether the mime type is for a HEIC/HEIF image.
*/
function wp_is_heic_image_mime_type( $mime_type ) {
$heic_mime_types = array(
'image/heic',
'image/heif',
'image/heic-sequence',
'image/heif-sequence',
);
return in_array( $mime_type, $heic_mime_types, true );
}

View File

@ -1467,7 +1467,7 @@ Library = wp.media.controller.State.extend(/** @lends wp.media.controller.Librar
isImageAttachment: function( attachment ) { isImageAttachment: function( attachment ) {
// If uploading, we know the filename but not the mime type. // If uploading, we know the filename but not the mime type.
if ( attachment.get('uploading') ) { if ( attachment.get('uploading') ) {
return /\.(jpe?g|png|gif|webp|avif|heic)$/i.test( attachment.get('filename') ); return /\.(jpe?g|png|gif|webp|avif|heic|heif)$/i.test( attachment.get('filename') );
} }
return attachment.get('type') === 'image'; return attachment.get('type') === 'image';

File diff suppressed because one or more lines are too long

View File

@ -5627,9 +5627,7 @@ function wp_show_heic_upload_error( $plupload_settings ) {
*/ */
function wp_getimagesize( $filename, ?array &$image_info = null ) { function wp_getimagesize( $filename, ?array &$image_info = null ) {
// Don't silence errors when in debug mode, unless running unit tests. // Don't silence errors when in debug mode, unless running unit tests.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG if ( defined( 'WP_DEBUG' ) && WP_DEBUG && ! defined( 'WP_RUN_CORE_TESTS' ) ) {
&& ! defined( 'WP_RUN_CORE_TESTS' )
) {
if ( 2 === func_num_args() ) { if ( 2 === func_num_args() ) {
$info = getimagesize( $filename, $image_info ); $info = getimagesize( $filename, $image_info );
} else { } else {
@ -5660,11 +5658,18 @@ function wp_getimagesize( $filename, ?array &$image_info = null ) {
return $info; return $info;
} }
$image_mime_type = wp_get_image_mime( $filename );
// Not an image?
if ( false === $image_mime_type ) {
return false;
}
/* /*
* For PHP versions that don't support WebP images, * For PHP versions that don't support WebP images,
* extract the image size info from the file headers. * extract the image size info from the file headers.
*/ */
if ( 'image/webp' === wp_get_image_mime( $filename ) ) { if ( 'image/webp' === $image_mime_type ) {
$webp_info = wp_get_webp_info( $filename ); $webp_info = wp_get_webp_info( $filename );
$width = $webp_info['width']; $width = $webp_info['width'];
$height = $webp_info['height']; $height = $webp_info['height'];
@ -5686,7 +5691,7 @@ function wp_getimagesize( $filename, ?array &$image_info = null ) {
} }
// For PHP versions that don't support AVIF images, extract the image size info from the file headers. // For PHP versions that don't support AVIF images, extract the image size info from the file headers.
if ( 'image/avif' === wp_get_image_mime( $filename ) ) { if ( 'image/avif' === $image_mime_type ) {
$avif_info = wp_get_avif_info( $filename ); $avif_info = wp_get_avif_info( $filename );
$width = $avif_info['width']; $width = $avif_info['width'];
@ -5709,11 +5714,13 @@ function wp_getimagesize( $filename, ?array &$image_info = null ) {
} }
// For PHP versions that don't support HEIC images, extract the size info using Imagick when available. // For PHP versions that don't support HEIC images, extract the size info using Imagick when available.
if ( 'image/heic' === wp_get_image_mime( $filename ) ) { if ( wp_is_heic_image_mime_type( $image_mime_type ) ) {
$editor = wp_get_image_editor( $filename ); $editor = wp_get_image_editor( $filename );
if ( is_wp_error( $editor ) ) { if ( is_wp_error( $editor ) ) {
return false; return false;
} }
// If the editor for HEICs is Imagick, use it to get the image size. // If the editor for HEICs is Imagick, use it to get the image size.
if ( $editor instanceof WP_Image_Editor_Imagick ) { if ( $editor instanceof WP_Image_Editor_Imagick ) {
$size = $editor->get_size(); $size = $editor->get_size();

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.7-RC3-59353'; $wp_version = '6.7-RC3-59360';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.