我想生成缩略图列表。我选择了 sorl-thumbnail,因为它似乎被大量使用和开发。
我的问题是模板标签“thumbnail”:它正确生成缩略图图像和缩略图对象,但我只需要 url。
我不介意缩略图是否尚未生成/准备好,我只关心模板生成。 我可以通过自定义 View 提供我的缩略图,这不是问题,因为这些只对少数人可用。
因此,为了简化,我想将 url 生成与缩略图生成本身分离。
sorl-thumbnail 可以吗?如果不能,您知道另一个可以做到的项目吗?
如果已经可以做到,我宁愿不自己写。
附言我会将缩略图存储在磁盘上。
最佳答案
我在使用 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' }}">
关于python - 有没有办法在不生成缩略图的情况下使用 sorl-thumbnail url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30841620/