Skip to content

Commit

Permalink
Media: Fix uploading of .heic images.
Browse files Browse the repository at this point in the history
- 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.




git-svn-id: https://develop.svn.wordpress.org/branches/6.7@59360 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
peterwilsoncc committed Nov 6, 2024
1 parent 905891e commit 1d6e65d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/js/media/controllers/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,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';
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/class-wp-image-editor-imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ protected function update_size( $width = null, $height = null ) {
* 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];
Expand Down
86 changes: 66 additions & 20 deletions src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 );
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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' ),
Expand Down Expand Up @@ -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 );
}
19 changes: 13 additions & 6 deletions src/wp-includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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'];
Expand All @@ -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'];
Expand All @@ -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();
Expand Down

0 comments on commit 1d6e65d

Please sign in to comment.