比如请求 http://xxx.com/image?name=filename&size=128 则返回相应大小的图片返回的是一张图片,128时返回filename_128.png,256时返回filename_256.png
1 回答

牧羊人nacy
TA贡献1862条经验 获得超7个赞
用 tornado 写过一个
流程大概就是取参数去读本地图片, 再缩放尺寸吐出去
import osimport magicimport StringIOfrom PIL import Imagefrom PIL.ExifTags import TAGSimport tornado.webclass img(tornado.web.RequestHandler, tornado.web.StaticFileHandler): def get_mime(self, path): mime = magic.Magic(mime=True) return mime.from_file(path) def send_img(self, ret, mime_type): self.set_header("Content-Type", mime_type) self.set_header('Content-Length', len(ret)) self.finish(ret) def resize(self, path, size): mime_type = self.get_mime(path) if not mime_type or not os.path.exists(path) or not os.path.isfile(path): raise tornado.web.HTTPError(404) img = Image.open(path) _size = tuple([int(s) for s in size.split('x')]) img.thumbnail(_size, Image.ANTIALIAS) buffer = StringIO.StringIO() img.save(buffer, mime_type[6:]) ret = buffer.getvalue() self.send_img(ret, mime_type) def get(self, path, includ_body=True): path = self.parse_url_path(path) abspath = os.path.abspath(os.path.join(self.root, path)) size = self.get_argument('s', None) if size: self.resize(abspath, size) else: tornado.web.StaticFileHandler.get(self, path, includ_body)
添加回答
举报
0/150
提交
取消