What are some best practices for handling memory limits in PHP when working with image processing functions like imagecreatefromjpeg()?

When working with image processing functions like imagecreatefromjpeg() in PHP, it's important to be mindful of memory limits as processing large images can consume a significant amount of memory. To handle memory limits, you can increase the memory limit in your PHP configuration or use techniques like freeing up memory after processing each image.

// Increase memory limit
ini_set('memory_limit', '256M');

// Process image
$image = imagecreatefromjpeg('image.jpg');

// Free up memory
imagedestroy($image);