Enforce maximum dimensions on image.

From #6470.
Could we use current memory usage here?