WordPress Version: 4.0
/**
* Check if the attachment is an image.
*
* @since 2.1.0
*
* @param int $post_id Optional. Attachment ID. Default 0.
* @return bool Whether the attachment is an image.
*/
function wp_attachment_is_image($post_id = 0)
{
$post_id = (int) $post_id;
if (!$post = get_post($post_id)) {
return false;
}
if (!$file = get_attached_file($post->ID)) {
return false;
}
$ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false;
$image_exts = array('jpg', 'jpeg', 'jpe', 'gif', 'png');
if ('image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts)) {
return true;
}
return false;
}