OpenWGA 7.6 - TMLScript reference


Object:

ImageScaler

Description The ImageScaler is a tool object to scale and convert image files from TMLScript. It is using the Java Advanced Imaging API plus some custom filters, offering an easy to use interface for these tasks.

An ImageScaler is created via method WGA.createImageScaler() taking an image file as parameter. The data of the image is loaded to the scaler ready to be scaled by the available methods. The modified image then again can be put out as regular file via scaler.writeImage() or attached to a document using this.attachScaledImage().

The capabilities of the ImageScaler object, especially which formats it can read and write, is dependent on the capabilities of the Java Advanced Imaging API. However on most platforms the valid input formats are JPEG, PNG and BMP (Windows Bitmap). Usable output formats are JPEG (the default) and PNG. See further information about this API on the  homepage of the Java Advanced Imaging API  or the Java Advanced Imaging API Programming Guide.
Retrieval WGA.createImageScaler()
Allowed in script types
  • WebTML pages and normal WebTML actions
  • Master actions
  • TMLScript tasks in jobs
  • Content type events
    portletevent
Properties and methods
Name Purpose
aspectRatio Returns the aspect ratio of the image
determineRenderedSize() Calculates the expected file size of the image in bytes
format Directly sets the target format for storage
formatSuffix Returns the file suffix of the chosen output format
growToSize(width, height) Enlargens the image to minimal sizes for both dimensions
height Returns the current height of the image in pixels
imageEncodeParam Sets encoding parameters for that target format of the file
interpolation Controls the interpolation strategy used for scaling
isTransparencySupportedForOutput() Returns if the chosen output format supports transparency
quality Controls the "quality" of the output format
scaleByFactor(widthFactor, heightFactor) Scales the image based on a factor
scaleToSize(width, height [, keepRatio]) Scales the image to an absolute target size
shrinkToSize(width, height) Shrinks the image to a maximum size for both dimensions
sourceFileName Returns the name of the source file
useJPEGForOutput() Configured the scaler to use JPEG as output format
usePNGForOutput() Configures the scaler to use PNG as output format
width Returns the current width of the image in pixels
writeImage(file)
writeImage(outputStream)
Writes the image data to some output
Examples The following example processes an image file that is uploaded from a WebTML form. The image is scaled so that it does not exceed a maximum size of 800x600 pixels, while keeping the ratio.The actual result size therefor will be lower than the given values on one dimension.

First ther form to upload the file:

<tml:form id="uploadImage">
Image: <tml:input name="thefile" type="file"/><br/>
<tml:button clickaction="scale">Scale image</tml:button>
</tml:form>



Then the code of the action: The method tmlform.getFile() is used to get a regular file object for the uploaded file, which is neccessary to pass it on to the scaler. In this example the method this.attachScaledImage() is used to directly attach the modified data in the Scaler to the current context document. Note the omitted suffix on the output file name.

<tml:action id="scale">
  var scaler = WGA.createImageScaler(tmlform.getfile(tmlform.thefile));
  scaler.shrinkToSize(800, 600);
  scaler.usePNGForOutput();
  attachScaledImage(content(), scaler, "scaled");
  content().save();
</tml:action>