What are the potential pitfalls of not using the correct compression settings for JPEG files in PHP?

Using incorrect compression settings for JPEG files in PHP can result in larger file sizes, slower loading times, and reduced image quality. To solve this issue, it is important to adjust the compression level to find the right balance between file size and image quality.

// Set the compression quality to a value between 0 and 100
$compression_quality = 80;

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

// Save the image with the specified compression quality
imagejpeg($image, 'image_compressed.jpg', $compression_quality);

// Free up memory
imagedestroy($image);