Example #1
0
def img(src, width=None, height=None, method=PROPORTIONAL, alt="", **attrs):
    """Renders an image tag."""
    params = {
        "alt": alt,
        "attrs": attrs,
    }
    try:
        thumbnail = default_thumbnail_cache.get_thumbnail(
            src,
            width = width,
            height = height,
            method = method,
        )
    except ThumbnailError:
        asset = AdaptiveAsset(src)
        params.update({
            "url": asset.get_url(),
            "width": width or "",
            "height": height or "",
        })
    else:
        params.update({
            "url": thumbnail.url,
            "width": thumbnail.width,
            "height": thumbnail.height,

        })
    return params
Example #2
0
def img(src, width=None, height=None, method=PROPORTIONAL, alt="", **attrs):
    """Renders an image tag."""
    params = {
        "alt": alt,
        "attrs": attrs,
    }
    try:
        thumbnail = default_thumbnail_cache.get_thumbnail(
            src,
            width=width,
            height=height,
            method=method,
        )
    except ThumbnailError:
        asset = AdaptiveAsset(src)
        params.update({
            "url": asset.get_url(),
            "width": width or "",
            "height": height or "",
        })
    else:
        params.update({
            "url": thumbnail.url,
            "width": thumbnail.width,
            "height": thumbnail.height,
        })
    return params
 def testImageCacheForSameSizeImageLeavesImageUnmodified(self):
     asset, image_size = get_test_thumbnail_asset()
     width, height = image_size
     thumbnail = default_thumbnail_cache.get_thumbnail(asset, width, height, "resize")
     self.assertEqual(thumbnail.width, width)
     self.assertEqual(thumbnail.height, height)
     # Make sure the assets are identical.
     self.assertEqual(hashlib.sha1(default_storage.open(default_asset_cache.get_name(asset)).read()).hexdigest(), hashlib.sha1(default_storage.open(default_asset_cache.get_name(thumbnail._asset)).read()).hexdigest())
 def testImageCacheThumbnailLarger(self):
     asset, image_size = get_test_thumbnail_asset()
     width, height = image_size
     width *= 3
     height *= 2
     thumbnail = default_thumbnail_cache.get_thumbnail(asset, width, height, "proportional")
     self.assertNotEqual(thumbnail.width, width)
     self.assertEqual(thumbnail.height, height)
     # Make sure the file contents are not identical.
     self.assertEqual(hashlib.sha1(default_storage.open(default_asset_cache.get_name(asset)).read()).hexdigest(), hashlib.sha1(default_storage.open(default_asset_cache.get_name(thumbnail._asset)).read()).hexdigest())
Example #5
0
 def testImageCacheForSameSizeImageLeavesImageUnmodified(self):
     asset, image_size = get_test_thumbnail_asset()
     width, height = image_size
     thumbnail = default_thumbnail_cache.get_thumbnail(
         asset, width, height, "resize")
     self.assertEqual(thumbnail.width, width)
     self.assertEqual(thumbnail.height, height)
     # Make sure the assets are identical.
     self.assertEqual(
         hashlib.sha1(
             default_storage.open(
                 default_asset_cache.get_name(asset)).read()).hexdigest(),
         hashlib.sha1(
             default_storage.open(
                 default_asset_cache.get_name(
                     thumbnail._asset)).read()).hexdigest())
 def testGetImgTag(self):
     asset, image_size = get_test_thumbnail_asset()
     width, height = image_size
     width /= 2
     height /= 2
     thumbnail = default_thumbnail_cache.get_thumbnail(asset, width, height, "resize")
     self.assertEqual(
         Template("{% load assets %}{% get_img asset width=width height=height method='resize' as img %}{{img.url}}:{{img.width}}:{{img.height}}").render(Context({
             "asset": asset,
             "width": width,
             "height": height,
         })),
         u'{src}:{width}:{height}'.format(
             src = thumbnail.url,
             width = thumbnail.width,
             height = thumbnail.height,
         ),
     )
 def testImgTag(self):
     asset, image_size = get_test_thumbnail_asset()
     width, height = image_size
     width /= 2
     height /= 2
     thumbnail = default_thumbnail_cache.get_thumbnail(asset, width, height, "resize")
     self.assertEqual(
         Template("{% load assets %}{% img asset width=width height=height method='resize' %}").render(Context({
             "asset": asset,
             "width": width,
             "height": height,
         })),
         u'<img src="{src}" width={width} height={height} alt="">'.format(
             src = thumbnail.url,
             width = thumbnail.width,
             height = thumbnail.height,
         ),
     )
 def testGetImgTag(self):
     asset, image_size = get_test_thumbnail_asset()
     width, height = image_size
     width /= 2
     height /= 2
     thumbnail = default_thumbnail_cache.get_thumbnail(asset, width, height, "resize")
     self.assertEqual(
         Template("{% load assets %}{% get_img asset width=width height=height method='resize' as img %}{{img.url}}:{{img.width}}:{{img.height}}").render(Context({
             "asset": asset,
             "width": width,
             "height": height,
         })),
         '{src}:{width}:{height}'.format(
             src = thumbnail.url,
             width = thumbnail.width,
             height = thumbnail.height,
         ),
     )
 def testImgTag(self):
     asset, image_size = get_test_thumbnail_asset()
     width, height = image_size
     width /= 2
     height /= 2
     thumbnail = default_thumbnail_cache.get_thumbnail(asset, width, height, "resize")
     self.assertEqual(
         Template("{% load assets %}{% img asset width=width height=height method='resize' %}").render(Context({
             "asset": asset,
             "width": width,
             "height": height,
         })),
         '<img src="{src}" width={width} height={height} alt="">'.format(
             src = thumbnail.url,
             width = thumbnail.width,
             height = thumbnail.height,
         ),
     )
Example #10
0
 def testImageCacheThumbnailSmaller(self):
     asset, image_size = get_test_thumbnail_asset()
     width, height = image_size
     width /= 3
     height /= 2
     thumbnail = default_thumbnail_cache.get_thumbnail(
         asset, width, height, "proportional")
     self.assertEqual(thumbnail.width, width)
     self.assertNotEqual(thumbnail.height, height)
     # Make sure the file contents are not identical.
     self.assertNotEqual(
         hashlib.sha1(
             default_storage.open(
                 default_asset_cache.get_name(asset)).read()).hexdigest(),
         hashlib.sha1(
             default_storage.open(
                 default_asset_cache.get_name(
                     thumbnail._asset)).read()).hexdigest())
Example #11
0
 def testImageCacheResizeLarger(self):
     asset, image_size = get_test_thumbnail_asset()
     width, height = image_size
     width *= 2
     height *= 2
     thumbnail = default_thumbnail_cache.get_thumbnail(
         asset, width, height, "resize")
     self.assertEqual(thumbnail.width, width)
     self.assertEqual(thumbnail.height, height)
     # Make sure the file contents are identical.
     self.assertEqual(
         hashlib.sha1(
             default_storage.open(
                 default_asset_cache.get_name(asset)).read()).hexdigest(),
         hashlib.sha1(
             default_storage.open(
                 default_asset_cache.get_name(
                     thumbnail._asset)).read()).hexdigest())
def responsive_img(src, method=PROPORTIONAL, alt="", **attrs):
    """Renders an image tag."""
    output = {
        "sizes": [],
        "alt": alt,
        "attrs": attrs,
    }

    for size in settings.RESPONSIVE_IMAGES_SIZES:
        params = {}

        try:
            thumbnail = default_thumbnail_cache.get_thumbnail(
                src,
                width=size['width'],
                height=size['height'],
                method=method,
            )
        except ThumbnailError:
            asset = AdaptiveAsset(src)
            params.update({
                "url": asset.get_url(),
                "width": size['width'] or "",
                "height": size['height'] or "",
            })
        else:
            params.update({
                "url": thumbnail.url,
                "width": thumbnail.width,
                "height": thumbnail.height,

            })

        output['sizes'].append(params)

    return output