What are some potential pitfalls to be aware of when using PHP to manipulate image sizes?
One potential pitfall when using PHP to manipulate image sizes is the risk of losing image quality when resizing. To avoid this, it is important to properly set the compression quality when saving the resized image. This can be done by using the `imagejpeg()` function with the quality parameter set to a value between 0 and 100, where 100 represents the highest quality.
// Load the original image
$original_image = imagecreatefromjpeg('original.jpg');
// Resize the image to a new width and height
$new_width = 200;
$new_height = 150;
$resized_image = imagescale($original_image, $new_width, $new_height);
// Save the resized image with specified compression quality
$quality = 90; // Set compression quality between 0 and 100
imagejpeg($resized_image, 'resized.jpg', $quality);
// Free up memory
imagedestroy($original_image);
imagedestroy($resized_image);