Tips & Tricks - Scaling images

With Groovy script, images in a given folder can be automatically scaled by process - e.g. to create thumbnails. To do this, switch to the "Processes" module. Create a new process and remove any existing elements.

Create a global timer and then a timer event handler. In the properties dialog of the timer event handler, select the previously created global timer on the "Timer event handler" tab.

Connect a new Groovy action to the previously created timer event handler. 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 required for 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. Enter the desired target path here instead of "IMAGE_DIRECTORY_PATH" (e.g. c:/scale-images). With the command

            dirImagePath.eachFileRecurse(FILES)
        

all files within the specified starting directory will be processed recursively. With the subsequent if query

            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, a scaled thumbnail is created. If there is a format that is not supported by the "ImageHelper" class, a corresponding error message is displayed in 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. The suffix "_thumbnail" is added to the original file name for better 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 using the preceding if query.

            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 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 has the value true, the image is only scaled if the resulting image is smaller than the original image. shrinkOnly can only be used in conjunction with maxWidth or maxHeight.

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