def _resize_avatar(self, image, size):
image = PIL.Image.open(io.BytesIO(image))
- # Convert RGBA images into RGB because JPEG doesn't support alpha-channels
- if image.mode == "RGBA":
- image = image.convert("RGB")
-
# Resize the image to the desired resolution (and make it square)
thumbnail = PIL.ImageOps.fit(image, (size, size), PIL.Image.ANTIALIAS)
# If writing out the image does not work with optimization,
# we try to write it out without any optimization.
try:
- thumbnail.save(f, "JPEG", optimize=True, quality=98)
+ thumbnail.save(f, image.format, optimize=True, quality=98)
except:
- thumbnail.save(f, "JPEG", quality=98)
+ thumbnail.save(f, image.format, quality=98)
return f.getvalue()
import datetime
import ldap
import logging
+import imghdr
import sshpubkeys
import tornado.web
return self.redirect(self.static_url("img/default-avatar.jpg"))
+ # Guess content type
+ type = imghdr.what(None, avatar)
+
# Set headers about content
- self.set_header("Content-Disposition", "inline; filename=\"%s.jpg\"" % account.uid)
- self.set_header("Content-Type", "image/jpeg")
+ self.set_header("Content-Disposition", "inline; filename=\"%s.%s\"" % (account.uid, type))
+ self.set_header("Content-Type", "image/%s" % type)
# Deliver payload
self.finish(avatar)