Django / PIL - save thumbnail version right when image is uploaded

前端 未结 6 815
挽巷
挽巷 2021-01-30 09:39

This is my forms.py:

class UploadImageForm(forms.ModelForm):
    class Meta:
        model = UserImages
        fields = [\'photo\']

and this i

6条回答
  •  鱼传尺愫
    2021-01-30 10:01

    there is super easier way,

    in model:

    def upload_thumb_dir(self, filename):
            path = f'path_to_thumb.jpg'
            return path
    
    thumb = ProcessedImageField(upload_to=upload_thumb_dir,
        processors=[ResizeToFill(192, 108)],
        max_length= 255,
        format='JPEG',
        default='preview.jpg',
        options={'quality': 80},
        null=True, blank=True
    )
    # ...
    def save(self, *args, **kwargs):
        self.thumb = self.rawfile.file
        super(VersionPreviews, self).save(*args, **kwargs)
    

提交回复
热议问题