diff options
| -rw-r--r-- | ignis/views.py | 29 | ||||
| -rw-r--r-- | templates/blog/home.html | 2 |
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 }} |
