How can memory management be optimized when working with images in PHP to prevent issues like corrupted files or excessive memory usage?

When working with images in PHP, memory management can be optimized by using functions like `imagecreatefromjpeg()` or `imagecreatefrompng()` to directly create image resources from files without loading the entire file into memory. Additionally, using functions like `imagedestroy()` to free up memory after processing images can prevent issues like corrupted files or excessive memory usage.

// Example of optimizing memory management when working with images in PHP
$image = imagecreatefromjpeg('example.jpg');

// Process the image

imagedestroy($image);