4

用户这样做是因为他们可以。

但是,图像自动调整大小等故障。

这让我成为一个悲伤的男孩。

如何在全站范围内将图像上传限制为 GIF、PNG 和 JPEG?

  • 对于原型

  • 为了灵巧

4

3 回答 3

5

使用 Archetypes,您可以覆盖图像内容类或使用以下架构创建您自己的自定义图像内容类。

您只需添加该行

allowable_content_types = ('image/gif', 'image/jpeg', 'image/png'),

到您的架构

IE

MyImageSchema = schemata.ATContentTypeSchema.copy() + atapi.Schema((
        ImageField('image',
            required = False,
            allowable_content_types = ('image/gif', 'image/jpeg', 'image/png'),
            storage=AttributeStorage(),
            sizes= {'large'   : (768, 768),
                   'preview' : (400, 400),
                   'mini'    : (200, 200),
                   'thumb'   : (128, 128),
                   'tile'    :  (64, 64),
                   'icon'    :  (32, 32),
                   'listing' :  (16, 16),
                  },
          widget = ImageWidget(
                     label=_(u"Image"),
                     show_content_type=False,
             ),
    ),

我可能会使用模式扩展器来扩展 Image 类,覆盖该特定字段

http://weblion.psu.edu/services/documentation/developing-for-plone/products-from-scratch/schemaextender

于 2012-02-03T13:47:21.347 回答
1

这些天我遇到了类似的问题并像这样解决它们:

  • 添加一个自定义小部件,将accept属性添加到文件输入
  • 设置field.swallowResizeExceptions = True以便用户在上传不支持的图像类型时至少不会收到站点错误
  • 在描述中工作的状态 mimetype

字段定义如下所示:

atapi.ImageField('image1',
    swallowResizeExceptions = True,
    widget = atapi.ImageWidget(
        label = _(u"Image 1"),
        description = _(u"Image used in listings. (JPEG, PNG and GIF are supported)"),
        show_content_type = False,
        accept = 'image/*',
        macro = 'mywidgets/myimage',
        ),
    ),

请注意,accept="image/jpeg,image/gif"firefox11 忽略了这一点,尽管根据http://www.w3schools.com/tags/att_input_accept.asp应该支持它

mywidgets/myimage 是 archetypes/skins/widgets/image.pt 的自定义版本,它使用了 archetypes/skins/widgets/file.pt 的自定义版本

<metal:define define-macro="edit">
...
   <metal metal:use-macro="here/mywidgets/myfile/macros/file_upload"/>
...

而 mywidgets/myfile.pt 只定义了这个宏:

<metal:define define-macro="file_upload"
       tal:define="unit accessor;
                   size unit/get_size | python:unit and len(unit) or 0;">
    <input type="file"
           size="30"
           tal:attributes="name string:${fieldName}_file;
                           id string:${fieldName}_file;
                           accept widget/accept|nothing;" />
    <script type="text/javascript"
        tal:define="isDisabled python:test(accessor() and size!=0, 'true', 'false')"
            tal:content="string:document.getElementById('${fieldName}_file').disabled=$isDisabled;">
    </script>
</metal:define>
于 2012-03-20T09:48:05.293 回答
0

使用验证后事件对 AT 的横向限制:

检查如何限制 Plone 上的图像文件扩展名?

于 2013-07-30T07:52:41.457 回答