What are potential pitfalls when setting the memory limit in PHP for image processing?

Setting a memory limit in PHP for image processing is important to prevent memory exhaustion and potential crashes. However, setting the limit too low can result in out of memory errors, while setting it too high can lead to inefficient memory usage. It's essential to find a balance based on the size and complexity of the images being processed.

// Set memory limit based on image size and complexity
$memory_limit = calculateMemoryLimit($image_size, $complexity);
ini_set('memory_limit', $memory_limit);