KalvadTech / django-cloudflare-images

Django library to add Cloudflare Images support for ImageField
MIT License
15 stars 6 forks source link

Serving multiple variants #3

Closed henningbra closed 1 year ago

henningbra commented 1 year ago

May I ask how to serve more than one variant, using a list of [small, medium, large] preregistered variants in Cloudflare Images. How to manipulate the url?

hnb2 commented 1 year ago

Hello @henningbra just to clarify you wish to serve 3 different variants off the same field ? Or you need 3 fields each with its own variant ?

Let me know

henningbra commented 1 year ago

Former

EDIT: 3 different variants off the same field for faster loading times. Have checked tests and tried lots of different methods to make it work to no avail.

henningbra commented 1 year ago

Below property works. It would be possible to make a template tag passing in a variable from the below.

    @property
    def url_custom(self):
        return f'{self.image.url.rsplit("/", 1)[0]}/custom'

Id that's the cleanest I will proceed.

hnb2 commented 1 year ago

Hi @henningbra I think in your case, the best would be to add either:

mschfh commented 4 months ago

@hnb2 Wouldn't it be possible to add a function that accepts the variant as argument and returns the variant URL instead of hardcoding it in the model definition? (Use case could be different thumbnail sizes of the same picture, etc)

hnb2 commented 4 months ago

So there is a method url_with_variant here defined in the Storage which is used in url here in the Field. So Maybe in this field we could add a method which allows to pass a parameter to make it 100% dynamic.