In the provided PHP script, what improvements can be made to optimize the image processing and color replacement process?

The current script processes each pixel of the image individually, which can be inefficient for large images. To optimize the image processing and color replacement process, we can utilize PHP's built-in image processing functions to perform batch operations on the image. This can significantly improve the performance and efficiency of the color replacement process.

<?php
// Load the image
$image = imagecreatefromjpeg('input.jpg');

// Define the colors to replace
$oldColor = imagecolorallocate($image, 255, 0, 0); // Old color (red)
$newColor = imagecolorallocate($image, 0, 255, 0); // New color (green)

// Replace the colors in the image
imagefilter($image, IMG_FILTER_COLORIZE, 0, 255, 0);

// Save the modified image
imagejpeg($image, 'output.jpg');

// Free up memory
imagedestroy($image);
?>