/
httpGraphic.py
202 lines (146 loc) · 4.87 KB
/
httpGraphic.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os,sys
import web
import traceback
import logging
import logging.handlers
import Image,ImageDraw
from cStringIO import StringIO
from setting import *
LOG_FORMAT = '[%(asctime)s] %(funcName)s(%(filename)s:%(lineno)s) [%(levelname)s]:%(message)s'
log = logging.getLogger()
urls = (
'/(.*)/', 'redirect /$1',
'/notfound','notfoundCrl',
'/graphic/(\w+)', 'serverCrl'
)
app = web.application(urls, globals())
def app_processor_start():
pass
def app_processor_end():
pass
def header_html():
web.header('Content-Type', 'text/html; charset=UTF-8')
def notfound():
web.ctx.status = '404 Not Found'
return web.notfound('404 Not Found')
def internalerror():
web.ctx.status = '500 Internal Server Error'
return web.internalerror('500 Internal Server Error')
def handle_validate(func):
def Function(*args):
#todo 验证请求
return func(*args)
return Function
app.add_processor(web.loadhook(app_processor_start))
app.add_processor(web.unloadhook(app_processor_end))
class Graphic:
_stream = None
_org = None
_format = None
_org_w = 0
_org_h = 0
_quality = 60
def __init__(self,original_file):
if not os.path.isfile(original_file):
raise "no original_file found"
self._org = Image.open(original_file)
self._org_w,self._org_h = self._org.size
if self._org.mode !='RGBA':
self._format='JPEG'
else:
self._format='PNG'
self._stream = StringIO()
def resize(self,size):
if size[1] is 0:
tmp=self._org.resize((size[0],(self._org_h*size[0])/self._org_w),Image.ANTIALIAS)
elif size[0] is 0:
tmp=self._org.resize(((self._org_w * size[1])/self._org_h,size[1]),Image.ANTIALIAS)
else:
f1 = 1.0 * size[0] / self._org_w
f2 = 1.0 * size[1] / self._org_h
factor = min([f1, f2])
width = int(self._org_w * factor)
height = int(self._org_h * factor)
tmp=self._org.resize((width, height),Image.ANTIALIAS)
tmp.save(self._stream,self._format,quality=self._quality)
def cut(self):
pass
def mark(self):
pass
def getDate(self):
return self._stream.getvalue()
#造一张默认图片
def notfoundImage(w,h):
if w is 0 or h is 0:
w = h = 200
elif w is 0:
w = h
elif h is 0:
h = w
output = StringIO()
img = Image.new('RGBA', (w,h), (240,240,240,0))
img.save(output,'JPEG')
return output.getvalue()
def mkTargetFile(action,filename,size):
global filename_template
dirname = os.path.dirname(filename)
filename = os.path.basename(filename)
name,ext = os.path.splitext(filename)
if size[0] is 0:
tpl = filename_template.get(action+"_h")
elif size[1] is 0:
tpl = filename_template.get(action+"_w")
else:
tpl = filename_template.get(action+"_wh")
return ''.join([UPLOAD_PATH,dirname,'/',tpl.format(name=name, width=size[0],height=size[1],ext=ext)])
class notfoundCrl:
def GET(self):
req = web.input(w=0,h=0)
output = notfoundImage(int(req.w), int(req.h))
web.header('Content-Type', 'image/jpeg; charset=UTF-8')
return output
class serverCrl:
@handle_validate
def GET(self,action):
#web.header('Content-Type', 'text/html; charset=UTF-8')
req = web.input(filename=None,w=0,h=0)
size = int(req.w), int(req.h)
target_file = mkTargetFile(action,req.filename,size)
if os.path.isfile(target_file):
fs = open(target_file)
stream = fs.read()
else:
original_file = "%s/%s" % (
UPLOAD_PATH,
req.filename
)
stream = None
try:
gc = Graphic(original_file)
if action == "resize":
gc.resize(size)
elif action is "cut":
gc.cut()
stream = gc.getDate()
if LOCALSTORAGE:
fs = open(target_file, 'wb')
fs.write(stream)
fs.close()
except IOError as e:
log.error(str(e))
stream = notfoundImage(size[0],size[1])
except:
excinfo = traceback.format_exc()
log.error(excinfo)
#request_uri = web.ctx.env.get('REQUEST_URI', '/')
stream = notfoundImage(size[0],size[1])
web.header('Content-Type', 'image/jpeg; charset=UTF-8')
return stream
if __name__ == "__main__":
create_log(LOG_FILE,LOG_LEVEL)
web.config.debug = True if DEBUG else False
app.notfound = notfound
app.internalerror = web.debugerror if DEBUG else internalerror
app.run()