The overlay feature overlays an image or text string on top of a derivative (output) image. This can be useful for description, branding, attribution, copyright notices, and so on.
With both types of overlays, the position, inset, and output width/height threshold (below which the overlay won't be displayed) are configurable.
Not all processors support overlays; see the table of supported features.
An image overlay must be a 24-bit PNG image, either RGB or RGBA. It will be blended pixel-for-pixel into the output image.
Overlay images can be located on the filesystem, and also, as of version 3.3, on a web server. Regardless, they are cached in memory after being loaded the first time, so there is no performance penalty for using web-hosted overlay images.
String overlays are dynamically rendered onto an image using Java 2D. The font, font size, font weight, color, stroke color, background color and opacity, etc., are configurable.
Line breaks within the string (\n
) are respected, enabling multi-line strings. Each line is auto-aligned to the edge of the image according to the overlay position.
Strings are guaranteed to never overflow outside of the image. When a string doesn't fit entirely inside at its specified font size, the image server will try to use the largest font size that fits, down to the configurable minimum allowed size. If none fit, the string won't be drawn.
The overlay system offers two "strategies," or modes of operation: a basic strategy, where overlay properties are hard-coded in the configuration file and applied to all requests; and a script strategy, where the decision of whether to apply an overlay, and what kind, depends on the return value of a delegate method. The overlays.strategy
configuration key is used to set the strategy.
With BasicStrategy, the overlays.BasicStrategy.*
keys in the configuration file are used to set an overlay type (image or string), position, inset, and others. This strategy is easy to configure and is all that is needed when it is intended for the same overlay to be applied to all images.
Perhaps you want to apply an image overlay to some of your images, and to others, you want to apply a string overlay, or no overlay. Perhaps you want to control overlays based on the client's IP address or user agent. Or, perhaps you only want to apply overlays to JPEG output images, and not GIFs. Or, perhaps you don't want to apply an overlay to images that have been rotated. All of this is possible by writing just a few lines of code.
With the Cantaloupe::overlay()
delegate method, you can tell the image server whether to apply an overlay in response to a particular request, and what its properties should be. An example of how this can be accomplished appears below:
Position and inset are configurable. Supported positions are:
top left
|
top center
|
top right
|
left center
|
center
|
right center
|
bottom left
|
bottom center
|
bottom right
|
Zooming image viewers display a mosaic of cropped image tiles. When overlays are enabled, each tile will have one, which is bound to look strange.
One way of working around this might be to set the overlays.BasicStrategy.output_width_threshold
and overlays.BasicStrategy.output_height_threshold
configuration options to values that are slightly larger than the tile size used by the image viewer. This will disable overlays for images the size of image viewer tiles, and enable them for anything larger. Unfortunately, the tile size used by the viewer may differ depending on the source image, as the recommended tile sizes in information responses will vary on a per-image basis.