matthewwithanm / django-imagekit

Automated image processing for Django. Currently v4.0
http://django-imagekit.rtfd.org/
BSD 3-Clause "New" or "Revised" License
2.26k stars 276 forks source link

how can pass height and width to the templatetag #515

Closed harshjadon9 closed 3 weeks ago

harshjadon9 commented 3 years ago

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 %} this generates a image of height 100 x 100

how can pass/define a custom height and width for the generated image from the HTML file....like

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

vstoykov commented 3 weeks ago

If you want to pass custom with and height to images you can use the {% thumbnail %} template tag.

If you still want to use your own ImageSpec then you need to override the __init__ method to accept the arguments that you will give. In your case you probbaly need something like:

class imagess(ImageSpec):
    format = 'JPEG'
    options = {'quality': 100}

    def __init__(self, source, width, height):
        processors = [SmartResize(width, height)]
        super().__init__(source)

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

For inspiration you can use the source code of the ImageSpec