Title: wp_get_image_mime
Published: June 15, 2017
Last modified: February 24, 2026

---

# wp_get_image_mime( string $file ): string|false

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_get_image_mime/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_get_image_mime/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_get_image_mime/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_get_image_mime/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_get_image_mime/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_get_image_mime/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_get_image_mime/?output_format=md#wp--skip-link--target)

Returns the real mime type of an image file.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_get_image_mime/?output_format=md#description)󠁿

This depends on exif_imagetype() or getimagesize() to determine real mime types.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_get_image_mime/?output_format=md#parameters)󠁿

 `$file`stringrequired

Full path to the file.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_get_image_mime/?output_format=md#return)󠁿

 string|false The actual mime type or false if the type cannot be determined.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_get_image_mime/?output_format=md#source)󠁿

    ```php
    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 ) ) {
    						$mime = $mime_type;
    					}
    				}
    			}
    		}
    	} catch ( Exception $e ) {
    		$mime = false;
    	}

    	return $mime;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/functions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/functions.php#L3335)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/functions.php#L3335-L3425)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_get_image_mime/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_is_heic_image_mime_type()](https://developer.wordpress.org/reference/functions/wp_is_heic_image_mime_type/)`wp-includes/functions.php` |

Checks if a mime type is for a HEIC/HEIF image.

  |

| Used by | Description | 
| [wp_get_avif_info()](https://developer.wordpress.org/reference/functions/wp_get_avif_info/)`wp-includes/media.php` |

Extracts meta information about an AVIF file: width, height, bit depth, and number of channels.

  | 
| [wp_get_webp_info()](https://developer.wordpress.org/reference/functions/wp_get_webp_info/)`wp-includes/media.php` |

Extracts meta information about a WebP file: width, height, and type.

  | 
| [wp_getimagesize()](https://developer.wordpress.org/reference/functions/wp_getimagesize/)`wp-includes/media.php` |

Allows PHP’s getimagesize() to be debuggable when necessary.

  | 
| [wp_check_filetype_and_ext()](https://developer.wordpress.org/reference/functions/wp_check_filetype_and_ext/)`wp-includes/functions.php` |

Attempts to determine the real file type of a file.

  | 
| [WP_Image_Editor_GD::load()](https://developer.wordpress.org/reference/classes/wp_image_editor_gd/load/)`wp-includes/class-wp-image-editor-gd.php` |

Loads image from $this->file into new GD Resource.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_get_image_mime/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.7.0](https://developer.wordpress.org/reference/since/6.7.0/) | Added support for HEIC images. | 
| [6.5.0](https://developer.wordpress.org/reference/since/6.5.0/) | Added support for AVIF images. | 
| [5.8.0](https://developer.wordpress.org/reference/since/5.8.0/) | Added support for WebP images. | 
| [4.7.1](https://developer.wordpress.org/reference/since/4.7.1/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_image_mime%2F)
before being able to contribute a note or feedback.