What potential pitfalls should be considered when outputting binary data as an image in PHP?

When outputting binary data as an image in PHP, potential pitfalls to consider include ensuring proper content-type headers are set, handling errors that may occur during image creation, and preventing injection attacks by validating input data. It's important to sanitize and validate the binary data before outputting it as an image to prevent security vulnerabilities.

<?php
// Set the content-type header to image
header('Content-Type: image/jpeg');

// Validate and sanitize binary data
$binaryData = $_POST['binary_data']; // Example: $_POST['binary_data']
$filteredData = filter_var($binaryData, FILTER_SANITIZE_STRING);

// Output the binary data as an image
echo $filteredData;
?>