How can the HTTP header received by the browser affect the output of PHP scripts for generating images?

When a browser sends an HTTP header that specifies the content type as an image, PHP scripts for generating images may not function correctly as the browser expects binary image data. To solve this issue, you can set the content type header in your PHP script before outputting the image data. This ensures that the browser interprets the output correctly as an image.

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

// Generate your image here
$image = imagecreate(200, 200);
$bgColor = imagecolorallocate($image, 255, 255, 255);
$textColor = imagecolorallocate($image, 0, 0, 0);
imagestring($image, 4, 50, 50, 'Hello, World!', $textColor);

// Output the image
imagejpeg($image);

// Free up memory
imagedestroy($image);
?>