With Intrexx 5 or with Online Update 19 for Version 4.5 customers get an opportunity to scale new or existing images automatically within Groovy scripts.

On basis of a simple application, the following example shows, how a thumbnail image with smaller dimensions will be generated from an uploaded image. It will no longer be necessary for users to create and upload the same image in different sizes manually. For example, this mechanism can be used for shop applications, where a preview image for items should be available.


Open the Intrexx Portal Manager and choose Application Designer. Create a new application from the template Empty Application. Rename this application, e.g. Article images and rename the already existing data group to images.

Create new edit elements for the title of the image and for the image itself on the edit page of this data group. Additionally create buttons to save and delete data records.

Once those elements are created, the edit page should look just as the following screenshot.



Optionally you can create a view table on the main page of the application to list all existing records.

Save and publish the application and switch to the module Processes. Create a new process and rename it, e.g. Article images: Create thumbnail.

For Version 4.5 create a data group event, for Version 5 a data group event handler respectively, which listens to insert events of the data group images. In version 4.5 create a condition with option always. As action, that will be executed, choose the Groovy script action.

After defining these elements, the new process should look like this (version 5):



Now open the Groovy editor of the groovy script action and insert the following script:

import de.uplanet.lucy.server.scripting.groovy.ImageHelper

// datafield image selection
def fileImage = g_record["GUID_DATAFIELD_IMAGE"]
def strFileName = fileImage.fileName
def strThumbnailName = "${strFileName[0..strFileName.lastIndexOf(".")-1]}_thumbnail.png"


if(ImageHelper.isSupportedImageFormat(fileImage.path))
ImageHelper.scaleImage(inputFile:fileImage.path, outputFile:"internal/files/${g_record.applicationGuid}/${strThumbnailName}", maxWidth:100, maxHeight:100)
else
g_log.error("Error creating thumbnail for ${strFileName}. Format is not supported.")


The first line contains the syntax to import the required class ImageHelper.

Afterwards required variables are being defined.

With
def fileImage = g_record["GUID_DATAFIELD_IMAGE"]
the image, which was uploaded by the file selection control, is referenced. Between the brackets the GUID of the image data field is required. In order to retrieve this GUID, simply right-click the mouse to open the context menu or click the icon . In this menu you get a selection of the available data fields of the current data group. Choose the file data field. The correct g_record expression will be inserted automatically.

The two lines of code
def strFileName = fileImage.fileName
def strThumbnailName = "${strFileName[0..strFileName.lastIndexOf(".")-1]}_thumbnail.png"


will create the new name of the thumbnail image. This new name will be a combination of the original file name followed by suffix _thumbnail. The image will be saved as a PNG image. If needed, the output format can be changed. Supported formats are PNG, JPEG and BMP.

The following step checks, whether or not the format of the provided image is supported by ImageHelper. If the format is supported, a scaled thumbnail will be generated from the provided image. If the format is not supported, a corresponding error message will be written to the portal’s log file
(Version 4.5, <xtreme>/org/<portalname>/log/portal.log) respectively to the log file of the current workflow
(Version 5, <intrexx>/org/<portalname>/log/workflow.log).

The actual scaling of the image takes place in the method scaleImage() of the class ImageHelper.

ImageHelper.scaleImage(inputFile:fileImage.path, outputFile:"internal/files/${g_record.applicationGuid}/${strThumbnailName}", maxWidth:100, maxHeight:100)

The method is called with so called named parameter. This parameters are provided to the method in the form of parameter:value and the parameters themselves have a fixed name. The two parameters inputFile and outputFile are required, further parameters are optional.

Name Description
inputFile The input file as a File-object (java.io.File) or as a character sequence (path relative to the portal directory). Required
outputFile The output file as a File-object (java.io.File) or as a character sequence
format The format or the scaled image; i.e. 'PNG' (default), 'JPEG' or 'BMP'
width The new width of the image.
height The new height of the image.
maxWidth The maximum width of the new image. The image will be scaled up to this value but not greater, while the aspect ratio will be maintained.
maxHeight The maximum height of the new image. The image will be scaled up to this value but not greater, while the aspect ratio will be maintained.
scaleX The horizontal scaling factor to be applied.
scaleY The vertical scaling factor to be applied.
shrinkOnly Scale only if the resulting image is smaller than the original. This option can only be used together with maxWidth or maxHeight.


Note:
width, maxWidth and scaleX are mutually exclusive

height, maxHeight and scaleY are mutually exclusive.

Close the script editor and publish the process on the server. Open the application within an internet browser of your choice and upload a new article image. By saving the data record a scaled thumbnail image will be generated and saved in the files directory of your portal.

Name of directory in version 4.5: <xtreme>/org/<portalname>/internal/files/<application-GUID>/

Name of directory in version 5: <intrexx>/org/<portalname>/internal/files/<application-GUID>/






United Planet
Intrexx Application Store Press Downloads
English