What are some common functions in PHP for optimizing JPEG compression in images?

When dealing with images in PHP, it's important to optimize JPEG compression to reduce file size without sacrificing image quality. This can be achieved using functions like imagecreatefromjpeg(), imagejpeg(), and imagecopyresampled() to resize and compress JPEG images efficiently.

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

// Create a new image with desired dimensions for optimization
$optimized_image = imagecreatetruecolor($new_width, $new_height);

// Copy and resize the original image to the optimized image
imagecopyresampled($optimized_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);

// Save the optimized image as a new JPEG file
imagejpeg($optimized_image, 'optimized.jpg', 80); // 80 is the quality parameter (0-100)