sorl-thumbnail icon indicating copy to clipboard operation
sorl-thumbnail copied to clipboard

How to create a thumbnail in a view, rather than dynamically via a template?

Open typonaut opened this issue 1 year ago • 0 comments

I am uploading images, and rather than create thumbnails dynamically via a template tag, I want to create the necessary thumbnails when the upload is done. This should essentially mirror what the template tag would do. So where:

{% thumbnail object.user.address.address_img "240" crop="center" as im %}
		<img itemprop="image" 
				src="{{ im.url }}" 
				alt="{{ object.display_name }}" 
				title="{{ object.display_name }}"
				width="{{ im.width }}"
				height="{{ im.height }}" 
				class="page-img"/>
									
{% endthumbnail %}

I would like to leave that code on the template, but pre-generate the thumbnail in the view:

th_img = get_thumbnail(object.user.address.address_img, '240', crop='center')

th_img has properties key, name, url, etc, but no file is created in the cache, and there is no entry in the thumbnail_kvstore.

How do I "save" the thumbnail and get an entry in the thumbnail_kvstore?

The thumbnail cannot go into a field for the model, because there are other sizes of thumbnail to be generated too.

Thanks

typonaut avatar Aug 22 '23 18:08 typonaut