Python Plone:缩放图像
我的plone页面模板中有以下代码:Python Plone:缩放图像,python,image,plone,Python,Image,Plone,我的plone页面模板中有以下代码: <img tal:define="folderimagetitle python:context.getField('folderimagetitle').getAccessor(context)()" tal:condition="folderimagetitle" src="" alt="" tal:replace="structure python:context.getWrappedField('folderimage').tag(co
<img
tal:define="folderimagetitle python:context.getField('folderimagetitle').getAccessor(context)()"
tal:condition="folderimagetitle"
src=""
alt=""
tal:replace="structure python:context.getWrappedField('folderimage').tag(context, alt=folderimagetitle, css_class='photo')"
/>
基本上我想显示的图像旁边的H1标签。图像将正常工作并显示,但它将显示与文本不对齐的完整图像大小。我也不想只添加高度和宽度属性,因为这会导致图像边缘参差不齐,无法很好地缩放
如何将其缩小到Plone中可用的默认大小之一,即列表、图标、平铺、拇指、迷你、预览和大尺寸?现代的方法是使用Plone.app.imaging
范例
<img tal:define="scales context/@@images;
thumbnail python: scales.scale('image', width=64, height=64);"
tal:condition="thumbnail"
tal:attributes="src thumbnail/url;
width thumbnail/width;
height thumbnail/height" />
公认的答案非常完美,但这里有一个额外的提示说明它是如何工作的: /@@images上下文可以在任何对象类型上获取,但scales.scale()的第一个参数是一个字段名,必须在该上下文上使用.getField(name)获取该字段名 因此,例如,如果您有一个画廊视图,其中显示了视图/图像上的图像列表,您可以使用:
<li tal:repeat="image view/images">
<img tal:define="scales image/@@images;
img python: scales.scale('image', width=100, height=100);"
tal:attributes="src img/url">
</li>
请特别注意,您不能仅以以下方式传递图像实例:
scales context/@@images; <--- Don't do this. You need the @@images for an Image
scales.scale(instance, width=10, height=10);
缩放上下文/@@images;
%if request.context.children.has(0, 'ATBlob'):
<%
# NB. The 'ATBlob' is the concrete Image instance
image = request.context.children.nth(0, 'ATBlob')
scales = image.restrictedTraverse('@@images')
scaled_image = scales.scale('image', width=100, height=100)
%>
${scaled_image}
%endif