# HG changeset patch # User Thierry Florac # Date 1595249619 -7200 # Node ID a731e29446ca87f49006ea30779edf411137c7cf # Parent 53322ba0ec778f06ae628aaf274be21764c11cbd Moved images rendering functions from .zmi module diff -r 53322ba0ec77 -r a731e29446ca src/pyams_file/image.py --- a/src/pyams_file/image.py Sun Jul 19 02:01:21 2020 +0200 +++ b/src/pyams_file/image.py Mon Jul 20 14:53:39 2020 +0200 @@ -10,18 +10,27 @@ # FOR A PARTICULAR PURPOSE. # -__docformat__ = 'restructuredtext' - +import random import re +import sys from io import BytesIO from PIL import Image, ImageFilter +from pyramid.renderers import render +from zope.dublincore.interfaces import IZopeDublinCore from zope.interface import implementer from zope.schema.fieldproperty import FieldProperty +from pyams_file.interfaces import IImage, IResponsiveImage, ISVGImage, IThumbnailGeometry, \ + IThumbnailer, IThumbnails +from pyams_utils.adapter import ContextAdapter, adapter_config +from pyams_utils.request import check_request +from pyams_utils.url import absolute_url + + +__docformat__ = 'restructuredtext' + from pyams_file import _ -from pyams_file.interfaces import IImage, IResponsiveImage, IThumbnailGeometry, IThumbnailer, IThumbnails -from pyams_utils.adapter import ContextAdapter, adapter_config WEB_FORMATS = ('JPEG', 'PNG', 'GIF') @@ -267,3 +276,62 @@ label = _("Large screen thumbnail") weight = 13 + + +# +# SVG utilities +# + +def render_svg(image, width=None, height=None, request=None, css_class='', img_class=''): + """Render SVG file""" + options = {'svg': image} + if width or height: + options['style'] = 'width: {0}{1}; height: {2}{3};'.format( + width, 'px' if isinstance(width, int) else '', + height, 'px' if isinstance(height, int) else '') + else: + options['style'] = '' + options['css_class'] = css_class + result = render('templates/svg-render.pt', options, request) + if img_class: + result = result.replace(''.format( + 'content': ''.format( key=cache_key, - src='{0}?_={1}'.format(absolute_url(thumbnail, request), dc.modified.timestamp()), + src='{0}?_={1}'.format(absolute_url(thumbnail, request), + dc.modified.timestamp()), title=II18n(image).query_attribute('title', request=request) ) }