Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Moving the image scaling into a separate function and adding tests
  • Loading branch information
coleifer committed Aug 21, 2010
1 parent fa8c870 commit 99e3066
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
12 changes: 3 additions & 9 deletions oembed/providers.py
Expand Up @@ -17,7 +17,8 @@
from oembed.exceptions import OEmbedException, OEmbedHTTPException
from oembed.image_processors import image_processor
from oembed.resources import OEmbedResource
from oembed.utils import fetch_url, get_domain, mock_request, cleaned_sites, size_to_nearest, relative_to_full
from oembed.utils import (fetch_url, get_domain, mock_request, cleaned_sites,
size_to_nearest, relative_to_full, scale)


resolver = get_resolver(None)
Expand Down Expand Up @@ -469,14 +470,7 @@ def resize(self, image_field, new_width=None, new_height=None):
return (image_field.url, current_width, current_height)

# calculate ratios
width_percent = (new_width / float(current_width))
if new_height:
height_percent = (new_height / float(current_height))

if not new_height or width_percent < height_percent:
new_height = int((float(current_height) * float(width_percent)))
else:
new_width = int((float(current_width) * float(height_percent)))
new_width, new_height = scale(current_width, current_height, new_width, new_height)

# use the image_processor defined in the settings, or PIL by default
return self._meta.image_processor.resize(image_field, new_width, new_height)
Expand Down
11 changes: 10 additions & 1 deletion oembed/tests/tests/utils.py
@@ -1,7 +1,7 @@
from django.contrib.sites.models import Site

from oembed.tests.tests.base import BaseOEmbedTestCase
from oembed.utils import size_to_nearest, relative_to_full, load_class, cleaned_sites
from oembed.utils import size_to_nearest, relative_to_full, load_class, cleaned_sites, scale

class OEmbedUtilsTestCase(BaseOEmbedTestCase):
def test_size_to_nearest(self):
Expand Down Expand Up @@ -59,3 +59,12 @@ def test_cleaned_sites(self):
self.assertEquals(cleaned[mobile_site.pk][1], 'Mobile Site')
self.assertEquals(cleaned[mobile_site.pk][2], 'http://m.testsite.com')
self.assertEquals(cleaned[mobile_site.pk][0], 'https?:\/\/(?:www[^\.]*\.)?m.testsite.com')

def test_scale(self):
self.assertEqual(scale(640, 480, 320), (320, 240))
self.assertEqual(scale(640, 480, 500, 240), (320, 240))
self.assertEqual(scale(640, 480, 320, 500), (320, 240))
self.assertEqual(scale(640, 480, 320, 240), (320, 240))

self.assertEqual(scale(640, 480, 700), (640, 480))
self.assertEqual(scale(640, 480, 700, 500), (640, 480))
18 changes: 18 additions & 0 deletions oembed/utils.py
Expand Up @@ -50,6 +50,24 @@ def size_to_nearest(width=None, height=None, allowed_sizes=OEMBED_ALLOWED_SIZES,
maxheight = size[1]
return maxwidth, maxheight

def scale(width, height, new_width, new_height=None):
# determine if resizing needs to be done (will not scale up)
if width < new_width:
if not new_height or height < new_height:
return (width, height)

# calculate ratios
width_percent = (new_width / float(width))
if new_height:
height_percent = (new_height / float(height))

if not new_height or width_percent < height_percent:
new_height = int((float(height) * float(width_percent)))
else:
new_width = int((float(width) * float(height_percent)))

return (new_width, new_height)

def fetch_url(url, method='GET', user_agent='django-oembed', timeout=SOCKET_TIMEOUT):
"""
Fetch response headers and data from a URL, raising a generic exception
Expand Down

0 comments on commit 99e3066

Please sign in to comment.