What are the 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 achieve this is by using the `imagecopyresampled()` function instead of `imagecopyresized()` as it resamples the image to a new size without losing quality. Additionally, you can free up memory by using `imagedestroy()` to release the memory allocated for the original image after resizing.

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

// Create a new image with the desired dimensions
$new_width = 200;
$new_height = 150;
$new_image = imagecreatetruecolor($new_width, $new_height);

// Resize the original image to fit the new dimensions
imagecopyresampled($new_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, imagesx($original_image), imagesy($original_image));

// Save or output the resized image
imagejpeg($new_image, 'resized.jpg');

// Free up memory by destroying the images
imagedestroy($original_image);
imagedestroy($new_image);