Improve image resize logic