def admin_thumb(self): if self.image is None: return "" from mezzanine.core.templatetags.mezzanine_tags import thumbnail thumb_url = thumbnail(self.image, 24, 24) return "<img src='%s%s' />" % (settings.MEDIA_URL, thumb_url)
def test_thumbnail_generation(self): """ Test that a thumbnail is created and resized. """ try: from PIL import Image except ImportError: return image_name = "image.jpg" size = (24, 24) copy_test_to_media("mezzanine.core", image_name) thumb_name = os.path.join( settings.THUMBNAILS_DIR_NAME, image_name, image_name.replace(".", "-%sx%s." % size), ) thumb_path = os.path.join(settings.MEDIA_ROOT, thumb_name) thumb_image = thumbnail(image_name, *size) self.assertEqual(os.path.normpath(thumb_image.lstrip("/")), thumb_name) self.assertNotEqual(os.path.getsize(thumb_path), 0) thumb = Image.open(thumb_path) self.assertEqual(thumb.size, size) # Clean up. del thumb os.remove(os.path.join(settings.MEDIA_ROOT, image_name)) os.remove(os.path.join(thumb_path)) rmtree(os.path.join(os.path.dirname(thumb_path)))
def thumbnails(html): """ Given a HTML string, converts paths in img tags to thumbnail paths, using Mezzanine's ``thumbnail`` template tag. Used as one of the default values in the ``RICHTEXT_FILTERS`` setting. """ from django.conf import settings from bs4 import BeautifulSoup from mezzanine.core.templatetags.mezzanine_tags import thumbnail # If MEDIA_URL isn't in the HTML string, there aren't any # images to replace, so bail early. if settings.MEDIA_URL.lower() not in html.lower(): return html dom = BeautifulSoup(html, "html.parser") for img in dom.findAll("img"): src = img.get("src", "") src_in_media = src.lower().startswith(settings.MEDIA_URL.lower()) width = img.get("width") height = img.get("height") if src_in_media and str(width).isdigit() and str(height).isdigit(): img["src"] = settings.MEDIA_URL + thumbnail(src, width, height) # BS adds closing br tags, which the browser interprets as br tags. return str(dom).replace("</br>", "")
def test_thumbnail_generation(self): """ Test that a thumbnail is created and resized. """ try: from PIL import Image except ImportError: return image_name = "image.jpg" size = (24, 24) copy_test_to_media("mezzanine.core", image_name) thumb_name = os.path.join(settings.THUMBNAILS_DIR_NAME, "thumbs-%s" % image_name, image_name.replace(".", "-%sx%s." % size)) thumb_path = os.path.join(settings.MEDIA_ROOT, thumb_name) thumb_image = thumbnail(image_name, *size) self.assertEqual(os.path.normpath(thumb_image.lstrip("/")), thumb_name) self.assertNotEqual(os.path.getsize(thumb_path), 0) thumb = Image.open(thumb_path) self.assertEqual(thumb.size, size) # Clean up. del thumb os.remove(os.path.join(settings.MEDIA_ROOT, image_name)) os.remove(os.path.join(thumb_path)) rmtree(os.path.join(os.path.dirname(thumb_path)))
def thumbnails(html): """ Given a HTML string, converts paths in img tags to thumbnail paths, using Mezzanine's ``thumbnail`` template tag. Used as one of the default values in the ``RICHTEXT_FILTERS`` setting. """ from django.conf import settings from html5lib import parse from mezzanine.core.templatetags.mezzanine_tags import thumbnail dom = parse(html, treebuilder="dom") for img in dom.getElementsByTagName("img"): src = img.getAttribute("src") width = img.getAttribute("width") height = img.getAttribute("height") if src and width and height: src = settings.MEDIA_URL + thumbnail(src, width, height) img.setAttribute("src", src) output = [] for parent in ("head", "body"): for node in dom.getElementsByTagName(parent)[0].childNodes: output.append(node.toxml()) if node.localName == "script": output.append("</script>") return "".join(output)
def image(content, width, height): if not content.image_scaling: return content.image if content.image_width or content.image_height: width = content.image_width height = content.image_height return thumbnail(content.image, width, height, int(content.image_quality))
def get_thumbnail_url(self): """ A url that serves the thumbnail of the image (Creates one if none yet exists) """ # FIXME: Make the parameters configurable return settings.MEDIA_URL + thumbnail(self.get_absolute_url(), 400, 200)
def get_thumb_url(self): thumb = None if self.admin_thumb_field: thumb = getattr(self, self.admin_thumb_field, None) if thumb is None: return "" return "%s%s" % (settings.MEDIA_URL, thumbnail(thumb, self.width, self.height, self.quality))
def render(self, name, value, attrs): rendered = super(ImageWidget, self).render(name, value, attrs) if value: orig = u"%s%s" % (settings.MEDIA_URL, value) thumb = u"%s%s" % (settings.MEDIA_URL, thumbnail(value, 48, 48)) rendered = (u"<a target='_blank' href='%s'>" u"<img style='margin-right:6px;' src='%s'>" u"</a>%s" % (orig, thumb, rendered)) return mark_safe(rendered)
def get_thumb_url(self): from mezzanine.core.templatetags.mezzanine_tags import thumbnail thumb = None if self.admin_thumb_field: thumb = getattr(self, self.admin_thumb_field, None) if thumb is None: return "" return "%s%s" % (settings.MEDIA_URL, thumbnail(thumb, self.width, self.height, self.quality))
def admin_thumb(self): thumb = None if self.admin_thumb_field: thumb = getattr(self, self.admin_thumb_field, None) if thumb is None: return "" from mezzanine.core.templatetags.mezzanine_tags import thumbnail thumb_url = thumbnail(thumb, 24, 24) return "<img src='%s%s'>" % (settings.MEDIA_URL, thumb_url)
def admin_thumb(self): thumb = None if self.admin_thumb_field: thumb = getattr(self, self.admin_thumb_field, None) if thumb is None: return "" from mezzanine.core.templatetags.mezzanine_tags import thumbnail x, y = settings.ADMIN_THUMB_SIZE.split('x') thumb_url = thumbnail(thumb, x, y) return "<img src='%s%s'>" % (settings.MEDIA_URL, thumb_url)
def render(self, name, value, attrs): rendered = super(ImageWidgetBase, self).render(name, value, attrs) if value: orig = '%s%s' % (settings.MEDIA_URL, value) thumb = '%s%s' % (settings.MEDIA_URL, thumbnail(value, 48, 48)) rendered = ('<a href="%s"> target="_blank"' '<img style="margin-right: 6px;" src="%s">' '</a><span class="clearable-image">%s</span>' % (orig, thumb, rendered)) return mark_safe(rendered)
def admin_thumb(self): thumb = "" if self.admin_thumb_field: thumb = getattr(self, self.admin_thumb_field, "") if not thumb: return "" from mezzanine.conf import settings from mezzanine.core.templatetags.mezzanine_tags import thumbnail x, y = settings.ADMIN_THUMB_SIZE.split('x') thumb_url = thumbnail(thumb, x, y) return format_html("<img src='{}{}'>", settings.MEDIA_URL, thumb_url)
def admin_thumb(self): thumb = "" if self.admin_thumb_field: thumb = getattr(self, self.admin_thumb_field, "") if not thumb: return "" from mezzanine.conf import settings from mezzanine.core.templatetags.mezzanine_tags import thumbnail x, y = 160, 90 thumb_url = thumbnail(thumb, x, y) return "<img src='%s%s'>" % (settings.MEDIA_URL, thumb_url)
def admin_thumb(self): thumb = "" if self.admin_thumb_type: images = self.images.filter(type=self.admin_thumb_type) if images: thumb = images[0].file if not thumb: return "" from mezzanine.conf import settings from mezzanine.core.templatetags.mezzanine_tags import thumbnail x, y = settings.ADMIN_THUMB_SIZE.split('x') thumb_url = thumbnail(thumb, x, y) return "<img src='%s%s'>" % (settings.MEDIA_URL, thumb_url)
def get_thumb_url(self): thumb = None if self.admin_thumb_field: thumb = getattr(self, self.admin_thumb_field, None) if thumb is None: return "" url = thumbnail(thumb.path, self.width, self.height, self.quality) # When using differing storage backends, # such as Boto and S3 appears that file path # can be stored as absolute rather than relative path url_obj = urlparse(url) if url_obj.scheme not in ['http', 'https']: url = "%s%s" % (settings.MEDIA_URL, url) return url
def makeTag(self, href, title, text): from mezzanine.core.templatetags.mezzanine_tags import thumbnail el = util.etree.Element("img") small_thumbnail_href = settings.MEDIA_URL + thumbnail(href, 30, 0) el.set("src", small_thumbnail_href) if title: el.set("title", title) if self.markdown.enable_attributes: text = handleAttributes(text, el) el.set("alt", self.unescape(text)) el.set('class', 'img-responsive blog-detail-featured-image progressive__img progressive--not-loaded') el.set("data-progressive", self.sanitize_url(href)) return el
def thumbnails(html): """ Given a HTML string, converts paths in img tags to thumbnail paths, using Mezzanine's ``thumbnail`` template tag. Used as one of the default values in the ``RICHTEXT_FILTERS`` setting. """ from django.conf import settings from bs4 import BeautifulSoup from mezzanine.core.templatetags.mezzanine_tags import thumbnail dom = BeautifulSoup(html) for img in dom.findAll("img"): src = img.get("src", "") src_in_media = src.lower().startswith(settings.MEDIA_URL.lower()) width = img.get("width") height = img.get("height") if src_in_media and width and height: img["src"] = settings.MEDIA_URL + thumbnail(src, width, height) return str(dom)
def test_thumbnail_generation(self): """ Test that a thumbnail is created. """ original_name = "testleaf.jpg" if not os.path.exists(os.path.join(settings.MEDIA_ROOT, original_name)): return thumbnail_name = "testleaf-24x24.jpg" thumbnail_path = os.path.join(settings.MEDIA_ROOT, thumbnail_name) try: os.remove(thumbnail_path) except OSError: pass thumbnail_image = thumbnail(original_name, 24, 24) self.assertEqual(thumbnail_image.lstrip("/"), thumbnail_name) self.assertNotEqual(0, os.path.getsize(thumbnail_path)) try: os.remove(thumbnail_path) except OSError: pass
def test_thumbnail_generation(self): """ Test that a thumbnail is created. """ orig_name = "testleaf.jpg" if not os.path.exists(os.path.join(settings.MEDIA_ROOT, orig_name)): return thumbnail_name = "testleaf-24x24.jpg" thumbnail_path = os.path.join(settings.MEDIA_ROOT, thumbnail_name) try: os.remove(thumbnail_path) except OSError: pass thumbnail_image = thumbnail(orig_name, 24, 24) self.assertEqual(thumbnail_image.lstrip("/"), thumbnail_name) self.assertNotEqual(0, os.path.getsize(thumbnail_path)) try: os.remove(thumbnail_path) except OSError: pass
def thumbnails(html): """ Given a HTML string, converts paths in img tags to thumbnail paths, using Mezzanine's ``thumbnail`` template tag. Used as one of the default values in the ``RICHTEXT_FILTERS`` setting. """ from django.conf import settings from html5lib.treebuilders import getTreeBuilder from html5lib.html5parser import HTMLParser from mezzanine.core.templatetags.mezzanine_tags import thumbnail dom = HTMLParser(tree=getTreeBuilder("dom")).parse(html) for img in dom.getElementsByTagName("img"): src = img.getAttribute("src") width = img.getAttribute("width") height = img.getAttribute("height") if src and width and height: src = settings.MEDIA_URL + thumbnail(src, width, height) img.setAttribute("src", src) nodes = dom.getElementsByTagName("body")[0].childNodes return "".join([node.toxml() for node in nodes])
def makeTag(self, href, title, text): from mezzanine.core.templatetags.mezzanine_tags import thumbnail el = util.etree.Element("img") small_thumbnail_href = settings.MEDIA_URL + thumbnail(href, 30, 0) el.set("src", small_thumbnail_href) if title: el.set("title", title) if self.markdown.enable_attributes: text = handleAttributes(text, el) el.set("alt", self.unescape(text)) el.set( 'class', 'img-responsive blog-detail-featured-image progressive__img progressive--not-loaded' ) el.set("data-progressive", self.sanitize_url(href)) return el
def thumbnails(html): """ Given a HTML string, converts paths in img tags to thumbnail paths, using Mezzanine's ``thumbnail`` template tag. Used as one of the default values in the ``RICHTEXT_FILTERS`` setting. """ from django.conf import settings from bs4 import BeautifulSoup from mezzanine.core.templatetags.mezzanine_tags import thumbnail # If MEDIA_URL isn't in the HTML string, there aren't any # images to replace, so bail early. if settings.MEDIA_URL.lower() not in html.lower(): return html dom = BeautifulSoup(html, "html.parser") for img in dom.findAll("img"): src = img.get("src", "") # check for gif if src[-3:] == 'gif': #check if gif is animated, don't thumbnail if animated gif = Image.open(src) try: gif.seek(1) except EOFError: gif.close() else: gif.close() continue src_in_media = src.lower().startswith(settings.MEDIA_URL.lower()) width = img.get("width") height = img.get("height") if src_in_media and width and height: img["src"] = settings.MEDIA_URL + thumbnail(src, width, height) # BS adds closing br tags, which the browser interprets as br tags. return str(dom).replace("</br>", "")
def make_thumb(image_abs_list, image_dir): MAXWIDTH = 980 MAXHEIGHT = 600 DELTA = 50 thumb_dir = os.path.join(image_dir, settings.THUMBNAILS_DIR_NAME) thumb_list = [] for image_name in image_abs_list: image_base = os.path.basename(image_name) img = Image.open(image_name) w, h = img.size if w == h or abs(w - h) < DELTA: size = (MAXHEIGHT, MAXHEIGHT) elif w > h: #landscape image if w / float(h) > 1.5: size = (MAXWIDTH, 0) else: size = (0, MAXHEIGHT) else: size = (0, MAXHEIGHT) image_base = os.path.splitext(image_base) thumb_image = os.path.join(thumb_dir, image_base[0]+ "-%sx%s" % size+ image_base[1]) if not os.path.exists(thumb_image): print "Processing", thumb_image thumb_image = thumbnail(image_name, *size) thumb_list.append(os.path.basename(thumb_image)) #else: print "skipping", thumb_image ### Move original file orig_dir = os.path.join(image_dir, 'orig') if not os.path.exists(orig_dir): os.makedirs(orig_dir) shutil.move(image_name, orig_dir) return thumb_list
def gallery_get_front_image(page): image = GalleryImage.objects.filter(gallery=page).first() print "Estae s: image", image.file image_url = "%s%s" % (MEDIA_URL, thumbnail(image.file, 230, 157)) return image_url
def get_thumbnail(self, obj): return settings.MEDIA_URL + thumbnail(obj.filename, 420, 260, 95, .5, .5, True, '#25286b');