How can the imagejpeg() function be utilized to directly output image data instead of saving it to a file in PHP?
To output image data directly instead of saving it to a file in PHP using the imagejpeg() function, you can make use of output buffering. By capturing the image data in a buffer and then directly outputting it, you can avoid saving the image to a file on the server. This method is useful when you want to display images dynamically without the need to save them as files.
<?php
// Create a blank image
$image = imagecreatetruecolor(200, 200);
// Set the header to indicate that the content is an image
header('Content-Type: image/jpeg');
// Start output buffering
ob_start();
// Output the image data directly
imagejpeg($image);
// End output buffering and flush the output
ob_end_flush();
// Free up memory
imagedestroy($image);
?>