Optimize the image upload function