wp_get_image_mime

Returns the real mime type of an image file.

Description

(string|false) wp_get_image_mime( (string) $file ); 

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

Returns (string|false)

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

Parameters (1)

0. $file (string)
Full path to the file.

Usage

  1. if ( !function_exists( 'wp_get_image_mime' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4. // Full path to the file. 
  5. $file = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_get_image_mime($file); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.php  
  1. function wp_get_image_mime( $file ) { 
  2. /** 
  3. * Use exif_imagetype() to check the mimetype if available or fall back to 
  4. * getimagesize() if exif isn't avaialbe. If either function throws an Exception 
  5. * we assume the file could not be validated. 
  6. */ 
  7. try { 
  8. if ( is_callable( 'exif_imagetype' ) ) { 
  9. $imagetype = exif_imagetype( $file ); 
  10. $mime = ( $imagetype ) ? image_type_to_mime_type( $imagetype ) : false; 
  11. } elseif ( function_exists( 'getimagesize' ) ) { 
  12. $imagesize = getimagesize( $file ); 
  13. $mime = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false; 
  14. } else { 
  15. $mime = false; 
  16. } catch ( Exception $e ) { 
  17. $mime = false; 
  18.  
  19. return $mime;