def get_media_thumbnail(size, filename):
filename = filename.decode('utf8')
ext = os.path.splitext(filename)[-1].lower()
if ext == '.pdf':
redirect('/resources/images/pdf-200x200.png')
return
if ext not in ('.jpg', '.png', '.jpeg', '.bmp'):
redirect('/resources/images/blank.png')
return
media_dir = app._config['henet']['media_dir']
thumbnails_dir = app._config['henet']['thumbnails_dir']
thumbname = size + '-' + filename
thumbnail_file = os.path.join(thumbnails_dir, thumbname)
if not os.path.exists(thumbnail_file):
image_file = os.path.join(media_dir, filename)
size = [int(i) for i in size.split('x')]
image = Image.open(image_file)
image.thumbnail(size)
image.save(thumbnail_file, 'JPEG')
mimetype = guess_type(thumbnail_file)[0]
return static_file(thumbname, root=thumbnails_dir,
mimetype=mimetype)
评论列表
文章目录