【问题标题】:Is there a way to use sorl-thumbnail url without generating the thumbnail?有没有办法在不生成缩略图的情况下使用 sorl-thumbnail url?
【发布时间】:2015-08-30 17:44:30
【问题描述】:

我想生成一个缩略图列表。我选择了 sorl-thumbnail,因为它似乎被广泛使用和开发。

我的问题是模板标签“缩略图”:它正确生成缩略图图像和缩略图对象,但我只想要 url。

我不介意缩略图图像是否尚未生成/准备好,我只关心模板生成。 我可以通过自定义视图提供缩略图,这不是问题,因为只有少数人可以使用。

所以为了简化,我想将 url 生成与缩略图生成本身分离。

sorl-thumbnail 可以吗?如果没有,你知道另一个可以的项目吗?

如果有些东西已经可以做到,我宁愿不自己写。

附:我会将缩略图存储在磁盘上。

【问题讨论】:

  • 在视图中或通过信号生成缩略图,并直接在模板中使用 url。
  • 在视图中生成它们需要很长时间。我更喜欢快速的 html 答案,然后浏览器将通过 GET 请求请求图像。
  • 然后使用异步任务来生成它们。
  • 如果没有人要求,我不想生成它们。真的我不介意图像是否需要时间来生成,但我想要一个快速的模板生成。我的问题是:有没有办法做到这一点?不是如何避免它。
  • 没有缩略图和 sorl-thumbnail 的香草代码无法生成 url,您的另一个选择是覆盖当前的实现并添加这样的行为。

标签: python django thumbnails sorl-thumbnail


【解决方案1】:

在我早期的 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' }}">

【讨论】:

  • 这是一段非常有趣的代码。不幸的是,它和 sorl-thumbnail 有同样的缺陷:如果你在模板的循环中使用它,生成缩略图需要很长时间。此外,生成将是顺序的,并且比并行请求生成需要更多时间。
猜你喜欢
  • 2012-04-17
  • 2019-10-14
  • 2019-12-18
  • 2012-09-18
  • 2011-07-01
  • 1970-01-01
  • 2016-03-05
  • 2015-06-14
  • 2023-03-08
相关资源
最近更新 更多