From 25382c9db6bf0f47623ccc3e34c0f70065372ee9 Mon Sep 17 00:00:00 2001 From: souliane Date: Thu, 19 Jun 2014 20:33:42 +0200 Subject: [PATCH] plugin XEP-0054: crop uploaded avatar to get a square --- src/plugins/plugin_xep_0054.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/plugins/plugin_xep_0054.py b/src/plugins/plugin_xep_0054.py index e7b55b48..0bebbac8 100755 --- a/src/plugins/plugin_xep_0054.py +++ b/src/plugins/plugin_xep_0054.py @@ -46,6 +46,7 @@ except ImportError: 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 @@ class XEP_0054(object): 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')