Dynamically adding processors and changing filenames #122

Closed
burakk opened this Issue Mar 25, 2012 · 3 comments

3 participants

@burakk

I still can not use imagekit without problems.
As being a designer more than a programmer i wanted my admin system's image forms can send cropped and resized image values. And imagekit will dynamically creates processors with those values.
On the form part i made it with javascript and now my forms send all values. Now i am collecting those values and use the below code to save images.

ON ADMIN SIDE WHEN THE FORM IS SUBMITTED SOMETIMES IMAGEKIT CREATES THE FILE BUT SOMETIMES DOES NOT CREATE ANY FILE. And i could not understood why the image file sometimes can not be created.

As a note i want to say that i am trying to write in detail as possible because i am sure many people will want to use an image edit library in this way. And that must be documented with a simple example.

And the code:

from django.db import models
from imagekit.processors import *
from imagekit.models.fields import ImageSpecField

#using callable upload_to, rename the file to the slug value and put it in "bigs" folder
def get_path(instance, filename):
    extension = filename.rsplit('.', 1)[1]      
    directory = "images/bigs/"+instance.album.name      
    name = instance.slug    
    return "%s/%s.%s" % (directory, name, extension)

#using callable cache_to use the new file name changed by callable upload_to method and put it in "thums" folder
def cache_to_thumb(instance, path, specname, extension):
     path="images/thumbs/"+instance.album.name+"/"+instance.filename()
     return path

#using callable processor resize the image with the form values
def get_thumb_processors(instance, file):
         return [Adjust(contrast=1.2, sharpness=1.1),
                    resize.Resize(width=instance.thumbW, height=instance.thumbH)]

class Image(models.Model):
   title =  models.CharField(max_length=15)
   slug = models.CharField(max_length=15)
    thumbW = models.IntegerField(default=50)
    thumbH = models.IntegerField(default=50)
    file = models.ImageField(upload_to=get_path)

    thumbnail = ImageSpecField(processors=get_thumb_processors,
            image_field='file', options={'quality': 90},
            cache_to=cache_to_thumb)

    def save(self, *args, **kwargs):
        #Here the important thing is that i want my imagespec and imagefield files
        #have the same name that is the slug. Maybe the problem is in here. I am changing imagefield
        #filename to slug because i want callable cache_to can get that slug name... I have tried getting slug directly 
        #in callable cache_to function but it did not work       
        self.slug = slugify(self.title) 
        super(Image, self).save(*args, **kwargs)
        test = Image.objects.all()[0]
        test.thumbnail.width

# I think that part is important because we used callable processros we have to invalidate 

@receiver(post_init, sender=Image)
def post_init_receiver(sender, instance, **kwargs):
    pass    

@receiver(post_save, sender=Image)
def post_save_receiver(sender, instance=None, created=False, raw=False, **kwargs):
    if not raw:       
        instance.thumbnail.invalidate()

Yes. I tried to be as clear as i can be, but if it seems not clear i can edit this post.

If we can solve this issue i will be glad. Because i think image-kit will be a beautiful library with valuable processors.

As always... Thank you.

@burakk

I have changed my code as below and the problems gone.

#when overriding save inside Image class i am not calling imagespec anymore
def save(self, *args, **kwargs):
        self.slug = slugify(self.title) 
        super(Image, self).save(*args, **kwargs)    


@receiver(post_save, sender=Image)
#I am calling myspec without invalidating anything
def post_save_receiver(sender, instance=None, created=False, raw=False, **kwargs):
      instance.thumbnail.url   
@domenkozar

Is it possible to add new processors in a view? I would like to add javascript cropping support.

@matthewwithanm

@burakk Sorry for the delayed response. While accessing the URL does cause the image to be generated as a side-effect, the recommended way to do this is by calling generate().

@iElectric With the newly-released 2.X series, we're aiming to make using the processors in non-model contexts much easier. Your view should be able to make use of imagekit.generators.SpecFileGenerator instead of using a model field or, if you need more flexibility, the functions in imagekit.utils. Note that these tools are undocumented and still kind of a work in progress so they may change in the future as the library grows to accommodate more use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment