How can the "Cannot modify header information" warning in PHP be resolved when working with image output?

When working with image output in PHP, the "Cannot modify header information" warning can be resolved by ensuring that no output is sent to the browser before setting headers using the header() function. To avoid this warning, make sure to call the header() function before any output is generated, including whitespace.

<?php
ob_start();
header('Content-Type: image/jpeg');
$image = imagecreatefromjpeg('image.jpg');
imagejpeg($image);
imagedestroy($image);
ob_end_flush();
?>