How can PHP developers ensure that only valid image files are processed when working with image-related functions like generateThumbnailData()?

To ensure that only valid image files are processed when working with image-related functions like generateThumbnailData(), PHP developers can use the getimagesize() function to check the image file's MIME type before processing it. This function returns an array containing information about the image file, including its MIME type. By checking if the MIME type corresponds to a valid image format (e.g., JPEG, PNG, GIF), developers can prevent processing of potentially harmful files.

function generateThumbnailData($imagePath) {
    $imageInfo = getimagesize($imagePath);
    
    if ($imageInfo && in_array($imageInfo['mime'], ['image/jpeg', 'image/png', 'image/gif'])) {
        // Process the image file
        // Generate thumbnail data
    } else {
        // Invalid image file format
        // Handle error or return false
    }
}