Browse Source

Add caching and smarter resizing to the Libravatar endpoint

Danielle McLean 9 months ago
parent
commit
e94a856a08
Signed by: Danielle McLean <dani@00dani.me> GPG Key ID: 8EB789DDF3ABD240
1 changed files with 9 additions and 3 deletions
  1. 9
    3
      users/views.py

+ 9
- 3
users/views.py View File

@@ -1,4 +1,5 @@
1 1
 from django.http import HttpResponse, HttpResponseRedirect
2
+from django.views.decorators.cache import cache_page
2 3
 from PIL import Image
3 4
 
4 5
 from lemoncurry import utils
@@ -12,6 +13,7 @@ def try_libravatar_org(hash, get):
12 13
     return HttpResponseRedirect(url)
13 14
 
14 15
 
16
+@cache_page(60 * 15)
15 17
 def libravatar(request, hash):
16 18
     g = request.GET
17 19
     size = g.get('s', g.get('size', 80))
@@ -42,8 +44,12 @@ def libravatar(request, hash):
42 44
         return try_libravatar_org(hash, g)
43 45
 
44 46
     im = Image.open(user.avatar)
45
-    im_resized = im.resize((size, size))
47
+    image_type = im.format
48
+    natural_size = min(im.size)
46 49
 
47
-    response = HttpResponse(content_type='image/'+im.format.lower())
48
-    im_resized.save(response, im.format)
50
+    im = im.crop((0, 0, natural_size, natural_size))
51
+    im = im.resize((size, size), resample=Image.HAMMING)
52
+
53
+    response = HttpResponse(content_type='image/'+image_type.lower())
54
+    im.save(response, image_type)
49 55
     return response

Loading…
Cancel
Save