summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Manfre2022-11-20 13:18:14 -0500
committerGitHub2022-11-20 11:18:14 -0700
commit75e73859688c8893edc2650d12b10e3005d9db76 (patch)
tree3cfe2779b0faa16dab6f9c36b0a4f11347a6365d
parenta43ccde8d99afb0cf58dec07e79dcc502ab790eb (diff)
downloadtakahe-75e73859688c8893edc2650d12b10e3005d9db76.tar.gz
takahe-75e73859688c8893edc2650d12b10e3005d9db76.tar.bz2
takahe-75e73859688c8893edc2650d12b10e3005d9db76.zip
Fix resize of image on upload of Identity profile pics
-rw-r--r--users/views/settings/profile.py23
1 files changed, 15 insertions, 8 deletions
diff --git a/users/views/settings/profile.py b/users/views/settings/profile.py
index 3b16d69..49fcc45 100644
--- a/users/views/settings/profile.py
+++ b/users/views/settings/profile.py
@@ -42,6 +42,13 @@ class ProfilePage(FormView):
"image": self.request.identity.image and self.request.identity.image.url,
}
+ def resize_image(self, image: File, *, size: tuple[int, int]) -> File:
+ with Image.open(image) as img:
+ resized_image = ImageOps.fit(img, size)
+ new_image_bytes = io.BytesIO()
+ resized_image.save(new_image_bytes, format=img.format)
+ return File(new_image_bytes)
+
def form_valid(self, form):
# Update identity name and summary
self.request.identity.name = form.cleaned_data["name"]
@@ -50,14 +57,14 @@ class ProfilePage(FormView):
icon = form.cleaned_data.get("icon")
image = form.cleaned_data.get("image")
if isinstance(icon, File):
- resized_image = ImageOps.fit(Image.open(icon), (400, 400))
- new_icon_bytes = io.BytesIO()
- resized_image.save(new_icon_bytes, format=icon.format)
- self.request.identity.icon.save(icon.name, File(new_icon_bytes))
+ self.request.identity.icon.save(
+ icon.name,
+ self.resize_image(icon, size=(400, 400)),
+ )
if isinstance(image, File):
- resized_image = ImageOps.fit(Image.open(image), (400, 400))
- new_image_bytes = io.BytesIO()
- resized_image.save(new_image_bytes, format=image.format)
- self.request.identity.image.save(image.name, File(new_image_bytes))
+ self.request.identity.image.save(
+ image.name,
+ self.resize_image(image, size=(400, 400)),
+ )
self.request.identity.save()
return redirect(".")