Skip to content
This repository has been archived by the owner on Sep 22, 2020. It is now read-only.

Commit

Permalink
Fix #15, add support for base64encoded strings as source
Browse files Browse the repository at this point in the history
  • Loading branch information
relekang committed Mar 28, 2015
1 parent 7ff0a2e commit f1a328a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
14 changes: 8 additions & 6 deletions t.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from tests import data
import thumbnails
from thumbnails.conf import settings

Expand All @@ -7,9 +8,10 @@

print(settings.THUMBNAIL_PATH)

print(thumbnails.get_thumbnail(URL, '800x800'))
print(thumbnails.get_thumbnail(URL, '800'))
print(thumbnails.get_thumbnail(URL, 'x800'))
print(thumbnails.get_thumbnail(URL, '400x400', 'center'))
print(thumbnails.get_thumbnail(URL, '400x400', 'top'))
print(thumbnails.get_thumbnail(URL, '400x400', 'left'))
print(thumbnails.get_thumbnail(URL, '800x800').url)
print(thumbnails.get_thumbnail(URL, '800').url)
print(thumbnails.get_thumbnail(URL, 'x800').url)
print(thumbnails.get_thumbnail(URL, '400x400', crop='center').url)
print(thumbnails.get_thumbnail(URL, '400x400', crop='top').url)
print(thumbnails.get_thumbnail(URL, '400x400', crop='left').url)
print(thumbnails.get_thumbnail(data.BASE64_STRING_OF_IMAGE, '200x200', crop='center').url)
7 changes: 7 additions & 0 deletions tests/data.py

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions tests/test_images.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# -*- coding: utf-8 -*-
import hashlib
import os
import unittest
from PIL import Image
from thumbnails.compat import BytesIO

from thumbnails.conf import settings
from thumbnails.images import SourceFile, Thumbnail

from . import data
from .compat import mock
from .utils import has_installed

Expand Down Expand Up @@ -64,3 +68,12 @@ def test_django_image_files(self):
self.assertEqual(f.file, self.FILE_PATH)
f = SourceFile(files.ImageFieldFile(field=field, instance=None, name=self.FILE_PATH))
self.assertEqual(f.file, self.FILE_PATH)

def test_base64_encoded_string(self):
file = SourceFile(data.BASE64_STRING_OF_IMAGE)
self.assertEqual(
hashlib.sha1(file.open().getvalue()).hexdigest(),
'6666212f5302426c845ecb2a2901fae021735f24'
)
image = Image.open(BytesIO(file.open().read()))
self.assertIsNotNone(image.load())
3 changes: 2 additions & 1 deletion thumbnails/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def get_thumbnail(original, size, **options):
Creates or gets an already created thumbnail for the given image with the given size and
options.
:param original: File-path or url to the image that you want an thumbnail of.
:param original: File-path, url or base64-encoded string of the image that you want an
thumbnail.
:param size: String with the wanted thumbnail size. On the form: ``200x200``, ``200`` or
``x200``.
Expand Down
4 changes: 4 additions & 0 deletions thumbnails/images.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# -*- coding: utf-8 -*-
import base64
import os

import requests
from thumbnails.compat import BytesIO

from thumbnails.conf import settings
from thumbnails.helpers import get_engine, get_storage_backend
Expand Down Expand Up @@ -80,4 +82,6 @@ def __init__(self, source_file):
def open(self):
if self.file.startswith('http'):
return requests.get(self.file, stream=True).raw
elif self.file.startswith(r'data:image/'):
return BytesIO(base64.b64decode(self.file.replace('data:image/jpeg;base64,', '')))
return get_storage_backend().open(self.file)

0 comments on commit f1a328a

Please sign in to comment.