What is the canonical way to get lead image

Hi everyone,

We are building an addon to add AMP support for Plone. Now we need to know what is the canonical way to get lead images at Plone content types to add lead image into our amp view.

I look for some code, there is plone.app.contenttypes LeadImage behavior, but I didn't find any example on how to use it on every content type.

Can someone help guiding me on how to do it?

Just activate the behavior on the type where you want to have it activated.

The lead image in Plone 5 usually comes from a viewlet, I think.

if you want it in your view, you can probably use something like (use your desired scale instead of context/leadimagesize and drop and leadsize and leadsize != 'none'):

1 Like

IMO, this is another example of something that was not very well though when designed; let me explain: what if I want to directly provide an image field in my content type without having to depend on the behavior?

and this is not a rare case: we have 2 different content types (News Article and Photo Gallery) that are containers and we implement the lead image as the first image in the container:

as previously experienced with the ICollection marker interface, we have an issue here.

1 Like

I agree. I've always thought the image representation of a content in a listing was a little restrictive. Perhaps there should be a generic adapter or way to ask content to provide an image representation. then different behaviours could implement how they want.

1 Like

I ended up with the following code:

def lead_image(self):
    """Return lead image information, if present. We try to guess
    the information based on field names as it's useless to try
    to deal with the ILeadImage interface.
    :returns: lead image information
    :rtype: dict or None
    """
    image = getattr(self.context, 'image', None)
    if image is None:
        return None  # no "image" field

    if not isinstance(image, NamedBlobImage):
        return None  # not a real image

    url = '{0}/@@download/image/{1}'.format(
        self.context.absolute_url(), image.filename)
    caption = getattr(self.context, 'image_caption', None)
    width, height = image.getImageSize()
    return dict(url=url, caption=caption, width=width, height=height)

An interface would be ideal, that way it could be implemented differently for different types of content.

2 Likes

exactly; and BTW I was again bitten by corner cases derived from API incompatibilities:

@thet @jensens any idea on how to simplify this?

Functions taking single context argument, registered as adapters for respective base content interfaces.

Sean

1 Like