aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2022-12-28 10:18:32 -0500
committerBobby <[email protected]>2022-12-28 10:18:32 -0500
commit86a0316c685ece0e7a0c2ab9433cfddd3ba02501 (patch)
tree1369af2299aa9a259f84903938965475019d109e
parent4806ff0a273eb8c20ef4fd86a283b7700e1f864d (diff)
downloadthatcomputerscientist-86a0316c685ece0e7a0c2ab9433cfddd3ba02501.tar.xz
thatcomputerscientist-86a0316c685ece0e7a0c2ab9433cfddd3ba02501.zip
I just added image resizing capabilities to ignis bruh
-rw-r--r--ignis/views.py29
-rw-r--r--templates/blog/home.html2
2 files changed, 28 insertions, 3 deletions
diff --git a/ignis/views.py b/ignis/views.py
index 211346eb..f9d27500 100644
--- a/ignis/views.py
+++ b/ignis/views.py
@@ -37,14 +37,39 @@ def tex(request):
@csrf_exempt
def post_image(request, post_id):
- post_id = post_id.replace('.png', '')
pi = Post.objects.get(id=post_id).post_image
+ size = request.GET.get('s')
if not pi:
return HttpResponse('No image found!', status=404)
# convert base64 data src to image
image = base64.b64decode(pi.split(',')[1])
- return HttpResponse(image, content_type='image/png')
+
+ # if size is specified, resize image
+ try:
+ size = int(size)
+ image = Image.open(BytesIO(image))
+
+ # resize width to size, compute height
+ width, height = image.size
+ height = int(height * (size / width))
+ width = size
+
+ print("Resizing image to {}x{}".format(width, height))
+
+ # resize image
+ image = image.resize((width, height), Image.ANTIALIAS)
+
+ # Convert back to gif and return
+ output = BytesIO()
+ image.save(output, format='GIF')
+
+ return HttpResponse(output.getvalue(), content_type='image/gif')
+ except:
+ # Convert back to gif and return
+ output = BytesIO()
+ output.write(image)
+ return HttpResponse(output.getvalue(), content_type='image/gif')
@csrf_exempt
def get_image(request, post_id, image_name):
diff --git a/templates/blog/home.html b/templates/blog/home.html
index b91dcb77..213ba4b1 100644
--- a/templates/blog/home.html
+++ b/templates/blog/home.html
@@ -64,7 +64,7 @@
</p>
<p style="text-align: justify; font-size: 13px; margin-bottom: 0px;">
<span>
- <img src="{% url 'ignis:post_image' post.id %}.png" alt="Cover image for {{ post.title }}" style="float: left; margin-right: 10px; margin-bottom: 10px; width: 200px; height: auto;">
+ <img src="{% url 'ignis:post_image' post.id %}?s=200" alt="Cover image for {{ post.title }}" style="float: left; margin-right: 10px; margin-bottom: 10px; width: 200px; height: auto;">
</span>
<span>
{{ post.excerpt | safe }}