How can a rotating banner image be done in Plone?
The first step is to create a Python script that will serve up the image you want. This article assumes that you want a random image from a folder to be chosen. Python-savvy readers can use more advanced techniques, such as making a list of images to load in the same order, etc.
In the /plone_skins/custom folder, add a Script (Python) object. Put this code into it:
from@from random import choice#
- Import a standard function, and get the HTML request and response objects.
from Products.PythonScripts.standard import html_quote
request = container.REQUEST
RESPONSE = request.RESPONSE
RESPONSE.setHeader(‘Content-Type’,‘image/jpeg’)#
- Get list of images in random_images folder
images = context.images.random_images.objectValues([‘ATImage’])#
- Return a random choice of one of the images
return choice(images).datadata@
This assumes your images will be stored in the random_images folder in root. Change the reference to wherever your images are being stored if need be.
If you are using images other than JPEGs, you will need to change the setHeader call so its second parameter reflects the image type you’re using. For example, image/gif or image/png.
Also, the objectValues function retrieves only the file types you ask it to, in this case “ATImage”, which means images uploaded through the Plone interface. Images uploaded through the ZMI have a different file type, “Image”. This is an important distinction, so change the parameter if you need to. An alternative is to leave the parameter to objectValues blank, so that it returns all objects in the folder. However, if you use this option, make sure your random images folder has only images in it!
Now that the script is in place, you can test it by pointing your browser to http://your.plone.site/randimg. It will work just like a normal image file on a web server. To add the image anywhere in your site, add the usual image HTML: <img src=“/randimg”>. Every time the page is reloaded, a randomly-selected image from your random_images folder will be shown.