What are the best practices for handling memory allocation and optimization when working with image manipulation functions in PHP?

When working with image manipulation functions in PHP, it's important to be mindful of memory allocation and optimization to prevent memory leaks and improve performance. One way to handle this is by properly freeing up memory after processing images using functions like `imagedestroy()` to release allocated memory.

// Load an image
$image = imagecreatefromjpeg('image.jpg');

// Perform image manipulation operations here

// Free up memory
imagedestroy($image);