Well, the really correct one would be to take the current memory usage and add memory usage from the largest image + resized version of it -- maybe with the same 1MB margin.

   $memoryNeeded = current usage + 1MB + width*height*6 + resized width*height*6