wp_get_image_mime

The timeline below displays how wordpress function wp_get_image_mime has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.5

/**
 * Returns the real mime type of an image file.
 *
 * This depends on exif_imagetype() or getimagesize() to determine real mime types.
 *
 * @since 4.7.1
 * @since 5.8.0 Added support for WebP images.
 * @since 6.5.0 Added support for AVIF images.
 *
 * @param string $file Full path to the file.
 * @return string|false The actual mime type or false if the type cannot be determined.
 */
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 (str_starts_with($magic, '52494646') && 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]) && ('avif' === hex2bin($magic[2]) || 'avis' === hex2bin($magic[2]))) {
            $mime = 'image/avif';
        }
    } catch (Exception $e) {
        $mime = false;
    }
    return $mime;
}

WordPress Version: 6.4

/**
 * Returns the real mime type of an image file.
 *
 * This depends on exif_imagetype() or getimagesize() to determine real mime types.
 *
 * @since 4.7.1
 * @since 5.8.0 Added support for WebP images.
 *
 * @param string $file Full path to the file.
 * @return string|false The actual mime type or false if the type cannot be determined.
 */
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 (str_starts_with($magic, '52494646') && 16 === strpos($magic, '57454250')) {
            $mime = 'image/webp';
        }
    } catch (Exception $e) {
        $mime = false;
    }
    return $mime;
}

WordPress Version: 6.3

/**
 * Returns the real mime type of an image file.
 *
 * This depends on exif_imagetype() or getimagesize() to determine real mime types.
 *
 * @since 4.7.1
 * @since 5.8.0 Added support for WebP images.
 *
 * @param string $file Full path to the file.
 * @return string|false The actual mime type or false if the type cannot be determined.
 */
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 {
                // phpcs:ignore WordPress.PHP.NoSilencedErrors
                $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 (str_starts_with($magic, '52494646') && 16 === strpos($magic, '57454250')) {
            $mime = 'image/webp';
        }
    } catch (Exception $e) {
        $mime = false;
    }
    return $mime;
}

WordPress Version: 6.1

/**
 * Returns the real mime type of an image file.
 *
 * This depends on exif_imagetype() or getimagesize() to determine real mime types.
 *
 * @since 4.7.1
 * @since 5.8.0 Added support for WebP images.
 *
 * @param string $file Full path to the file.
 * @return string|false The actual mime type or false if the type cannot be determined.
 */
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 {
                // phpcs:ignore WordPress.PHP.NoSilencedErrors
                $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 (0 === strpos($magic, '52494646') && 16 === strpos($magic, '57454250')) {
            $mime = 'image/webp';
        }
    } catch (Exception $e) {
        $mime = false;
    }
    return $mime;
}

WordPress Version: 5.8

/**
 * Returns the real mime type of an image file.
 *
 * This depends on exif_imagetype() or getimagesize() to determine real mime types.
 *
 * @since 4.7.1
 * @since 5.8.0 Added support for WebP images.
 *
 * @param string $file Full path to the file.
 * @return string|false The actual mime type or false if the type cannot be determined.
 */
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')) {
            // 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 {
                // phpcs:ignore WordPress.PHP.NoSilencedErrors
                $imagesize = @getimagesize($file);
            }
            $mime = isset($imagesize['mime']) ? $imagesize['mime'] : false;
        } else {
            $mime = false;
        }
        if (false !== $mime) {
            return $mime;
        }
        $handle = fopen($file, 'rb');
        if (false === $handle) {
            return false;
        }
        $magic = fread($handle, 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 (0 === strpos($magic, '52494646') && 16 === strpos($magic, '57454250')) {
            $mime = 'image/webp';
        }
        fclose($handle);
    } catch (Exception $e) {
        $mime = false;
    }
    return $mime;
}

WordPress Version: 5.7

/**
 * Returns the real mime type of an image file.
 *
 * This depends on exif_imagetype() or getimagesize() to determine real mime types.
 *
 * @since 4.7.1
 *
 * @param string $file Full path to the file.
 * @return string|false The actual mime type or false if the type cannot be determined.
 */
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 = wp_getimagesize($file);
            $mime = isset($imagesize['mime']) ? $imagesize['mime'] : false;
        } else {
            $mime = false;
        }
    } catch (Exception $e) {
        $mime = false;
    }
    return $mime;
}

WordPress Version: 5.3

/**
 * Returns the real mime type of an image file.
 *
 * This depends on exif_imagetype() or getimagesize() to determine real mime types.
 *
 * @since 4.7.1
 *
 * @param string $file Full path to the file.
 * @return string|false The actual mime type or false if the type cannot be determined.
 */
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;
}

WordPress Version: 4.8

/**
 * Returns the real mime type of an image file.
 *
 * This depends on exif_imagetype() or getimagesize() to determine real mime types.
 *
 * @since 4.7.1
 *
 * @param string $file Full path to the file.
 * @return string|false The actual mime type or false if the type cannot be determined.
 */
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;
}

WordPress Version: .17

/**
 * Returns the real mime type of an image file.
 *
 * This depends on exif_imagetype() or getimagesize() to determine real mime types.
 *
 * @since 4.7.1
 *
 * @param string $file Full path to the file.
 * @return string|false The actual mime type or false if the type cannot be determined.
 */
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')) {
            $mime = image_type_to_mime_type(exif_imagetype($file));
        } elseif (function_exists('getimagesize')) {
            $imagesize = getimagesize($file);
            $mime = isset($imagesize['mime']) ? $imagesize['mime'] : false;
        } else {
            $mime = false;
        }
    } catch (Exception $e) {
        $mime = false;
    }
    return $mime;
}