aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2022-12-28 10:40:33 -0500
committerBobby <[email protected]>2022-12-28 10:40:33 -0500
commit1421b7737a17b06ceeea532e4911548fac5bdd3e (patch)
treee90b16b105d104fde6f9170a8aac091325c06bea
parentb4980e3758186ee5ef5bc275d641b4c35b60e172 (diff)
downloadthatcomputerscientist-1421b7737a17b06ceeea532e4911548fac5bdd3e.tar.xz
thatcomputerscientist-1421b7737a17b06ceeea532e4911548fac5bdd3e.zip
Fixing ignis image return format by moving size in url
-rw-r--r--ignis/urls.py2
-rw-r--r--ignis/views.py19
-rw-r--r--templates/blog/home.html2
-rw-r--r--templates/blog/post.html2
4 files changed, 14 insertions, 11 deletions
diff --git a/ignis/urls.py b/ignis/urls.py
index 44ed5de1..94de2803 100644
--- a/ignis/urls.py
+++ b/ignis/urls.py
@@ -4,7 +4,7 @@ from . import views
app_name = 'ignis'
urlpatterns = [
path('/tex', views.tex, name='tex'),
- path('/post_image/<str:post_id>', views.post_image, name='post_image'),
+ path('/post_image/<int:size>/<str:post_id>', views.post_image, name='post_image'),
path('/upload', views.upload_image, name='upload_image'),
path('/image/<post_id>/<image_name>', views.get_image, name='get_image'),
path('/cover/<str:repository>', views.cover_image, name='cover_image'),
diff --git a/ignis/views.py b/ignis/views.py
index f9d27500..fd127d55 100644
--- a/ignis/views.py
+++ b/ignis/views.py
@@ -36,27 +36,30 @@ def tex(request):
return HttpResponse(output.getvalue(), content_type='image/gif')
@csrf_exempt
-def post_image(request, post_id):
+def post_image(request, size, post_id):
+ post_id = post_id.replace('.gif', '')
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])
- # if size is specified, resize image
- try:
- size = int(size)
+ size = int(size)
+ if size != 0:
image = Image.open(BytesIO(image))
+
+ # set min and max size
+ if size < 100:
+ size = 100
+ elif size > 1000:
+ size = 1000
# 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)
@@ -65,7 +68,7 @@ def post_image(request, post_id):
image.save(output, format='GIF')
return HttpResponse(output.getvalue(), content_type='image/gif')
- except:
+ else:
# Convert back to gif and return
output = BytesIO()
output.write(image)
diff --git a/templates/blog/home.html b/templates/blog/home.html
index 213ba4b1..44c931ae 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 %}?s=200" 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' '200' post.id %}.gif" alt="Cover image for {{ post.title }}" style="float: left; margin-right: 10px; margin-bottom: 10px; width: 200px; height: auto;">
</span>
<span>
{{ post.excerpt | safe }}
diff --git a/templates/blog/post.html b/templates/blog/post.html
index 2f89fd9a..fdbc0143 100644
--- a/templates/blog/post.html
+++ b/templates/blog/post.html
@@ -8,7 +8,7 @@
<article>
{% load subdomain %}
<div class="article-cover">
- <img src="{% url 'ignis:post_image' post.id %}?s=720" alt="Cover Image" style="width: 720px; margin: 0 auto; display: block;">
+ <img src="{% url 'ignis:post_image' '720' post.id %}.gif" alt="Cover Image" style="width: 720px; margin: 0 auto; display: block;">
</div>
<h1 style="margin-bottom: 12px; font-size: 2rem;">{{ post.title }}</h1>
<p style="line-height: 1.6em;">