(画像系のコンテンツ)

はじめに

Plone provides image upload in two ways

  • As content type, will be visible in the sitemap. This is the default “Image” content, but you can add your custom content type.
  • As a field. Then the image is directly associated with one content object. Use Archetypes.fields.ImageField.

Custom image content type

If you want to have your custom content type behave like Plone stock Image content type.

  • Inherit content class from Products.ATContentType.content.image.ATImage and use schema correspondingly
  • Make your GenericSetup types XML to look like Image.xml.
  • Do not set workflow for your type in profiles/default/workflows.xml
<?xml version="1.0"?>
<object name="portal_workflow" meta_type="Plone Workflow Tool">

 <bindings>
  <type type_id="YourImageType"/>
 </bindings>
</object>

Image scales

When the image is uploaded, both field or content, Plone creates default scaled down versions from it.

These are configured as ImageField sizes parameter. See ImageField class notes here:

Default image scales for Image content are configured in

Configuration:

sizes= {'large'   : (768, 768),
        'preview' : (400, 400),
        'mini'    : (200, 200),
        'thumb'   : (128, 128),
        'tile'    :  (64, 64),
        'icon'    :  (32, 32),
        'listing' :  (16, 16),
       },

getScale()

ImageField provides function getScale() to get the scaled version of the image based on the sizes configuration key.

See example in __bobo_traverse__

Accessing image

ImageField is mapped to traversable attribute of your content type. If your content object has field “imageOne” and content is in URL:

http://yoursite/content

Image can be directly downloaded from:

http://yoursite/content/imageOne

Scaled versions for Image content (ATImage)

If you want different scales you can add image_XXX prefix where XXX is the corresponding scale name:

http://yoursite/content/imageOne/image_preview

This hook is defined in __bobo_traverse__ in ATImage class

portal_catalog and images

Do not index image object themselves, as putting in image data to porta_catalog brain would grealy increase its site and make brain look-up slow.

Instead, index only image paths using getPhysicalPath(). When you need to display image using metadata columns, you can generate the image URL manually. Then, image object will be waken up when the browser makes a HTTP request for the image.

Custom image scales and recreating scale data

Below is an example how to make custom image scales available on your Plone site.

  • Monkey-patch ATImages to have new scale versions available
  • Have migration code which will run all through all ATImages on the site and recreate their scale versions, thus populating image scale data for new scale versions also
  • The new sizes are automatically effected to rich text editor image sizes options (active WYSIWYG editor on Plone site)

images.py:

"""

    Add alternative image sizes to default ATImage scales.

    NOTE: This does not effect available user interface options in the visual editor etc.

"""

import transaction
from zope.app.component.hooks import setHooks, setSite, getSite

from Products.Five.browser import BrowserView

from Products.ATContentTypes.content.image import ATImage
from Products.ATContentTypes.interface.image import IATImage

# Monkey patch our new image sizes to be available in ATImage default scales.
# This also will affect WYSIWYG text editor "image sizes" option.
ATImage.schema["image"].sizes.update({
    "custom1" : (290, 290),
    "custom2" : (210, 210),
})

class RescaleImages(BrowserView):
    """ Migration view to recreate all image scale versions on all Image content types on the site.

    To trigger this migration code, enter the view URL manually to the browser address bar::

            http://yourhost/site/@@rescale_images

    We assume you run Zope in foreground mode and monitor the console for the messages.

    This code is designed to work with sites with plenty of images.
    Tested with > 5000 images.

    Note that you need to run this rescale code only once to migrate the existing image conte.t
    New images will have custom scale versions available when the images are created.
    """

    def __call__(self):
        """ View processing entry point.
        """

        portal = getSite()

        # Iterate through all Image content items on the site
        all_images = portal.portal_catalog(show_inactive=True, language="ALL", object_provides=IATImage.__identifier__)

        done = 0

        for brain in all_images:

            done += 1

            content = brain.getObject()

            print "(%d / %d) creating scales for image: %s" % (done, len(all_images), "/".join(content.getPhysicalPath()))

            # This will trigger ImageField scale rebuild
            content.schema["image"].createScales(content)

            # Since this is HUGE operation (think resizing 2 GB images)
            # it is not nice idea to buffer the transaction in the memory
            # (Zope default behavior).
            # Using subtransactions we hint Zope when it would be a good time to flush the
            # changes to the disk
            # http://www.zope.org/Members/mcdonc/HowTos/transaction
            if done % 10 == 0:
                # Commit subtransaction for every 10th processed item
                transaction.get().commit(True)


        # Note that when code exists the view
        # there will huuuge delay before the message below is returned to the browser.
        # This is because Zope updates Data.fs.

        # Make simple HTTP 200 answer
        return "Recreated image scales for %d images" % len(all_images)

configure.zcml