Change how we set ImageFile.MAXBLOCK

Previously, we set it to an arbitrary large number (which turned out
not to be large enough in some cases). Since we may have been
overriding something the user explicitly chose (and our value may be
overridden by another app), that probably wasn't good. After this
change, `MAXBLOCK` is only increased when it needs to be--and even
then, only temporarily.
This commit is contained in:
Matthew Tretter 2011-11-05 21:20:36 -04:00
parent 7ab1d08e49
commit 8911836b53
2 changed files with 7 additions and 7 deletions

View file

@ -2,22 +2,17 @@ import os
import datetime
from StringIO import StringIO
from django.conf import settings
from django.core.files.base import ContentFile
from django.db import models
from django.db.models.fields.files import ImageFieldFile
from django.db.models.signals import post_save, post_delete
from django.utils.encoding import force_unicode, smart_str
from imagekit.lib import Image, ImageFile
from imagekit.utils import img_to_fobj, get_spec_files, open_image, \
format_to_extension, extension_to_format, UnknownFormatError, \
UnknownExtensionError
from imagekit.processors import ProcessorPipeline
# Modify image file buffer size.
ImageFile.MAXBLOCK = getattr(settings, 'PIL_IMAGEFILE_MAXBLOCK', 256 * 2 ** 10)
class _ImageSpecMixin(object):
def __init__(self, processors=None, quality=70, format=None):

View file

@ -21,10 +21,15 @@ def img_to_fobj(img, format, **kwargs):
try:
img.save(tmp, format, **kwargs)
except IOError:
# PIL can have problems saving large JPEGs if MAXBLOCK isn't big enough,
# So if we have a problem saving, we temporarily increase it. See
# http://github.com/jdriscoll/django-imagekit/issues/50
old_maxblock = ImageFile.MAXBLOCK
ImageFile.MAXBLOCK = img.size[0] * img.size[1]
img.save(tmp, format, **kwargs)
ImageFile.MAXBLOCK = old_maxblock
try:
img.save(tmp, format, **kwargs)
finally:
ImageFile.MAXBLOCK = old_maxblock
tmp.seek(0)
return tmp