better way of handling optional image encoding