changeset 1081:5d89fecdf667

plugin XEP-0054: crop uploaded avatar to get a square
author souliane <souliane@mailoo.org>
date Thu, 19 Jun 2014 20:33:42 +0200
parents f25ec9fd7cc4
children 246712d2e7bc
files src/plugins/plugin_xep_0054.py
diffstat 1 files changed, 14 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0054.py	Thu Jun 19 19:20:52 2014 +0200
+++ b/src/plugins/plugin_xep_0054.py	Thu Jun 19 20:33:42 2014 +0200
@@ -46,6 +46,7 @@
     from wokkel.subprotocols import XMPPHandler
 
 AVATAR_PATH = "avatars"
+AVATAR_DIM = (64, 64)
 
 IQ_GET = '/iq[@type="get"]'
 NS_VCARD = 'vcard-temp'
@@ -242,8 +243,19 @@
         except IOError:
             return Failure(exceptions.DataError("Can't open image"))
 
-        if img.size != (64, 64):
-            img = img.resize((64, 64))
+        if img.size != AVATAR_DIM:
+            img.thumbnail(AVATAR_DIM, Image.ANTIALIAS)
+            if img.size[0] != img.size[1]:  # we need to crop first
+                left, upper = (0, 0)
+                right, lower = img.size
+                offset = abs(right - lower) / 2
+                if right == min(img.size):
+                    upper += offset
+                    lower -= offset
+                else:
+                    left += offset
+                    right -= offset
+                img = img.crop((left, upper, right, lower))
         img_buf = StringIO()
         img.save(img_buf, 'PNG')