jonasundderwolf / django-image-cropping

Django helper application to easily and non-destructively crop arbitrarily large images in admin and frontend.
Other
553 stars 130 forks source link

Comaptibility issue with easythumbnails #150

Open jphilip opened 4 years ago

jphilip commented 4 years ago

I had a compatibility issue with easy_thumbnails when using its ThumbnailerImageField in a model. This is related to issue #149 . The issue arises when setting the resize_source option on the ThumbnailerImageField , which allows to resize the image before it is saved whereas the thumbnail is messed up. This is due to the fact that image_cropping initializes the ImageRationFields before the image is resized by easy_thumbnails (through a pre_save signal), resulting in the wrong coordinates for it box. I was able to implement a workaround by forcing the change of the image before the signal. One place that works is the save method of my model. This may be hard to fix in image_cropping, so I am posting the workaround if it can help someone, maybe worth having it in the docs.

def save(self, *args, **kwargs):
        options = getattr(self._meta.get_field('image'), 'resize_source')
        if 'quality' not in options:
            options['quality'] = self.image.thumbnail_quality
        content = Thumbnailer(
            self.image, self.image.name).generate_thumbnail(options)
        self.image.save(self.image.name, content, save=False)
        super().save(*args, **kwargs)