Don't try to guess the file extension.

Remote images may end in query strings or simply have no extension.
Trying to extract the extension from a remote path may cause tempfile
creation to fail. ImageMagick should be able to figure out the image type
without an extension. So just sha1 the image path and use that.