4

The users do it because they can.

However, image auto-resize etc. breaks down.

This make me a sad boy.

How to limit image uploads to GIF, PNG and JPEG sitewide?

  • For Archetypes

  • For Dexterity

ggozad
  • 13,105
  • 3
  • 40
  • 49
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • Isn't it more useful to allow any kind of image and use a transform to have imagemagick, convert your BMPs to PNGs? I'm a bit surprised this isn't automatic. – Auspex Feb 03 '12 at 15:06
  • sidewide limitiation for AT: Check http://stackoverflow.com/questions/15951825/how-to-restrict-image-file-extension-on-plone/17941158#17941158 – Mathias Jul 30 '13 at 07:51

3 Answers3

5

Using Archetypes you override the image content class or create your own custom image content class with the following schema.

You can just add the line

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

to your schema

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,
             ),
    ),

I would probably use a schema extender to extend the Image class, overriding that particular field

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

Purple Hexagon
  • 3,538
  • 2
  • 24
  • 45
  • Sorry, had to unmark this answer as correct. For some reason, allowable_content_types is being ignored. Plone 3.3. This is also default value for ATImage. – Mikko Ohtamaa Feb 06 '12 at 21:16
1

i ran into similar problems these days and worked around them like that:

  • add a custom widget that adds an accept attribute to the file input
  • set field.swallowResizeExceptions = True so users at least don't get a site-error when uploading an unsopported image type
  • state mimetypes that work in description

The field definition looks like this:

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',
        ),
    ),

note that accept="image/jpeg,image/gif"was ignored by firefox11 although it sould be supported according to http://www.w3schools.com/tags/att_input_accept.asp

mywidgets/myimage is a customized version of archetypes/skins/widgets/image.pt that uses a customized version of archetypes/skins/widgets/file.pt

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

and mywidgets/myfile.pt simply defines this macro:

<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>
fRiSi
  • 1,238
  • 6
  • 13
0

Side-wide restriction for AT using the post validation event:

check How to restrict image file extension on Plone?

Community
  • 1
  • 1
Mathias
  • 6,777
  • 2
  • 20
  • 32
  • just seeing it's a stackoverflow link... if the question is a exact duplicate, it should be flagged as such (is it?) Otherwise, the answer might not fit this question :-) – kleopatra Jul 30 '13 at 08:10
  • The answer fits for the Archetype part, but no for Dexterity. – Mathias Aug 03 '13 at 21:03