W3cubDocs

/WordPress

wp_get_image_mime( string $file )

Returns the real mime type of an image file.

Description

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

Parameters

$file

(string) (Required) Full path to the file.

Return

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

Source

File: wp-includes/functions.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 avaialbe. 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' ) ) {
			$imagesize = @getimagesize( $file );
			$mime      = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false;
		} else {
			$mime = false;
		}
	} catch ( Exception $e ) {
		$mime = false;
	}

	return $mime;
}

Changelog

Version Description
4.7.1 Introduced.

© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_get_image_mime