SmileyChris / easy-thumbnails

Easy thumbnails for Django
http://easy-thumbnails.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
1.39k stars 319 forks source link

Do not generate thumbnail for image smaller than alias sizes #558

Open Akiat opened 4 years ago

Akiat commented 4 years ago

Hi and thank you for this great module.

Is there a way to not generate a new thumbnail when the alias size is greater than the original image size ?

e.g with these aliases:

THUMBNAIL_ALIASES = {
    'web_app': {
        'small': {'size': (150, 150), 'crop': True},
        'medium': {'size': (500, 1000), 'crop': False},
        'big': {'size': (1000, 2000), 'crop': False},
    },
}

If I upload an image with a size of 300x300, it will be great to only have 1 thumbnail generated (the small one), and the other which redirect on the original image when we access it via thumbnail_url(self.media_field, 'medium').

If it's not yet supported, do you think it can be a feasable upgrade ?

Thanks !

jrief commented 4 years ago

For each thumbnail to be created, easy-thumbnails first generates a unique name using the given parameters (size, crop, upscale, etc.). It then checks if an image with that name already exists. If so, that image is used, otherwise it is generated.

There is a function which generates that name. You can override it and add your own logic. Then for instance you can generate the same name for medium and big images.

I hope this helps.

Akiat commented 4 years ago

Thanks for your quick reply 👍

Do you have the name of this function ?

jrief commented 4 years ago

https://easy-thumbnails.readthedocs.io/en/latest/ref/settings/?highlight=namer#easy_thumbnails.conf.Settings.THUMBNAIL_NAMER

Akiat commented 4 years ago

Thanks a lot, I tried to re-use the logic of the namer "alias", but thumbnail_options.get('ALIAS', '') returns nothing.

https://github.com/SmileyChris/easy-thumbnails/blob/master/easy_thumbnails/namers.py#L30

How can I access the name of the requested alias ?