我有灵巧型,图像字段定义如下:
image = NamedBlobImage(
title=_(u'Lead Image'),
description=_(u"Upload a Image of Size 230x230."),
required=True,
)
如何添加验证器来检查上传的图像文件?例如,如果图像宽度超过 500 像素,则警告用户上传另一个文件。感谢提示或示例代码。
最佳答案
您要设置约束函数:
from zope.interface import Invalid
from foo.bar import MessageFactory as _
def imageSizeConstraint(value):
# value implements the plone.namedfile.interfaces.INamedBlobImageField interface
width, height = value.getImageSize()
if width > 500 or height > 500:
raise Invalid(_(u"Your image is too large"))
然后将该函数设置为 NamedBlobImage
字段的约束
:
image = NamedBlobImage(
title=_(u'Lead Image'),
description=_(u"Upload a Image of Size 230x230."),
constraint=imageSizeConstraint,
required=True,
)
请参阅Dexterity manual on validation欲了解更多信息,以及 plone.namedfile
interface definitions .
关于plone - 特定宽度/高度的图像字段验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15136352/