Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import uuid | ||
import os | ||
import shutil | ||
import sys | ||
import urlparse | ||
import re | ||
import hashlib | ||
import logging | ||
|
||
from lxml import html | ||
from PIL import Image, ImageFile | ||
|
@@ -16,6 +16,11 @@ | |
|
||
ImageFile.MAXBLOCKS = 10000000 | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class ImageResizeError(Exception): | ||
pass | ||
|
||
|
||
def match_or_none(string, rx): | ||
""" | ||
|
@@ -174,6 +179,9 @@ def transcode_to_jpeg(image, path, width, height): | |
new_image.save(new_path, quality=80, optimize=1) | ||
return new_path | ||
|
||
def _get_resize_error_message(path): | ||
return 'There was a problem resizing this image: {0}'.format(os.path.split(path)[1]) | ||
|
||
def re_render(path, width, height): | ||
""" | ||
Given an original image, width, and height, creates a thumbnailed image | ||
|
@@ -193,6 +201,7 @@ def re_render(path, width, height): | |
@return: Path to the 'rendered' image. | ||
@rtype: "/path/to/image" | ||
""" | ||
|
||
try: | ||
image = Image.open(path) | ||
except IOError: | ||
|
@@ -230,15 +239,21 @@ def re_render(path, width, height): | |
return new_path | ||
|
||
# Re-render the image, optimizing for filesize | ||
new_image = image.resize((width, height), Image.ANTIALIAS) | ||
try: | ||
new_image = image.resize((width, height), Image.ANTIALIAS) | ||
except TypeError: | ||
raise ImageResizeError(_get_resize_error_message(path)), None, sys.exc_info()[2] | ||
|
||
image_params = {} | ||
if image.format != "GIF": | ||
image_params = dict( | ||
quality = 80, | ||
optimize = 1, | ||
) | ||
new_image.save(new_path, **image_params) | ||
try: | ||
new_image.save(new_path, **image_params) | ||
except IOError: | ||
raise ImageResizeError(_get_resize_error_message(path)), None, sys.exc_info()[2] | ||
This comment has been minimized.
Sorry, something went wrong. |
||
return new_path | ||
|
||
def get_html_tree(content): | ||
|
@@ -258,17 +273,27 @@ def resize_images(post_content): | |
@return: Modified contents. | ||
@rtype: basestring | ||
""" | ||
|
||
# Get tree | ||
tree = get_html_tree(post_content) | ||
|
||
# Get images | ||
imgs = tree.xpath('//img[starts-with(@src, "%s")]' % settings.STATIC_URL) | ||
for img in imgs: | ||
orig_url = img.attrib['src'] | ||
orig_path = get_local_path(orig_url) | ||
|
||
width, height = get_dimensions(img) | ||
rendered_path = re_render(orig_path, width, height) | ||
try: | ||
rendered_path = re_render(orig_path, width, height) | ||
except ImageResizeError: | ||
raise | ||
except Exception as e: | ||
# If something goes wrong, just use the original path so as not to | ||
# interrupt the user. However, log it, because it's still a real problem. | ||
logger.error(e, exc_info=True, extra={ | ||
'stack': True, | ||
}) | ||
rendered_path = orig_path | ||
This comment has been minimized.
Sorry, something went wrong.
fdintino
Member
|
||
|
||
# If we haven't changed the image, move along. | ||
if rendered_path == orig_path: | ||
|
It might help to have some information about the exception passed along. Something I used to do in cropduster was catch
IOError
, then doThe rationale behind this is that there could be other, unknown
IOError
exceptions that we should log rather than present to the user.For the same reason I might consider passing the message of the
TypeError
above along to the user after the boilerplate "There was a problem resizing this image"