What are some potential security risks of directly sending images as byte streams to the browser in PHP?

Sending images as byte streams directly to the browser in PHP can expose your application to potential security risks such as cross-site scripting attacks or malicious file execution. To mitigate these risks, it is recommended to validate user input, sanitize the image data, and set appropriate content headers to prevent any unintended execution of the image data.

// Example of sending an image as byte stream with proper headers
$imageData = // Retrieve image data from a secure source

header('Content-Type: image/jpeg'); // Set appropriate content type header
header('Content-Length: ' . strlen($imageData)); // Set content length header

echo $imageData; // Output the image data to the browser
exit(); // Terminate script execution after sending the image