How can one efficiently check if a URL points to a valid image file using PHP functions?

To efficiently check if a URL points to a valid image file using PHP functions, you can use the getimagesize() function. This function returns an array with information about the image, including its dimensions and MIME type. By checking if the function returns false or an array with valid image information, you can determine if the URL points to a valid image file.

$url = 'https://example.com/image.jpg';
$image_info = getimagesize($url);

if ($image_info !== false && in_array($image_info['mime'], ['image/jpeg', 'image/png', 'image/gif'])) {
    echo 'Valid image file';
} else {
    echo 'Not a valid image file';
}