I came across several approaches for managing thumbnails of images and
then I wrote mine to allow user to specify any number of thumbnail
images to be created.

When initializing the field class, specify the dimensions that you
want to have like:

photo = ImageWithThumbsField(upload_to = 'path', dims = ((60, 60),
(120, 120), (600, 600)))


And this class takes care of creation and deletion of the images:

class ImageWithThumbsField(models.ImageField):
    def __init__(self, dims = None, *args, **kwargs):
        super(ImageWithThumbsField, self).__init__(*args, **kwargs)
        self.dims = dims

    def save_file(self, new_data, new_object, original_object, change,
rel, save):
        super(ImageWithThumbsField, self).save_file(new_data,
new_object, original_object, change, rel, save)
        directory = self.get_directory_name()
        im = Image.open('%s%s/%s' % (settings.MEDIA_ROOT,
                                     directory,
                                     new_data['image_file']
['filename']))
        for dim in self.dims:
            # resize image
            scale = min(float(dim[0])/im.size[0], float(dim[1])/
im.size[1])
            if scale > 1:
                newim = im
            else:
                width = int(im.size[0] * scale)
                height = int(im.size[1] * scale)
                newim = im.resize((width, height), Image.ANTIALIAS)
            newim.save('%s%s/%dx%d_%s' % (settings.MEDIA_ROOT,
                                          directory,
                                          dim[0], dim[1],
                                          new_data['image_file']
['filename']), im.format)

    def delete_file(self, instance):
        super(ImageWithThumbsField, self).delete_file(instance)
        (directory, filename) = os.path.split(instance.image)
        for dim in self.dims:
            try:
                os.unlink('%s%s/%dx%d_%s' % (settings.MEDIA_ROOT,
                                             directory,
                                             dim[0], dim[1],
                                             filename))
            except (IOError, OSError):
                pass

    def get_url(self, dim, filename):
        return '%s/%s/%dx%d_%s' % (settings.MEDIA_URL,
                                   self.get_directory_name(),
                                   dim[0], dim[1],
                                   filename)


What I couldn't do is to dynamically add methods to return the url of
a resized image when its dimensions are given. I want the image urls
to be accessible like img.image_60x60_url(), if a thumb with
dimensions 60x60 exists.

So, I added the get_url method that returns the url for that purpose
and tried setattr in the loop in the save_file to add those methods as
follows:

    for dim in self.dims:
        setattr(self, 'image_%dx%d_url' % (dim[0], dim[1]),
self.get_url(dim, new_data['image_file']['filename']))

and I couldn't access it in the views and templates. Then I tried
contribute_to_class but I couldn't find clear documentation of this
method and after some trial and error, I decided to ask for help.

Thanks for any suggestions...

oMat


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to