Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ThumbnailerImageField saves incorrect width_field and height_field when resize_source is applied #548

Open
michelts opened this issue Aug 27, 2020 · 3 comments

Comments

@michelts
Copy link

Consider the following snippet:

    image_width = models.PositiveIntegerField()
    image_height = models.PositiveIntegerField()
    image = ThumbnailerImageField(
        width_field="image_width",
        height_field="image_height",
        resize_source={"size": (100, 100)},
    )

When I save an instance of the model with this field, the image_field and image_height fields are populated with the original image size rather than the thumbnail size.

I couldn't find why this is happening, but after activating a higher level of debugging, I found that the object is saved twice:

  • once in the ThumbnailerImageField.save method, in this case, with the correct dimensions.
  • once more after that, with the original dimensions, although I couldn't find where this is happening yet.

I'm using Django==3.0.5 and easy-thumbnails==2.7.

@Akiat
Copy link

Akiat commented Dec 1, 2020

I can reproduce this issue. It's really anoying for my use cases. Would it be possible to have a fix ?

@michelts
Copy link
Author

michelts commented Dec 2, 2020

I personally couldn't find a way to fix it, but I implemented a workaround using a pre-save signal. See it below, assuming you have a model Figure with an attribute image:

@receiver(models.signals.pre_save, sender=Figure)
def resize_image(sender, instance, **kwargs):
    orig_file = io.BytesIO(instance.image.read())
    thumbnailer = get_thumbnailer(orig_file, instance.image.name)
    thumb = thumbnailer.get_thumbnail(options, save=False)
    instance.image.save(instance.image.name, thumb, save=False)
    instance.image_width = thumb.width
    instance.image_height = thumb.height

@Akiat
Copy link

Akiat commented Dec 2, 2020

I used a workaround but your way is more elegant, thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants