django-imagekit icon indicating copy to clipboard operation
django-imagekit copied to clipboard

How to create several thumbs, which are created while saving model?

Open LennyLip opened this issue 10 years ago • 10 comments
trafficstars

Say, i have ImageFiled that stored URL in DB and file on hdd.

  img = ProcessedImageField(storage=photo_fs,
                          processors=[Transpose(Transpose.AUTO),
                                      ResizeToFit(1280, 960, False),
                                      ResizeCanvas(1280, 960, anchor=Anchor.CENTER)
                                      ],
                          format='JPEG',
                          options={'quality': 75})

how can i create second (third, etc) thumb while saving model? something like a:

   img_640_480 = ProcessedImageField(source="img",
                          processors=[ResizeToFit(640, 480, False),],
                          file_name=source_file_name_wo_ext+"_640-480.jpg"
                             )

thanks!

LennyLip avatar Jul 24 '15 09:07 LennyLip

up. ups, i want img_640_480 not from img. because img already have canvas.. I want to create img_640_480 from uploaded source file.

LennyLip avatar Jul 24 '15 12:07 LennyLip

img_640_480 = ProcessedImageField(source="img", 
                                  processors=[ResizeToFit(640, 480, False),],
                                  cachefile_strategy='imagekit.cachefiles.strategies.Optimistic')

nex2hex avatar Jul 28 '15 15:07 nex2hex

hmm, ProcessedImageField TypeError: init() got an unexpected keyword argument 'source'

LennyLip avatar Jul 28 '15 16:07 LennyLip

Now, i just using several ProcessedImageField (without "source') and copy request.data.get('img') to each of them when saving. It is ugly, but it is work.

LennyLip avatar Jul 28 '15 16:07 LennyLip

show me full your code please, with forms, models and view. Your solution with request.data.get('img') are ugly

nex2hex avatar Jul 29 '15 08:07 nex2hex

first of all, i think ProcessedImageField has no "source" argument.

class ProcessedImageField(ImageField, SpecHost):

It is in ImageSpecField, not in SpecHostField.

How it works now:

class Photo(models.Model):
    img = ProcessedImageField(storage=photo_fs,
                          processors=[Transpose(Transpose.AUTO),
                                      ResizeToFit(1280, 960, False),
                                      ResizeCanvas(1280, 960, anchor=Anchor.CENTER)
                                      ],
                          format='JPEG',
                          options={'quality': 75})
     img_640_480 = ProcessedImageField(storage=photo_fs,
                                  processors=[Transpose(Transpose.AUTO),
                                              ResizeToFit(640, 480, False),
                                              ResizeCanvas(640, 480, anchor=Anchor.CENTER)
                                              ],
                                  format='JPEG',
                                  options={'quality': 75})

I have no form and views, because i use Django Rest Framework with js frontend.

DRF class view

class PhotosList(generics.ListAPIView, generics.CreateAPIView):
   def perform_create(self, serializer):
       serializer.save(owner=self.request.user,
                    img=self.request.data.get('img'),
                    img_640_480=self.request.data.get('img'),
                    img_240_180=self.request.data.get('img'),
                    img_140_100=self.request.data.get('img'))

LennyLip avatar Jul 29 '15 08:07 LennyLip

Sorry for my typo, you need ImageSpecField for thumbs, and ProcessedImageField for original images

class Photo(models.Model):
    img = ProcessedImageField(storage=photo_fs,
                          processors=[Transpose(Transpose.AUTO),
                                      ResizeToFit(1280, 960, False),
                                      ResizeCanvas(1280, 960, anchor=Anchor.CENTER)
                                      ],
                          format='JPEG',
                          options={'quality': 75})
     img_640_480 = ImageSpecField (source='img',
                                  processors=[Transpose(Transpose.AUTO),
                                              ResizeToFit(640, 480, False),
                                              ResizeCanvas(640, 480, anchor=Anchor.CENTER)
                                              ],
                                  format='JPEG',
                                  options={'quality': 75})

nex2hex avatar Jul 29 '15 11:07 nex2hex

see more docs here https://django-imagekit.readthedocs.org/en/latest/#defining-specs-in-models

nex2hex avatar Jul 29 '15 11:07 nex2hex

i've read this docs. I have two question -

  1. how can i create ImageSpecField with storage attribute to store second image for example on other server (not to MEDIA dir)
  2. I've try to use ImageSpecField with source='img'. as i know, field is create Image from ProcessedImageField (which already has been procceced). I want to proccess second image with request.DATA. For example, say we upload img with 300x300. When we ResizeCanvas 1280, we have (1280-300)/2 = 490 white border (first image) Second img 640: if we use ResizeToFit from "source", then we have 490/2=245 white border but if we use ResizeCanvas from request.DATA, then we have (640-300)/2=170 white border (it is better) We can use django ImageField for source, but i don’t want to keep the original image.

LennyLip avatar Aug 03 '15 19:08 LennyLip

  1. try to set up cachefile_storage and cachefile_backend attrs
  2. Save original image without resizing to ProcessedImageField and make 2 different ImageSpecs for thumbs

nex2hex avatar Aug 04 '15 09:08 nex2hex