Introduction
The python adaptiveasset example is extracted from the most popular open source projects, you can refer to the following example for usage.
Programming language: Python
Namespace/package name: optimizationsassetcache
Example#1File:
assets.pyProject:
DjangoBD/django-optimizations
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#2File:
assets.pyProject:
DjangoBD/django-optimizations
def video_img(src, width, height, method=VIDEO_PROPORTIONAL, alt="", **attrs):
"""Renders an image tag from the given video."""
params = {
"alt": alt,
"attrs": attrs,
"width": width,
"height": height,
}
try:
url = default_video_cache.get_url(src, width, height, method, format=JPEG_FORMAT)
except VideoError:
asset = AdaptiveAsset(src)
url = asset.get_url()
params["url"] = url
return params
Example#3File:
responsive_images.pyProject:
onespacemedia/django-responsive-images
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