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
* 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 );
$width = $size[0];
$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.
$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.
*
@ -3119,13 +3125,24 @@ function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) {
'image/tiff' => 'tif',
'image/webp' => 'webp',
'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/heif' => 'heic',
'image/heic-sequence' => 'heic',
'image/heif-sequence' => 'heic',
)
);
// Replace whatever is after the last period in the filename with the correct extension.
if ( ! empty( $mime_to_ext[ $real_mime ] ) ) {
$filename_parts = explode( '.', $filename );
array_pop( $filename_parts );
$filename_parts[] = $mime_to_ext[ $real_mime ];
$new_filename = implode( '.', $filename_parts );
@ -3316,9 +3333,7 @@ function wp_get_image_mime( $file ) {
$mime = ( $imagetype ) ? image_type_to_mime_type( $imagetype ) : false;
} elseif ( function_exists( 'getimagesize' ) ) {
// Don't silence errors when in debug mode, unless running unit tests.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG
&& ! defined( 'WP_RUN_CORE_TESTS' )
) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && ! defined( 'WP_RUN_CORE_TESTS' ) ) {
// Not using wp_getimagesize() here to avoid an infinite loop.
$imagesize = getimagesize( $file );
} else {
@ -3365,22 +3380,28 @@ function wp_get_image_mime( $file ) {
// Divide the header string into 4 byte groups.
$magic = str_split( $magic, 8 );
if (
isset( $magic[1] ) &&
isset( $magic[2] ) &&
'ftyp' === hex2bin( $magic[1] ) &&
( 'avif' === hex2bin( $magic[2] ) || 'avis' === hex2bin( $magic[2] ) )
) {
$mime = 'image/avif';
}
if ( isset( $magic[1] ) && isset( $magic[2] ) && 'ftyp' === hex2bin( $magic[1] ) ) {
if ( 'avif' === hex2bin( $magic[2] ) || 'avis' === hex2bin( $magic[2] ) ) {
$mime = 'image/avif';
} elseif ( 'heic' === hex2bin( $magic[2] ) ) {
$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 (
isset( $magic[1] ) &&
isset( $magic[2] ) &&
'ftyp' === hex2bin( $magic[1] ) &&
( 'heic' === hex2bin( $magic[2] ) || 'heif' === hex2bin( $magic[2] ) )
) {
$mime = 'image/heic';
if ( wp_is_heic_image_mime_type( $mime_type ) ) {
$mime = $mime_type;
}
}
}
}
} catch ( Exception $e ) {
$mime = false;
@ -3423,7 +3444,13 @@ function wp_get_mime_types() {
'webp' => 'image/webp',
'avif' => 'image/avif',
'ico' => 'image/x-icon',
// TODO: Needs improvement. All images with the following mime types seem to have .heic file extension.
'heic' => 'image/heic',
'heif' => 'image/heif',
'heics' => 'image/heic-sequence',
'heifs' => 'image/heif-sequence',
// Video formats.
'asf|asx' => 'video/x-ms-asf',
'wmv' => 'video/x-ms-wmv',
@ -3543,7 +3570,7 @@ function wp_get_ext_types() {
return apply_filters(
'ext2type',
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' ),
'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' ),
@ -9037,3 +9064,22 @@ function wp_admin_notice( $message, $args = array() ) {
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 ) {
// If uploading, we know the filename but not the mime type.
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';

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 ) {
// Don't silence errors when in debug mode, unless running unit tests.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG
&& ! defined( 'WP_RUN_CORE_TESTS' )
) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && ! defined( 'WP_RUN_CORE_TESTS' ) ) {
if ( 2 === func_num_args() ) {
$info = getimagesize( $filename, $image_info );
} else {
@ -5660,11 +5658,18 @@ function wp_getimagesize( $filename, ?array &$image_info = null ) {
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,
* 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 );
$width = $webp_info['width'];
$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.
if ( 'image/avif' === wp_get_image_mime( $filename ) ) {
if ( 'image/avif' === $image_mime_type ) {
$avif_info = wp_get_avif_info( $filename );
$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.
if ( 'image/heic' === wp_get_image_mime( $filename ) ) {
if ( wp_is_heic_image_mime_type( $image_mime_type ) ) {
$editor = wp_get_image_editor( $filename );
if ( is_wp_error( $editor ) ) {
return false;
}
// If the editor for HEICs is Imagick, use it to get the image size.
if ( $editor instanceof WP_Image_Editor_Imagick ) {
$size = $editor->get_size();

View File

@ -16,7 +16,7 @@
*
* @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.