What are the best practices for handling image output in PHP, specifically when using JPGraph?

When handling image output in PHP, especially when using JPGraph, it is important to set the appropriate content type header before outputting the image. This ensures that the browser interprets the data as an image and displays it correctly. Additionally, it is good practice to use ob_clean() before sending the image data to prevent any unwanted output before the image.

// Set the content type header
header("Content-type: image/png");

// Create JPGraph image object
$graph = new Graph(400, 300);

// Set up the graph here...

// Send the image data
ob_clean();
$graph->Stroke();