wppaste
WordPress

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:3335
Returns the real mime type of an image file.

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

Reads or writes the filesystem via file_get_contents().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
16–98

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 128.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • filesystemfilesystem accessfile_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.

WhenInstructionsCalls it makes
is_callable() && $mime !== false16is_callable(), exif_imagetype()
!is_callable() && $mime !== false20–25is_callable(), getimagesize()
is_callable() && $mime !== false20is_callable(), exif_imagetype(), image_type_to_mime_type()
is_callable() && $mime === false && $magic === false26is_callable(), exif_imagetype(), file_get_contents()
!is_callable() && $mime === false && $magic === false30–35is_callable(), getimagesize(), file_get_contents()
is_callable() && $mime === false && $magic === false30is_callable(), exif_imagetype(), image_type_to_mime_type(), file_get_contents()
is_callable() && $mime === false && $magic !== false && !isset($magic)39–45is_callable(), exif_imagetype(), file_get_contents(), bin2hex(), str_split()
!is_callable() && $mime === false && $magic !== false && !isset($magic)43–54is_callable(), getimagesize(), file_get_contents(), bin2hex(), str_split()
is_callable() && $mime === false && $magic !== false && !isset($magic)43–49is_callable(), exif_imagetype(), image_type_to_mime_type(), file_get_contents(), bin2hex(), str_split()
is_callable() && $mime === false && $magic !== false && isset($magic)47–51is_callable(), exif_imagetype(), file_get_contents(), bin2hex(), str_split(), hex2bin()
!is_callable() && $mime === false && $magic !== false && isset($magic)51–60is_callable(), getimagesize(), file_get_contents(), bin2hex(), str_split(), hex2bin()
is_callable() && $mime === false && $magic !== false && isset($magic)51–55is_callable(), exif_imagetype(), image_type_to_mime_type(), file_get_contents(), bin2hex(), str_split(), hex2bin()
15 further outcomes, up to 93 instructions
is_callable() && $mime === false && $magic !== false && isset($magic)54–58is_callable(), exif_imagetype(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin()
!is_callable() && $mime === false && $magic !== false && isset($magic)58–67is_callable(), getimagesize(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin()
is_callable() && $mime === false && $magic !== false && isset($magic)58–62is_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–64is_callable(), exif_imagetype(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin()
!is_callable() && $mime === false && $magic !== false && isset($magic)64–73is_callable(), getimagesize(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin()
is_callable() && $mime === false && $magic !== false && isset($magic)64–68is_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–70is_callable(), exif_imagetype(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin(), hex2bin()
!is_callable() && $mime === false && $magic !== false && isset($magic)70–79is_callable(), getimagesize(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin(), hex2bin()
is_callable() && $mime === false && $magic !== false && isset($magic)70–74is_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–76is_callable(), exif_imagetype(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), hex2bin()
!is_callable() && $mime === false && $magic !== false && isset($magic)76–85is_callable(), getimagesize(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), hex2bin()
is_callable() && $mime === false && $magic !== false && isset($magic)76–80is_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)84–89is_callable(), exif_imagetype(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), finfo_open(), finfo_file(), wp_is_heic_image_mime_type()
!is_callable() && $mime === false && $magic !== false && isset($magic)88–98is_callable(), getimagesize(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), finfo_open(), finfo_file(), wp_is_heic_image_mime_type()
is_callable() && $mime === false && $magic !== false && isset($magic)88–93is_callable(), exif_imagetype(), image_type_to_mime_type(), file_get_contents(), bin2hex(), str_split(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), hex2bin(), finfo_open(), finfo_file(), wp_is_heic_image_mime_type()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12716–98181 fewer instruction than PHP 8.5
8.512816–9818
8.412816–98186 fewer instructions than PHP 8.3
8.313416–10418
8.213416–10418
8.113416–104184 fewer instructions than PHP 7.4
7.413816–10818

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

Used by · 5

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 ); 					if ( PHP_VERSION_ID < 80100 ) { // finfo_close() has no effect as of PHP 8.1.						finfo_close( $fileinfo );					} 					if ( wp_is_heic_image_mime_type( $mime_type ) ) {

Changelog

Introduced in 4.7.1. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.7.0
Added support for HEIC images.from the docblock
6.5.0
Added support for AVIF images.from the docblock
5.8.0
Added support for WebP images.from the docblock
4.7.1
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 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.