Question

I am using sorl thumbnail for the first time. I am working with Low Level API and using the get_thumbnail method.

As per the link, I successfully used im = get_thumbnail(my_file, '100x100', crop='center', quality=99), but I would like to specify the destination source location and the filename as well. Is that possible? How do I do that?

Was it helpful?

Solution

Short answer: you can't. The path and filename are generated by sorl and directly relate to how the thumbnail paths and their files are cached, and stored.

If you look at the actual method in sorl that gets or creates the thumbnail:

https://github.com/sorl/sorl-thumbnail/blob/master/sorl/thumbnail/base.py#L32

you'll see that the destinatation and filename of the created thumbnail is a hash value of the source filename, source location, source dimensions and source options. It creates this hash, checks the cache to see a value for that hash/key is present, if it is - return the path corresponding to that thumbnail, if not - generates the thumbnail, save it and cache the path.

Therefore changing the destination of the filename would mean you could no longer retrieve that thumbnail from the cache.

The only thing you can change is the root folder of where you want these files to be saved i.e. under /cache/... or /thumbnails/...

OTHER TIPS

I believe you can extend 'sorl.thumbnail.base.ThumbnailBackend' override _get_thumbnail_filename method that handle destination filename and sign the new class to THUMBNAIL_BACKEND in settings.

from django.core.files.base import File

my_file = File(open('/path/to/file', 'rb')) # or my_file = open('/path/to/file', 'rb')
im = get_thumbnail(my_file, '100x100', crop='center', quality=99)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top