getimagesize_mimes_to_exts
Allows you to add an image MIME type and its corresponding file extension.
This filter is used in WordPress core when an uploaded image has an extension that differs from the file's actual MIME type.
For example, suppose there is an image named my-image.jpg, but its actual format is PNG, so the filename is incorrect. During validation, WordPress reaches this hook and obtains the extension for the actual MIME type from it, 'image/png' => 'png'. It then replaces the filename extension, and the image is uploaded to the site as my-image.png.
This hook can be useful when adding image formats that require a mapping between a file extension and its actual MIME type.
Usage
add_filter( 'getimagesize_mimes_to_exts', 'wp_kama_getimagesize_mimes_to_exts_filter' );
/**
* Function for `getimagesize_mimes_to_exts` filter-hook.
*
* @param array $mime_to_ext Array of image mime types and their matching extensions.
*
* @return array
*/
function wp_kama_getimagesize_mimes_to_exts_filter( $mime_to_ext ){
// filter...
return $mime_to_ext;
}
- $mime_to_ext(array)
Array of MIME types and their corresponding file extensions.
array( 'image/jpeg' => 'jpg', 'image/png' => 'png', 'image/gif' => 'gif', 'image/bmp' => 'bmp', 'image/tiff' => 'tif', )
Examples
#1 Correct the extension for the .webp format
Suppose WebP image uploads have been enabled with this code:
add_filter( 'upload_mimes', function( $mimes ){
$mimes['webp'] = 'image/webp';
return $mimes;
} );
Now suppose we try to upload an image downloaded from the web with a .png extension, such as img.png, whose actual format is WebP. The image is not uploaded, and WordPress reports that the file type is not permitted for security reasons. This seems unexpected: PNG images have been uploaded countless times, and WebP was explicitly permitted once the actual format was discovered.
This happens because WordPress determines during validation that the specified file extension does not match the actual file type. It tries to find the correct extension and validate the file again, but cannot find the extension because the mapping does not exist by default, so validation fails.
The required mapping can be added through this hook:
add_filter( 'getimagesize_mimes_to_exts', function( $mime_to_ext ){
$mime_to_ext['image/webp'] = 'webp';
return $mime_to_ext;
} );Changelog
| Since 3.0.0 | Introduced. |
Where the hook is called
$mime_to_ext = apply_filters( 'getimagesize_mimes_to_exts', array( 'image/jpeg' => 'jpg', 'image/png' => 'png', 'image/gif' => 'gif', 'image/bmp' => 'bmp', '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', ) );