在 Django 中指定带有简单缩略图的文件名后缀
Specify a filename suffix with easy-thumbnails in django
我正在使用 django 中的 easy-thumbnails 库来创建缩略图。但是,我不知道如何覆盖缩略图命名过程。目前 lib 将缩略图大小附加到文件名,但我想指定一个自定义名称,例如_大。请问这怎么办?
您可以定义自己的 thumbnail-processor
并在此处作为最后一行:http://easy-thumbnails.readthedocs.org/en/latest/ref/settings/#easy_thumbnails.conf.Settings.THUMBNAIL_PROCESSORS
THUMBNAIL_PROCESSORS = (
'easy_thumbnails.processors.colorspace',
'easy_thumbnails.processors.autocrop',
'easy_thumbnails.processors.scale_and_crop',
'easy_thumbnails.processors.filters',
'easy_thumbnails.processors.background',
'yourProject.thumbnail_processors.renaming', #<---- your custom one
)
您的处理器文件 (yourProject/thumbnail_processors.py
) 看起来像:
def renaming(image, bang=False, **kwargs):
"""
rename the filename here and just return the image
"""
return image
虽然没有测试
django-easy-thumbnails
使用 default renaming function. You can write your own naming function and set it in settings as a default naming function the library should use, as described here THUMBNAIL_NAMER:
myapp.utils
def namer(thumbnailer, prepared_options, source_filename,
thumbnail_extension, **kwargs):
# do something and return name
pass
settings.py
THUMBNAIL_NAMER = 'myapp.utils.namer'
我正在使用 django 中的 easy-thumbnails 库来创建缩略图。但是,我不知道如何覆盖缩略图命名过程。目前 lib 将缩略图大小附加到文件名,但我想指定一个自定义名称,例如_大。请问这怎么办?
您可以定义自己的 thumbnail-processor
并在此处作为最后一行:http://easy-thumbnails.readthedocs.org/en/latest/ref/settings/#easy_thumbnails.conf.Settings.THUMBNAIL_PROCESSORS
THUMBNAIL_PROCESSORS = (
'easy_thumbnails.processors.colorspace',
'easy_thumbnails.processors.autocrop',
'easy_thumbnails.processors.scale_and_crop',
'easy_thumbnails.processors.filters',
'easy_thumbnails.processors.background',
'yourProject.thumbnail_processors.renaming', #<---- your custom one
)
您的处理器文件 (yourProject/thumbnail_processors.py
) 看起来像:
def renaming(image, bang=False, **kwargs):
"""
rename the filename here and just return the image
"""
return image
虽然没有测试
django-easy-thumbnails
使用 default renaming function. You can write your own naming function and set it in settings as a default naming function the library should use, as described here THUMBNAIL_NAMER:
myapp.utils
def namer(thumbnailer, prepared_options, source_filename,
thumbnail_extension, **kwargs):
# do something and return name
pass
settings.py
THUMBNAIL_NAMER = 'myapp.utils.namer'