How can resource optimization be improved in the provided PHP code for image resizing?

The resource optimization in the provided PHP code for image resizing can be improved by using the PHP GD library functions to directly resize the image instead of creating multiple copies of the image. By resizing the image directly, we can reduce the amount of memory and processing power required to handle the image resizing operation.

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

// Get the original image dimensions
$original_width = imagesx($original_image);
$original_height = imagesy($original_image);

// Calculate the new dimensions for the resized image
$new_width = 200;
$new_height = ($original_height / $original_width) * $new_width;

// Create a new image resource for the resized image
$resized_image = imagecreatetruecolor($new_width, $new_height);

// Resize the original image to the new dimensions
imagecopyresampled($resized_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);

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

// Free up memory by destroying the image resources
imagedestroy($original_image);
imagedestroy($resized_image);