Are there best practices for optimizing memory usage when resizing images in PHP scripts?

When resizing images in PHP scripts, it's important to optimize memory usage to prevent running out of memory or causing performance issues. One way to do this is by using the `imagecopyresampled()` function instead of `imagecopyresized()` as it resamples the image to reduce memory usage. Additionally, you can unset the original image after resizing to free up memory.

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

// Get the dimensions of the original image
$originalWidth = imagesx($originalImage);
$originalHeight = imagesy($originalImage);

// Create a new image with the desired dimensions
$newWidth = 200;
$newHeight = 150;
$newImage = imagecreatetruecolor($newWidth, $newHeight);

// Resize the original image to the new dimensions
imagecopyresampled($newImage, $originalImage, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);

// Output the resized image
imagejpeg($newImage, 'resized.jpg');

// Free up memory by unsetting the original and new images
imagedestroy($originalImage);
imagedestroy($newImage);