Skip to content

Instantly share code, notes, and snippets.

@matthewwithanm
Created March 19, 2012 23:25
Show Gist options
  • Save matthewwithanm/2128399 to your computer and use it in GitHub Desktop.
Save matthewwithanm/2128399 to your computer and use it in GitHub Desktop.
Burakk's Image Model (Simplified)
from django.db import models
from imagekit.processors import *
from imagekit.models.fields import ImageSpecField
import os
def get_thumb_processors(instance, file):
return [resize.ResizeToFill(width=instance.thumb_width,
height=instance.thumb_height)]
def cache_to_thumb(instance, path, specname, extension):
return os.path.join('uploads', 'images', 'thumbs',
'%s.jpeg' % instance.slug)
def get_path(instance, filename):
return os.path.join('uploads', 'images', 'bigs', '%s.jpeg' % instance.slug)
class Image(models.Model):
slug = models.CharField(max_length=15)
thumb_width = models.IntegerField(blank=True, null=True)
thumb_height = models.IntegerField(blank=True, null=True)
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):
super(Image, self).save(*args, **kwargs)
test = Image.objects.all()[0]
test.thumbnail.width
def __unicode__(self):
return self.slug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment