如何将高度和宽度传递给 imagekit 模板标签?

问题描述

views.py

class imagess(ImageSpec):
    processors = [SmartResize(100,100)]
    format = 'JPEG'
    options = {'quality': 100}

register.generator('main:imagess',imagess)

index.html

{% generateimage 'main:imagess' source=source_file %}

这会生成一个高度为 100 x 100 的图像

如何为从 HTML 文件生成的图像传递/定义自定义高度和宽度....like

{% generateimage 'main:imagess' source=source_file height=1080 width=720 %}

解决方法

只需添加额外的属性并在视图函数定义中传递它们,您就可以使用这些参数:

{% generateimage 'myapp:thumbnail' source=source_file height=1080 width=720 %}

Views.py

class imagess(ImageSpec,height,width):
    processors = [SmartResize(height,width)]
    format = 'JPEG'
    options = {'quality': 100}

register.generator('main:imagess',imagess)