问题描述
索尔缩略图有一个低级API,可以让您做到这一点,例如:
Sorl thumbnail has a low-level API that allows you to do, for example:
from sorl.thumbnail import get_thumbnail
im = get_thumbnail(my_file, '100x100', crop='center', quality=99)
这将返回对缓存文件的引用。如果它已经被创建,它是超级快速。但是,如果必须首次创建它,则使用远程存储(如S3)可能需要很长时间。
This returns a reference to the cached file. If it's already been created, it's super quick. However, if it has to create it for the first time it can take a long time when using remote storage such as S3.
有没有办法运行命令Python(即不在模板中),以检查sorl是否会首次生成缩略图?
Is there a way to run a command in Python (ie. not in a template) to check if sorl will have to generate a thumbnail for the first time?
PS。我知道类似的问题,但这在模板上下文中提出了这个问题,并且使用了一个hacky解决方案作为使用自定义SQL而不是sorl API的答案。
PS. I'm aware of similar question here but this is asking about it in a template context, and has a hacky solution as an answer that uses custom SQL and not the sorl API.
推荐答案
p>在我的版本 sorl.thumbnail
, 11.12
,方法 get_thumbnail
在sorl.thumbnail.base.py中定义,并启动如下:
In my version sorl.thumbnail
, 11.12
, the method get_thumbnail
is defined in sorl.thumbnail.base.py and starts as follows:
def get_thumbnail(self, file_, geometry_string, **options):
"""..."""
source = ImageFile(file_)
for key, value in self.default_options.iteritems():
options.setdefault(key, value)
# ...
for key, attr in self.extra_options:
value = getattr(settings, attr)
if value != getattr(default_settings, attr):
options.setdefault(key, value)
name = self._get_thumbnail_filename(source, geometry_string, options)
thumbnail = ImageFile(name, default.storage)
cached = default.kvstore.get(thumbnail)
if cached:
return cached
if not thumbnail.exists():
...
如果您使用此代码并返回类似于
If you use this code and return something like
cached or thumbnail.exists()
这应该给你所需的结果。
this should give you the desired result.
这篇关于检查sorl缩略图是否已经使用低级API缓存了图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!