Check that images have valid dimensions (issue 4575)

Attempted fix for #4575.

Should we add a test case, or is that not necessary?