What are the best practices for compressing and outputting images in PHP to reduce load times?

When outputting images in PHP, it's important to compress them to reduce load times. One way to achieve this is by using the `imagejpeg()` function in PHP to output JPEG images with a specified quality level. By setting the quality level to a lower value, you can reduce the file size of the image while maintaining an acceptable level of visual quality.

// Load the original image
$original_image = imagecreatefromjpeg('original.jpg');

// Create a new JPEG image with reduced quality
imagejpeg($original_image, 'output.jpg', 50);

// Output the compressed image
header('Content-Type: image/jpeg');
readfile('output.jpg');

// Clean up
imagedestroy($original_image);