def image_preview(request, app_label, model, id, size): """ Grab all image link within a peice of content and generate thumbnails of largest image """ # get page object; protect size try: content_type = ContentType.objects.get(app_label=app_label, model=model) instance = content_type.get_object_for_this_type(id=id) except: return HttpResponseNotFound(_("Image not found."), content_type="text/plain") keys = [ settings.CACHE_PRE_KEY, IMAGE_PREVIEW_CACHE, model, str(instance.id), size ] key = '.'.join(keys) response = cache.get(key) if not response: from tendenci.apps.base.utils import make_image_object_from_url, image_rescale # set sizes size_min = (30, 30) size_cap = (512, 512) size_tuple = size.split('x') if len(size_tuple) == 2: size = int(size_tuple[0]), int(size_tuple[1]) else: size = int(size), int(size) if size > size_cap: size = size_cap image_urls = [] image = Pil.new('RGBA', size_min) # find biggest image, dimension-wise for image_url in image_urls: image_candidate = make_image_object_from_url(image_url) if image_candidate: if image_candidate.size > image.size: image = image_candidate if image.size[1] > size_min[1]: # rescale, convert-to true-colors; return response image = image_rescale(image, size) if image.mode != "RGB": image = image.convert("RGB") response = HttpResponse(content_type='image/jpeg') image.save(response, "JPEG", quality=100) keys = [ settings.CACHE_PRE_KEY, IMAGE_PREVIEW_CACHE, model, str(instance.id), size ] key = '.'.join(keys) cache.set(key, response) return response else: # raise http 404 error (returns page not found) return HttpResponseNotFound(_("Image not found."), content_type="text/plain") else: return response
def image_preview(request, app_label, model, id, size): """ Grab all image link within a peice of content and generate thumbnails of largest image """ # get page object; protect size try: content_type = ContentType.objects.get(app_label=app_label, model=model) instance = content_type.get_object_for_this_type(id=id) except: return HttpResponseNotFound(_("Image not found."), content_type="text/plain") keys = [settings.CACHE_PRE_KEY, IMAGE_PREVIEW_CACHE, model, str(instance.id), size] key = '.'.join(keys) response = cache.get(key) original_size = size if not response: from tendenci.apps.base.utils import parse_image_sources, make_image_object_from_url, image_rescale # set sizes size_min = (30,30) size_cap = (512,512) size_tuple = size.split('x') if len(size_tuple) == 2: size = int(size_tuple[0]), int(size_tuple[1]) else: size = int(size), int(size) if size > size_cap: size = size_cap image_urls = [] image = Pil.new('RGBA',size_min) # find biggest image, dimension-wise for image_url in image_urls: image_candidate = make_image_object_from_url(image_url) if image_candidate: if image_candidate.size > image.size: image = image_candidate if image.size[1] > size_min[1]: # rescale, convert-to true-colors; return response image = image_rescale(image, size) if image.mode != "RGB": image = image.convert("RGB") response = HttpResponse(content_type='image/jpeg') image.save(response, "JPEG", quality=100) keys = [settings.CACHE_PRE_KEY, IMAGE_PREVIEW_CACHE, model, str(instance.id), size] key = '.'.join(keys) cache.set(key, response) return response else: # raise http 404 error (returns page not found) return HttpResponseNotFound(_("Image not found."), content_type="text/plain") else: return response