mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-04-22 14:04:43 +00:00
Spec.process now only optionally saves the new image to disk
This commit is contained in:
parent
9662ec2be0
commit
390e60c558
4 changed files with 32 additions and 17 deletions
|
|
@ -0,0 +1,10 @@
|
|||
"""
|
||||
|
||||
Django ImageKit
|
||||
|
||||
Author: Justin Driscoll <justin.driscoll@gmail.com>
|
||||
Version: 1.0
|
||||
|
||||
"""
|
||||
from models import IKModel
|
||||
from specs import ImageSpec
|
||||
|
|
@ -4,6 +4,7 @@ from django.conf import settings
|
|||
from django.db import models
|
||||
from django.db.models.base import ModelBase
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from imagekit.options import Options
|
||||
from imagekit import specs
|
||||
|
||||
|
|
|
|||
|
|
@ -17,24 +17,27 @@ class ImageSpec(object):
|
|||
return getattr(cls, 'access_as', cls.__name__.lower())
|
||||
|
||||
@classmethod
|
||||
def create(cls, image, filename):
|
||||
def process(cls, image, save_as=None):
|
||||
processed_image = image.copy()
|
||||
for proc in cls.processors:
|
||||
processed_image = proc.process(processed_image)
|
||||
try:
|
||||
if image.format != 'JPEG':
|
||||
try:
|
||||
processed_image.save(im_filename)
|
||||
return
|
||||
except KeyError:
|
||||
pass
|
||||
processed_image.save(filename, 'JPEG',
|
||||
quality=int(cls.output_quality),
|
||||
optimize=True)
|
||||
except IOError, e:
|
||||
if os.path.isfile(filename):
|
||||
os.unlink(filename)
|
||||
raise e
|
||||
|
||||
if save_as is not None:
|
||||
try:
|
||||
if image.format != 'JPEG':
|
||||
try:
|
||||
processed_image.save(save_as)
|
||||
return
|
||||
except KeyError:
|
||||
pass
|
||||
processed_image.save(save_as, 'JPEG',
|
||||
quality=int(cls.output_quality),
|
||||
optimize=True)
|
||||
except IOError, e:
|
||||
if os.path.isfile(filename):
|
||||
os.unlink(filename)
|
||||
raise e
|
||||
|
||||
return processed_image
|
||||
|
||||
|
||||
|
|
@ -45,7 +48,7 @@ class Accessor(object):
|
|||
self.spec = spec
|
||||
|
||||
def create(self):
|
||||
self._img = self.spec.create(self.image, self.path)
|
||||
self._img = self.spec.process(self.image, save_as=self.path)
|
||||
|
||||
def delete(self):
|
||||
if self.exists:
|
||||
|
|
|
|||
|
|
@ -5,7 +5,8 @@ from django.conf import settings
|
|||
from django.core.files.base import ContentFile
|
||||
from django.test import TestCase
|
||||
|
||||
from models import *
|
||||
from models import IKModel
|
||||
from specs import ImageSpec
|
||||
|
||||
|
||||
# Required PIL classes may or may not be available from the root namespace
|
||||
|
|
|
|||
Loading…
Reference in a new issue