SmileyChris

sorl-thumbnail ImageWithThumbnailsField

Rambling on about Django 77 months ago.

If you're using sorl-thumbnail, you may want to update svn and check out the new field I recently added.

No docs as of this moment, so just look at the source.

What, you're not afraid of source are you?

Ok, if you insist, here's a summary:

To use, just replace your current ImageField with ImageWithThumbnailsField (which you import from sorl.thumbnail.fields).

It takes an extra argument: thumbnail, which is a dictionary containing the 2-length tuple of the size and optionally a list of thumbnail options. For example, if you used this field:

MyModel(models.Model):
    ...
    photo = ImageWithThumbnailsField(
        upload_to='profiles',
        thumbnail={
            'size': (50, 50),
            'options': ('crop',)
        })

Your model instance will have two new functions:

get_photo_thumbnail
Returns a DjangoThumbnail instance for your pleasure (if you use this in a template, it'll return the full URL to the thumbnail)
get_photo_thumbnail_tag
Returns the HTML <img src="..." width="..." height="..." alt="" /> tag. You may find this useful for showing the thumbnail in admin using list_display.

If you want to use multiple thumbnails, you can also provide a extra_thumbnails dictionary:

photo = ImageWithThumbnailsField(
    upload_to='profiles',
    thumbnail={
        'size': (50, 50),
        'options': ('crop',)
    },
    extra_thumbnails={
        'admin': {
            'size': (60, 30)
        }
    })

Which would another two functions on your instance for each extra thumbnail. In this case, get_photo_admin_thumbnail and get_photo_admin_thumbnail_tag.

Leave a comment or see my other ramblings.

Comments (10)

Simon said: (76 months ago)

Thank you so much. This is perfect.

SAn said: (74 months ago)

realy cool, thanks a lot!

Daniel Jewett said: (72 months ago)

Hi Chris,
This is working well in admin list view (using get_image_thumbnail_tag(), but switching to add or change view produces an error with the following:

global name 'oldforms' is not defined

54. return [oldforms.ImageUploadField, oldforms.HiddenField] ...

▼ Local vars
Variable Value
self
<sorl.thumbnail.fields.ImageWithThumbnailsField object at 0x1977c50>

What can I do about that?

Thanks. Oh and beautiful baby!

Daniel Jewett said: (72 months ago)

Well, I added:

from django import oldforms

to 'sorl/thumbnails/fields.py'

Seems to work for now.

Regards,
Dan J.

:) Chris said: (72 months ago)

Thanks for your feedback, Daniel. Stuff like this is best tracked in the issue tracker: http://code.google.com/p/sorl-thumbnail/issues/...

cocobuster said: (45 months ago)

Hi,

I am having an issue with the thumbnail. Let say I have an image of 3000x4000, when the thumbnail is created the image orientations changes, my image was vertical, now it is horizontal.

any idea to force the resulting thumbnail to keep original orientation?

Prince said: (28 months ago)

Not able to download. No permission.

Prince said: (28 months ago)

not able to download. No Permission

Prince said: (28 months ago)

not able to download. No Permission

Prince said: (28 months ago)

not able to download. No Permission

Leave a comment