Tips & Tricks - Scale images

By implementing Groovy script in a process, images in a defined folder can be scaled automatically - e.g. to create thumbnails. Open the Processes module. Create a new process and remove any existing elements, where appropriate.

Create a global timer and then a timer event handler. Select the global timer we just created on the "Timer event handler" tab in the properties dialog.

Connect a new Groovy action to the timer event handler from the previous step. Open the properties dialog of the action and open the Intrexx editor there. Insert the following script there:

import static groovy.io.FileType.FILES
import de.uplanet.lucy.server.scripting.groovy.ImageHelper

def dirImagePath = new File("IMAGE_DIRECTORY_PATH")
def strFileName
def strThumbnailName
def fileThumbnail

dirImagePath.eachFileRecurse(FILES){image ->
	if (!image.name.endsWith("_thumbnail.png"))
	{
		strFileName = image.name

		if(ImageHelper.isSupportedImageFormat(image.path))
		{
			strThumbnailName = "${strFileName[0..strFileName.lastIndexOf(".")-1]}_thumbnail.png"
			fileThumbnail    = new File(image.path, strThumbnailName)

			if (!fileThumbnail.file)
				ImageHelper.scaleImage(inputFile:image.path, outputFile:"${image.parent}/${strThumbnailName}", maxWidth:100, maxHeight:100)
			else
				g_log.warn("Thumbnail for image ${strFileName} already exists.")
		}
		else
		{
			g_log.error("Error creating thumbnail for ${strFileName}. Format is not supported.")
		}
	}
}

The import statement at the beginning of the script integrates the "ImageHelper" class needed for the scaling. Afterwards, the variables are defined. With

def dirImagePath = new File("IMAGE_DIRECTORY_PATH")

you specify which directory will be searched through the images to be scaled. In place of "IMAGE_DIRECTORY_PATH" specify the required target path (e.g. c:/scale-images). With the command

dirImagePath.eachFileRecurse(FILES)

all files within the specified starting directory will be processed recursively. The associated if statement

if (!image.name.endsWith("_thumbnail.png"))

ensures that the only files that will be processed are those that are not thumbnails that have already been generated by the process. In the next step, a check is made to ensure that the image in question is of a supported file type or not. If the format is valid, then a scaled thumbnail will be created. If the format is not valid, i.e. it is not supported by the "ImageHelper" class, then a corresponding error message will be written to the log file of the process. With the line

def strThumbnailName = "${strFileName[0..strFileName.lastIndexOf(".")-1]}_thumbnail.png"

a new filename for the thumbnail image will be created. Here the suffix "_thumbnail" will be added to the original filename to make them easier to identification. The thumbnail generated in this example will be stored in the png format. The format can be changed if required. The supported formats are PNG, JPEG and BMP. The actual scaling of the original image is carried out with the "scaleImage" method of the ImageHelper. The scaling only takes place when a thumbnail image of the original image does not yet exist. This is checked with the preceding if statement.

if (!fileThumbnail.file)
ImageHelper.scaleImage(inputFile:image.path, outputFile:"${image.parent}/${strThumbnailName}", maxWidth:100, maxHeight:100)

The call of this method takes place using so-called named parameters. This means that the parameter will be transferred in the form "parameter:value" and the parameter will have a fixed name. The two parameters "inputFile" and "outputFile" are mandatory parameters, all of the others are optional. The following parameters are available:

Name

Description

inputFile

Mandatory parameter - the input file as a file object (java.io.File) or as a string (path relative to the portal directory).

outputFile

Mandatory parameter - the output file as a file object (java.io.File) or as a string (path relative to the portal directory).

format

Output format of the scaled image (PNG, JPEG or BMP). The default value here is PNG.

width

The width of the scaled image.

height

The height of the scaled image.

maxWidth

The maximum width of the scaled image. The image will be scaled to this value while maintaining the aspect ratio.

maxHeight

The maximum height of the scaled image. The image will be scaled to this value while maintaining the aspect ratio.

scaleX

The horizontal scaling factor the image is scaled by.

scaleY

The vertical scaling factor the image is scaled by.

shrinkOnly

If this parameter is set to true, the image will only be scaled if the resulting image is smaller than the original image. shrinkOnly can only be used in conjunction with maxWidth or maxHeight.

The following parameters are mutually exclusive and cannot be used together: width, maxWidth and scaleX or height, maxHeight and scaleY Close the script editor and save the process. Start the timer via the "Start global timer job" context menu. If the process is performed successfully, you will find a scaled preview image for each supported image in the specified directory.