Rewrites resizing logic.

Adds support for constraining by _h_eight or _w_idth (in addition to _s_quare).
Adds checks preventing image enlargement.