comparison src/plugins/plugin_xep_0054.py @ 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 7b4600ad73ad
children 96fb74a4714d
comparison
equal deleted inserted replaced
1080:f25ec9fd7cc4 1081:5d89fecdf667
44 from twisted.words.protocols.xmlstream import XMPPHandler 44 from twisted.words.protocols.xmlstream import XMPPHandler
45 except ImportError: 45 except ImportError:
46 from wokkel.subprotocols import XMPPHandler 46 from wokkel.subprotocols import XMPPHandler
47 47
48 AVATAR_PATH = "avatars" 48 AVATAR_PATH = "avatars"
49 AVATAR_DIM = (64, 64)
49 50
50 IQ_GET = '/iq[@type="get"]' 51 IQ_GET = '/iq[@type="get"]'
51 NS_VCARD = 'vcard-temp' 52 NS_VCARD = 'vcard-temp'
52 VCARD_REQUEST = IQ_GET + '/vCard[@xmlns="' + NS_VCARD + '"]' # TODO: manage requests 53 VCARD_REQUEST = IQ_GET + '/vCard[@xmlns="' + NS_VCARD + '"]' # TODO: manage requests
53 54
240 try: 241 try:
241 img = Image.open(filepath) 242 img = Image.open(filepath)
242 except IOError: 243 except IOError:
243 return Failure(exceptions.DataError("Can't open image")) 244 return Failure(exceptions.DataError("Can't open image"))
244 245
245 if img.size != (64, 64): 246 if img.size != AVATAR_DIM:
246 img = img.resize((64, 64)) 247 img.thumbnail(AVATAR_DIM, Image.ANTIALIAS)
248 if img.size[0] != img.size[1]: # we need to crop first
249 left, upper = (0, 0)
250 right, lower = img.size
251 offset = abs(right - lower) / 2
252 if right == min(img.size):
253 upper += offset
254 lower -= offset
255 else:
256 left += offset
257 right -= offset
258 img = img.crop((left, upper, right, lower))
247 img_buf = StringIO() 259 img_buf = StringIO()
248 img.save(img_buf, 'PNG') 260 img.save(img_buf, 'PNG')
249 261
250 vcard_elt = vcard_set.addElement('vCard', NS_VCARD) 262 vcard_elt = vcard_set.addElement('vCard', NS_VCARD)
251 photo_elt = vcard_elt.addElement('PHOTO') 263 photo_elt = vcard_elt.addElement('PHOTO')