plone - 特定宽度/高度的图像字段验证器

标签 plone dexterity

我有灵巧型,图像字段定义如下:

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/

相关文章:

plone - 打包Plone数据

plone - 重启时丢失一些 z3c 关系数据

testing - 如何在测试中启用敏捷行为?

python - 使用条件规则执行随机 python 脚本 Zope/Plone

plone - ConnectionStateError 的可能原因是什么?

Plone Dexterity Behaviors 可引用的行为不可引用?

plone - 将 "news item"样式缩略图添加到 Dexterity 内容类型

plone - 如何从浏览器 View 访问 z3c.form 小部件设置

Plone 工作流 : Publish an object, 以及所有使用/引用的对象

plone - 如何修复 zc.relation 目录中的 KeyError