From 5aa00975ffc018c9f6680852a599bd438003f77c Mon Sep 17 00:00:00 2001
From: Andrea Lepori <alepori@student.ethz.ch>
Date: Fri, 28 Aug 2020 21:12:20 +0200
Subject: vertical concatanate images, resize images

---
 accounts/views.py | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/accounts/views.py b/accounts/views.py
index 9bdfacf..696391d 100644
--- a/accounts/views.py
+++ b/accounts/views.py
@@ -94,7 +94,7 @@ def personal(request):
                 filename = filename + ".jpg"
 
                 # encode in JPEG
-                im = Image.open(medic.vac_certificate.file)
+                im = Image.open(medic.health_care_certificate.file)
                 im_io = BytesIO()
                 im.save(im_io, 'JPEG', quality=90)
                 im_io.seek(0)
@@ -157,12 +157,15 @@ def personal(request):
                     im = Image.open(files.pop(0))
                     for f in files:
                         i = Image.open(f)
-                        dst = Image.new('RGB', (im.width + i.width, max(im.height, i.height)), (255, 255, 255))
+                        dst = Image.new('RGB', (max(im.width, i.width), im.height + i.height), (255, 255, 255))
                         dst.paste(im, (0, 0))
-                        dst.paste(i, (im.width, 0))
+                        dst.paste(i, (0, im.height))
                         im = dst
 
                 im_io = BytesIO()
+                # resize image if larger than max value
+                if im.height > 16383:
+                    im = im.resize((round(im.width/im.height*16383), 16383))
                 # compress image in WEBP
                 im.save(im_io, 'WEBP', quality=50, method=4)
                 medic.vac_certificate.save(
@@ -171,6 +174,9 @@ def personal(request):
             except UnidentifiedImageError:
                 error = True
                 error_text = "Il file non è un immagine valida"
+            except IOError:
+                error = True
+                error_text = "Il file è un immagine troppo grande"
 
         if "health_care_certificate" in request.FILES:
             files = request.FILES.getlist('health_care_certificate')
@@ -183,12 +189,15 @@ def personal(request):
                     im = Image.open(files.pop(0))
                     for f in files:
                         i = Image.open(f)
-                        dst = Image.new('RGB', (im.width + i.width, max(im.height, i.height)), (255, 255, 255))
+                        dst = Image.new('RGB', (max(im.width, i.width), im.height + i.height), (255, 255, 255))
                         dst.paste(im, (0, 0))
-                        dst.paste(i, (im.width, 0))
+                        dst.paste(i, (0, im.height))
                         im = dst
 
                 im_io = BytesIO()
+                # resize image if larger than max value
+                if im.height > 16383:
+                    im = im.resize((round(im.width/im.height*16383), 16383))
                 # compress image in WEBP
                 im.save(im_io, 'WEBP', quality=50, method=4)
                 medic.health_care_certificate.save(
@@ -197,6 +206,9 @@ def personal(request):
             except UnidentifiedImageError:
                 error = True
                 error_text = "Il file non è un immagine valida"
+            except IOError:
+                error = True
+                error_text = "Il file è un immagine troppo grande"
 
         # user requested file delete
         if request.POST["delete_vac"] == 'vac':
-- 
cgit v1.2.1