summaryrefslogtreecommitdiff
path: root/cgi-bin/image.cgi
blob: edcca1e96158f6603f363743960a741d8110d64b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/python3

import cgi
import cgitb
import sys
cgitb.enable()

import os
from io import BytesIO

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

width = width if width < 4000 else 4000
height = height if height < 4000 else 4000

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("")
sys.stdout.flush()

imgbytes = BytesIO()
im.save(fp=imgbytes, format=fmt["ptype"])

sys.stdout.buffer.write(imgbytes.getvalue())