Skip to content

Instantly share code, notes, and snippets.

@Snakeyyy
Last active August 29, 2015 14:11
Show Gist options
  • Save Snakeyyy/ae4a164401e36c5b4886 to your computer and use it in GitHub Desktop.
Save Snakeyyy/ae4a164401e36c5b4886 to your computer and use it in GitHub Desktop.
from sorl.thumbnail import get_thumbnail
from django.http import HttpResponseRedirect
def responsive_image(request):
image_path = request.GET.get('image_path')
width = int(request.GET.get('width'))
height = int(request.GET.get('height'))
is_retina = bool(request.GET.get('is_retina', False))
if is_retina:
width *= 2
height *= 2
if not image_path or not width or not height:
return HttpResponse(content='missing get parameters', status=404)
path_ = 'http://'+request.META['HTTP_HOST'] + image_path
thumb = get_thumbnail(path_, '%dx%d' % (width, height))
return HttpResponseRedirect(thumb.url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment