From dae9aac1bcaeaab86649051cd0ab56a9fecbf63c Mon Sep 17 00:00:00 2001 From: Bobby Date: Fri, 18 Nov 2022 15:12:23 -0500 Subject: rename module --- ignis/__init__.py | 0 ignis/admin.py | 3 +++ ignis/apps.py | 6 ++++++ ignis/migrations/__init__.py | 0 ignis/models.py | 3 +++ ignis/tests.py | 3 +++ ignis/urls.py | 8 ++++++++ ignis/views.py | 39 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 62 insertions(+) create mode 100644 ignis/__init__.py create mode 100644 ignis/admin.py create mode 100644 ignis/apps.py create mode 100644 ignis/migrations/__init__.py create mode 100644 ignis/models.py create mode 100644 ignis/tests.py create mode 100644 ignis/urls.py create mode 100644 ignis/views.py (limited to 'ignis') diff --git a/ignis/__init__.py b/ignis/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ignis/admin.py b/ignis/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/ignis/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/ignis/apps.py b/ignis/apps.py new file mode 100644 index 00000000..8c2eea77 --- /dev/null +++ b/ignis/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class IgnisConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'ignis' diff --git a/ignis/migrations/__init__.py b/ignis/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ignis/models.py b/ignis/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/ignis/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/ignis/tests.py b/ignis/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/ignis/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/ignis/urls.py b/ignis/urls.py new file mode 100644 index 00000000..4e931c4d --- /dev/null +++ b/ignis/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from . import views + +app_name = 'ignis' +urlpatterns = [ + path('tex/', views.tex, name='tex'), + path('post_image//', views.post_image, name='post_image'), +] diff --git a/ignis/views.py b/ignis/views.py new file mode 100644 index 00000000..ffbce848 --- /dev/null +++ b/ignis/views.py @@ -0,0 +1,39 @@ +from django.shortcuts import render +from PIL import Image +from io import BytesIO +from django.http import HttpResponse +from django.views.decorators.csrf import csrf_exempt +import base64 +from blog.models import Post + +# Create your views here. +@csrf_exempt +def tex(request): + # get expression from request query + expression = request.GET.get('expr').replace('"', '').strip() + if not expression: + return HttpResponse('No expression provided!', status=400) + + import requests + + image = requests.get('https://latex.codecogs.com/png.image?%5Cinline%20%5Clarge%20%5Cdpi%7B300%7D%5Cbg%7Btransparent%7D' + expression).content + + # Image is a transparent GIF with black text. Invert the colors. + image = Image.open(BytesIO(image)) + image = image.convert('RGBA') + image = Image.eval(image, lambda x: 255 - x) + + # Convert back to gif and return + output = BytesIO() + image.save(output, format='GIF') + return HttpResponse(output.getvalue(), content_type='image/gif') + +@csrf_exempt +def post_image(request, post_id): + pi = Post.objects.get(id=post_id).post_image + 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') -- cgit v1.2.3