Django easy-thumbnails vs sorl-thumbnail differences
Asked Answered
T

2

17

I've been reading about and trying various thumbnailing apps for Django. These are the requirements:

  • All generated thumbnails must be saved in a S3 bucket separate from the original images, i.e. separate storage class

  • When the image instance is deleted, the original image file along with all generated thumbnails must be delete as well

  • Minimize expensive inefficiencies, ex. Fetching the url of a thumbnail to serialize in DRF shouldn't look in S3 bucket to see if it exists every time, etc.

VersatileImageField fails the first requirement. ImageKit fails the second requirement. The third requirement is where I'm most confused; I'm trying to inform myself on best practices but the information is fragmented and I'm not confident in making a decision based on what I've learned so far.

From what I've read, my impression is that the most efficient behavior would be as follows:

  • generate the thumbnail immediately upon save and assume it always exists
  • to access thumbnail, generate the URL based on the original image's filename and thumbnail dimensions/quality, since we know it definitely exists
  • post_delete will delete all thumbnails and original file

I'd be most interested in learning about the differences in the approaches that easy-thumbnails and sorl-thumbnail take (if they align with the process I very briefly outlined above or if they have something even more efficient), and the advantages/disadvantages in each of their methodologies.

Trilbie answered 1/2, 2016 at 21:51 Comment(3)
How did you solve your problem?Symphonia
@weisson I tried all three of these apps, all were imperfect and too opinionated (there's no other way to go about it). I'm using client-side thumbnailing with pica.js. The image blob is uploaded directly to S3, no ImageField or pillow needed.Trilbie
It's 2020 and I'm having the same problem. Each time I have to add images to a project I have a bad feeling because of this.Hanghangar
B
1

I hope this may help you in the model, there are two fields image and thumbnail, in view make validation image type and size after that generate thumbnail using Pill

from PIL import Image as Img
from io import BytesIO

def create(self,request):
    mutable = request.POST._mutable
    request.POST._mutable = True

    for value in request.FILES.items():
        im = Img.open(value[1])
        im.thumbnail((425, 236), Img.ANTIALIAS)
        buffer = BytesIO()
        im.save(fp=buffer, format='JPEG')

        requset.POST['thumbnail'] = ContentFile(buffer.getvalue(), thumnail_name)

     request.POST._mutable = mutable

to save images in folder and thumbnails anther folder you can use different path with upload_to in ImageField

Bulganin answered 5/5, 2021 at 3:26 Comment(0)
O
0

I'm not sure if this is helpful, but I've used easy-thumbnails in the past and I'm fairly sure that it does all of the things that you're asking for if you configure it a bit. Configuring it with the save function is a bit tricky, as the save function doesn't care to be configured, but it's not impossible. The main thing that can cause problems is that you have to use 'save and continue editing' to access and use the thumbnail option. It won't be visible until you do so if you haven't created it already since it's created on save.

def save()
    found_id = self.id
        super(Team, self).save(*args, **kwargs)
        if self.image and found_id is None and self.original_image_width and self.original_image_height:
            self.image = get_thumbnailer(self.image).get_thumbnail({
                'size': (self.original_image_width, self.original_image_height)
            }).name
        super(Team, self).save(*args, **kwargs)
Obligatory answered 4/3, 2016 at 16:36 Comment(2)
Could you please provide an example snippet of code of how you would configure the save function, and how you would use 'save and continue editing'? Thanks.Trilbie
Basically what happens with 'Save and continue editing' is that the save function would generate the easy_thumbnails urls You would basically create options for the original image width and height, as well as the same for the thumbnail, and an image uploader and section to store the thumbnail in the modelObligatory

© 2022 - 2024 — McMap. All rights reserved.