wp_get_image_mime( string $file ): string|false
- Since
- 4.7.1, 5.8.0, 6.5.0, 6.7.0
- Source
wp-includes/functions.php:3330
Description
This depends on exif_imagetype() or getimagesize() to determine real mime types.
Compatibility
- WordPress
- since 6.7.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$filestring- Full path to the file.
Return value
string|false- The actual mime type or false if the type cannot be determined.
Performance profile
How much work a call to wp_get_image_mime() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 16–101
- Plugin surface
- None
- Called by
- 5
Reads or writes the filesystem via file_get_contents().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 131.
Nothing here hands control to plugin code.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- filesystemfilesystem access
file_get_contents()called directly
What one call costs · 27 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_image_mime() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_callable() && $mime !== false | 16 | is_callable(), exif_imagetype() |
!is_callable() && $mime !== false | 20–25 | is_callable(), getimagesize() |
is_callable() && $mime !== false | 20 | is_callable(), exif_imagetype(), image_type_to_mime_type() |
is_callable() && $mime === false && $magic === false | 26 | is_callable(), exif_imagetype(), file_get_contents() |
!is_callable() && $mime === false && $magic === false | 30–35 | is_callable(), getimagesize(), file_get_contents() |
is_callable() && $mime === false && $magic === false | 30 | is_callable(), exif_imagetype(), image_type_to_mime_type(), file_get_contents() |
is_callable() && $mime === false && $magic !== false && !isset($magic) | 39–45 | is_callable(), exif_imagetype(), file_get_contents(), bin2hex(), str_split() |
!is_callable() && $mime === false && $magic !== false && !isset($magic) | 43–54 | is_callable(), getimagesize(), file_get_contents(), bin2hex(), str_split() |
is_callable() && $mime === false && $magic !== false && !isset($magic) | 43–49 | is_callable(), exif_imagetype(), image_type_to_mime_type(), file_get_contents(), bin2hex(), str_split() |
is_callable() && $mime === false && $magic !== false && isset($magic) | 47–51 | is_callable(), exif_imagetype(), file_get_contents(), bin2hex(), str_split(), hex2bin() |
!is_callable() && $mime === false && $magic !== false && isset($magic) | 51–60 | is_callable(), getimagesize(), file_get_contents(), bin2hex(), str_split(), hex2bin() |
is_callable() && $mime === false && $magic !== false && isset($magic) | 51–55 | is_callable(), exif_imagetype(), image_type_to_mime_type(), file_get_contents(), bin2hex(), str_split(), hex2bin() |
15 further outcomes, up to 96 instructions
is_callable() && $mime === false && $magic !== false && isset($magic) | 54–58 | is_callable(), exif_imagetype(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin() |
!is_callable() && $mime === false && $magic !== false && isset($magic) | 58–67 | is_callable(), getimagesize(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin() |
is_callable() && $mime === false && $magic !== false && isset($magic) | 58–62 | is_callable(), exif_imagetype(), image_type_to_mime_type(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin() |
is_callable() && $mime === false && $magic !== false && isset($magic) | 60–64 | is_callable(), exif_imagetype(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin() |
!is_callable() && $mime === false && $magic !== false && isset($magic) | 64–73 | is_callable(), getimagesize(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin() |
is_callable() && $mime === false && $magic !== false && isset($magic) | 64–68 | is_callable(), exif_imagetype(), image_type_to_mime_type(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin() |
is_callable() && $mime === false && $magic !== false && isset($magic) | 66–70 | is_callable(), exif_imagetype(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin(), hex2bin() |
!is_callable() && $mime === false && $magic !== false && isset($magic) | 70–79 | is_callable(), getimagesize(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin(), hex2bin() |
is_callable() && $mime === false && $magic !== false && isset($magic) | 70–74 | is_callable(), exif_imagetype(), image_type_to_mime_type(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin(), hex2bin() |
is_callable() && $mime === false && $magic !== false && isset($magic) | 72–76 | is_callable(), exif_imagetype(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), hex2bin() |
!is_callable() && $mime === false && $magic !== false && isset($magic) | 76–85 | is_callable(), getimagesize(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), hex2bin() |
is_callable() && $mime === false && $magic !== false && isset($magic) | 76–80 | is_callable(), exif_imagetype(), image_type_to_mime_type(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), hex2bin() |
is_callable() && $mime === false && $magic !== false && isset($magic) | 87–92 | is_callable(), exif_imagetype(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), finfo_open(), finfo_file(), finfo_close(), wp_is_heic_image_mime_type() |
!is_callable() && $mime === false && $magic !== false && isset($magic) | 91–101 | is_callable(), getimagesize(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), finfo_open(), finfo_file(), finfo_close(), wp_is_heic_image_mime_type() |
is_callable() && $mime === false && $magic !== false && isset($magic) | 91–96 | is_callable(), exif_imagetype(), image_type_to_mime_type(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), finfo_open(), finfo_file(), finfo_close(), wp_is_heic_image_mime_type() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 130 | 16–101 | 18 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 131 | 16–101 | 18 | |
| 8.4 | 131 | 16–101 | 18 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 137 | 16–107 | 18 | |
| 8.2 | 137 | 16–107 | 18 | |
| 8.1 | 137 | 16–107 | 18 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 138 | 16–108 | 18 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 4
- bin2hex()
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- hex2bin()
- wp_is_heic_image_mime_type()Checks if a mime type is for a HEIC/HEIF image.
Used by · 5
- WP_Image_Editor_GD::load()Loads image from $this->file into new GD Resource.
- wp_check_filetype_and_ext()Attempts to determine the real file type of a file.
- wp_get_avif_info()Extracts meta information about an AVIF file: width, height, bit depth, and number of channels.
- wp_get_webp_info()Extracts meta information about a WebP file: width, height, and type.
- wp_getimagesize()Allows PHP's getimagesize() to be debuggable when necessary.
Source code
function wp_get_image_mime( $file ) { /* * Use exif_imagetype() to check the mimetype if available or fall back to * getimagesize() if exif isn't available. If either function throws an Exception * we assume the file could not be validated. */ try { if ( is_callable( 'exif_imagetype' ) ) { $imagetype = exif_imagetype( $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' ) ) { // Not using wp_getimagesize() here to avoid an infinite loop. $imagesize = getimagesize( $file ); } else { $imagesize = @getimagesize( $file ); } $mime = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false; } else { $mime = false; } if ( false !== $mime ) { return $mime; } $magic = file_get_contents( $file, false, null, 0, 12 ); if ( false === $magic ) { return false; } /* * Add WebP fallback detection when image library doesn't support WebP. * Note: detection values come from LibWebP, see * https://github.com/webmproject/libwebp/blob/master/imageio/image_dec.c#L30 */ $magic = bin2hex( $magic ); if ( // RIFF. ( str_starts_with( $magic, '52494646' ) ) && // WEBP. ( 16 === strpos( $magic, '57454250' ) ) ) { $mime = 'image/webp'; } /** * Add AVIF fallback detection when image library doesn't support AVIF. * * Detection based on section 4.3.1 File-type box definition of the ISO/IEC 14496-12 * specification and the AV1-AVIF spec, see https://aomediacodec.github.io/av1-avif/v1.1.0.html#brands. */ // Divide the header string into 4 byte groups. $magic = str_split( $magic, 8 ); 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 ( wp_is_heic_image_mime_type( $mime_type ) ) { $mime = $mime_type; } }Changelog
Introduced in 4.7.1. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.