在我早期的 Django 体验中,我遇到了完全相同的问题。我要求所有生成的缩略图都存储在磁盘上。我们在使用 PIL 的部署服务器上也遇到了问题(当时 Python 3 有几个问题)。
我们找不到任何可以帮助我们的东西,所以我们最终得到了以下结果。我们还最终从命令行使用 imagemagick 来避免 PIL。
这个想法是创建一个自定义标签,要求提供缩略图。如果不存在,标签将创建缩略图并将其存储在磁盘上供以后使用(在具有样式名称的子目录中)。因此,只有在有人第一次访问该页面时才会出现延迟。这个想法来自 Drupal。
此外,缩略图尺寸是特定的,因此我们在项目设置中对其进行了硬编码,但更改它应该不会太难。
坦率地说,我不确定这是否是最佳解决方案,但它仍然有效。它也是我在 Python 中的早期代码之一,所以对它要温柔(现在我完全了解例如os.join 等,但还没有时间改进和测试;但我也对你的建设性意见非常感兴趣) .
首先,这里是设置:
IMAGE_STYLES = {
'thumbnail_style': {
'type': 'thumbnail', # maintain a max of one of the two dimensions, depending on aspect ratio
'size': (150, 1000)
},
'thumbnail_crop_style': {
'type': 'thumbnail-crop', # maintain both dimensions by cropping the remaining
'size': (150, 150)
},
'thumbnail_upscale_style': {
'type': 'thumbnail-upscale', # same as first, but allow upscaling
'size': (150, 150)
},
}
这是自定义标签:
from django import template
from django.template.defaultfilters import stringfilter
from django.conf import settings
from subprocess import check_output, call
import os
register = template.Library()
@register.filter
@stringfilter
def image_style(url, style):
""" Return the url of different image style
Construct appropriately if not exist
Assumptions:
- Works only for Linux OS; double slashes are anyway ignored
- MEDIA_URL is local (url is in form MEDIA_URL.. (eg /media/..)
:param url: An image url
:param style: Specify style to return image
:return: image url of specified style
"""
path_file_name = settings.BASE_DIR + url
style_url_path = '/'.join((os.path.dirname(url), style))
style_url = '/'.join((style_url_path, os.path.basename(url)))
style_path = settings.BASE_DIR + style_url_path
style_path_file_name = settings.BASE_DIR + style_url
style_def = settings.IMAGE_STYLES[style]
if not os.path.exists(style_path_file_name):
if not os.path.exists(style_path):
os.makedirs(style_path)
by = chr(120) # x
plus = chr(43) # +
source_size_str = str(check_output(['identify', path_file_name])).split(' ')[2]
source_size_array = source_size_str.split(by)
source_size_x = int(source_size_array[0])
source_size_y = int(source_size_array[1])
target_size_x = style_def['size'][0]
target_size_y = style_def['size'][1]
target_size_str = str(target_size_x) + by + str(target_size_y)
if style_def['type'] == 'thumbnail':
if target_size_x > source_size_x and target_size_y > source_size_y:
target_size_str = source_size_str
call(['convert', path_file_name, '-thumbnail', target_size_str, '-antialias', style_path_file_name])
elif style_def['type'] == 'thumbnail-upscale':
call(['convert', path_file_name, '-thumbnail', target_size_str, '-antialias', style_path_file_name])
elif style_def['type'] == 'thumbnail-crop':
source_ratio = float(source_size_x) / float(source_size_y)
target_ratio = float(target_size_x) / float(target_size_y)
if source_ratio > target_ratio: # crop vertically
crop_target_size_x = int(source_size_y * target_ratio)
crop_target_size_y = source_size_y
offset = (source_size_x - crop_target_size_x) / 2
crop_size_str = str(crop_target_size_x) + by + str(crop_target_size_y) + plus + str(offset) + plus + '0'
else: # crop horizontally
crop_target_size_x = source_size_x
crop_target_size_y = int(source_size_x / target_ratio)
offset = (source_size_y - crop_target_size_y) / 2
crop_size_str = str(crop_target_size_x) + by + str(crop_target_size_y) + plus + '0' + plus + str(offset)
call(['convert', path_file_name, '-crop', crop_size_str, style_path_file_name])
call(['convert', style_path_file_name, '-thumbnail', target_size_str, '-antialias', style_path_file_name])
return style_url
这是在ntemplate中的使用方法:
<img src="{{ image_field.url|image_style:'my_style' }}">