Are there specific PHP functions or libraries that can be used to check if a file is a valid image before allowing it to be uploaded?

When allowing users to upload images to a website, it is important to validate that the uploaded file is actually an image to prevent security vulnerabilities. One way to check if a file is a valid image is by using PHP functions like `getimagesize()` or `exif_imagetype()` to determine the image type based on its content. These functions can help verify that the uploaded file is indeed an image before allowing it to be processed further.

// Check if the uploaded file is a valid image
function is_valid_image($file) {
    $allowed_types = array(IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF);
    
    $image_type = exif_imagetype($file['tmp_name']);
    
    if(in_array($image_type, $allowed_types)) {
        return true;
    } else {
        return false;
    }
}

// Example usage
if(is_valid_image($_FILES['image'])) {
    // Process the uploaded image
    move_uploaded_file($_FILES['image']['tmp_name'], 'uploads/' . $_FILES['image']['name']);
    echo 'Image uploaded successfully!';
} else {
    echo 'Invalid image file. Please upload a valid image.';
}