Optimize image dimension extraction