Skip to content

Instantly share code, notes, and snippets.

@jacobh
Created November 6, 2011 10:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobh/1342723 to your computer and use it in GitHub Desktop.
Save jacobh/1342723 to your computer and use it in GitHub Desktop.
django-imagekit
from django.db import models
from django.contrib.auth.models import User
from imagekit.models import ImageSpec
from imagekit.processors import resize, Adjust
from django.db.models.signals import post_save
from django.dispatch import receiver
from celery.task import task
from celery.execute import send_task
from base64 import urlsafe_b64encode
from hashlib import sha512
from os import urandom
from time import time
def imagefield_upload_to(instance, filename):
dir = 'photos/o/'
time_hash = urlsafe_b64encode(sha512(str(time())).digest())[:20:]
random_key = urlsafe_b64encode(sha512(urandom(100)).digest())[:5:]
extension = filename.split('.')[-1::].pop()
return dir + time_hash + random_key + '.' + extension
class Photo(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length = 100, blank=True)
description = models.TextField(blank=True)
is_active = models.BooleanField(default=False)
original_image = models.ImageField(upload_to = imagefield_upload_to)
small_thumbnail = ImageSpec(
processors = [
resize.Crop(width=100, height=100),
Adjust(sharpness=1.2),
],
image_field = 'original_image',
format = 'JPEG',
quality = 85,
cache_to = 'photos/st',
)
large_thumbnail = ImageSpec(
processors = [
resize.Crop(width=200, height=200),
Adjust(sharpness=1.2),
],
image_field = 'original_image',
format = 'JPEG',
quality = 85,
cache_to = 'photos/lt',
)
large_image = ImageSpec(
processors = [
resize.Fit(width=1024, height=1024),
Adjust(sharpness=1.2),
],
image_field = 'original_image',
format = 'JPEG',
quality = 85,
cache_to = 'photos/l',
)
medium_image = ImageSpec(
processors = [
resize.Fit(width=640, height=640),
Adjust(sharpness=1.2),
],
image_field = 'original_image',
format = 'JPEG',
quality = 85,
cache_to = 'photos/m',
)
small_image = ImageSpec(
processors = [
resize.Fit(width=240, height=240),
Adjust(sharpness=1.2),
],
image_field = 'original_image',
format = 'JPEG',
quality = 85,
cache_to = 'photos/s',
)
def __unicode__(self):
return u'%s by %s' % (self.title, self.user.username)
@task(name="photos.models.generate_resized_images")
def generate_resized_images(primary_key):
from photos.models import Photo
try:
instance = Photo.objects.get(id=primary_key)
instance.small_thumbnail._create()
instance.large_thumbnail._create()
instance.small_image._create()
instance.medium_image._create()
instance.large_image._create()
print 'success!'
except:
print 'couldn\'t resize images :/'
@receiver(post_save, sender=Photo)
def photo_postsave_handler(sender, instance, created, raw, **kwargs):
if created == True:
send_task("photos.models.generate_resized_images", [instance.id])
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'imagekit',
'djcelery',
'pages',
'photos',
'themes',
'users',
)
# Celery (For delayed jobs)
BROKER_HOST = "127.0.0.1"
BROKER_BACKEND="redis"
REDIS_PORT=6379
REDIS_HOST = "127.0.0.1"
BROKER_USER = ""
BROKER_PASSWORD =""
BROKER_VHOST = "0"
REDIS_DB = 0
REDIS_CONNECT_RETRY = True
CELERY_SEND_EVENTS = True
CELERY_RESULT_BACKEND = 'redis'
CELERY_TASK_RESULT_EXPIRES = 10
CELERYBEAT_SCHEDULER = "djcelery.schedulers.DatabaseScheduler"
import djcelery
djcelery.setup_loader()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment