How can the getimagesize() function in PHP be used as an alternative method for verifying the content type of an image file?

When verifying the content type of an image file, relying solely on the file extension can be unreliable as it can be easily manipulated. Using the getimagesize() function in PHP can provide a more accurate way to determine the content type of an image file by examining its actual image data. This function returns an array containing the image's dimensions and MIME type, which can be used to verify the file's content type.

$image_info = getimagesize($image_path);
if ($image_info && in_array($image_info['mime'], array('image/jpeg', 'image/png', 'image/gif'))) {
    // File is an image of allowed type (JPEG, PNG, GIF)
    // Proceed with processing the image
} else {
    // File is not a valid image or of an allowed type
    // Handle the error accordingly
}