Correctly consider an image margin when resizing (see #6018)