summaryrefslogtreecommitdiff
path: root/cgi-bin
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2019-02-16 15:21:19 +0000
committerDaniel Silverstone <dsilvers@digital-scurf.org>2019-02-16 15:21:53 +0000
commit05609efbb88bce492605d720667d7111c379115a (patch)
treeb61f63cd48374900219a3ee1620f36e7d82d53e1 /cgi-bin
parente85ed06c23df3049dee8ae8f31f295b01170411e (diff)
downloadnetsurf-test-05609efbb88bce492605d720667d7111c379115a.tar.gz
netsurf-test-05609efbb88bce492605d720667d7111c379115a.tar.bz2
CGI: Add image.cgi
Image generator CGI, supports: width=xxx height=xxx format=xxx Formats supported include: png, jpeg, bmp, ico, gif, webp Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'cgi-bin')
-rwxr-xr-xcgi-bin/image.cgi54
1 files changed, 54 insertions, 0 deletions
diff --git a/cgi-bin/image.cgi b/cgi-bin/image.cgi
new file mode 100755
index 0000000..b128c3a
--- /dev/null
+++ b/cgi-bin/image.cgi
@@ -0,0 +1,54 @@
+#!/usr/bin/python3
+
+import cgi
+import cgitb
+import sys
+cgitb.enable()
+
+import os
+from base64 import b64decode
+
+auth = os.getenv("HTTP_AUTHORIZATION")
+query = os.getenv("QUERY_STRING") or ""
+
+query = cgi.parse_qs(query)
+
+width = query.get("width", ["100"])[0]
+height = query.get("height", ["100"])[0]
+fmt = query.get("format", "png")[0]
+
+try:
+ width = int(width)
+except:
+ width = 100
+
+try:
+ height = int(height)
+except:
+ height = 100
+
+width = width if width > 0 else 100
+height = height if height > 0 else 100
+
+FORMATS = {
+ "png": { "ctype": "image/png", "ptype": "png" },
+ "jpeg": { "ctype": "image/jpeg", "ptype": "jpeg" },
+ "bmp": { "ctype": "image/bmp", "ptype": "bmp" },
+ "ico": { "ctype": "image/ico", "ptype": "ico" },
+ "gif": { "ctype": "image/gif", "ptype": "gif" },
+ "webp": { "ctyle": "image/webp", "ptype": "webp" },
+}
+
+fmt = FORMATS.get(fmt)
+
+if fmt is None:
+ fmt = FORMATS["png"]
+
+from PIL import Image
+
+im = Image.new('RGB', (width, height))
+im.putdata([(255,0,0)] * (width * height))
+
+print("Content-Type: {}".format(fmt["ctype"]))
+print("")
+im.save(fp=sys.stdout.buffer, format=fmt["ptype"])