Alfresco leverages the power of ImageMagick for image transformations. Refer to Chapter 2, Installation and Configuration for installing ImageMagick. Alfresco JavaScript provides some APIs to perform image transformations. The prerequisite for this is to have ImageMagick installed. The Script node has the APIs mentioned in the following section for image transformation.
Alfresco provides the following APIs for image transformations. transformImage
is overloaded with different parameters. You can use any of these based on your requirement:
transformImage(string mimetype):
This will transform an image to a new type specified in mimetype
and will return the transformed image node if successful or null if failed.transformImage(string mimetype, string options):
This will transform an image to a new type specified in mimetype
by applying the supplied ImageMagick options and will return the transformed image node if successful or null if failed.transformImage(string mimetype, ScriptNode destination):
This will transform an image to a new type specified in mimetype
and will return the transformed image node if successful or null if failed. A new image document will be created in specified destination folder.transformImage(string mimetype, string options, ScriptNode destination):
This will transform an image to a new type specified in mimetype
by applying the supplied ImageMagick options and will return the transformed image node if successful or null if failed. A new image document will be created in the specified destination folder.Following are the steps to configure image transformation as an action in Alfresco.
web-client-config-custom.xml
file as:<config> <actions> <!-- Transform Image Action --> <action id="tranform_image"> <permissions> <permission allow="true">Write</permission> </permissions> <evaluator>org.alfresco.web.action.evaluator. WCMWorkflowEvaluator</evaluator> <label-id>title_action_transform_image</label-id> <image>/images/icons/action.gif</image> <action>browseSandbox</action> <script>/Company Home/ Data Dictionary/Scripts/transform_image.js</script> <params> <param name="id">#{actionContext.id}</param> </params> <target>new</target> </action> </actions> </config>
avm_file_browse
in the web-client-config-custom.xml
file, as follows:<config> <actions> <action-group id="avm_file_browse" replace="true"> <show-link>false</show-link> <action idref="edit_file" /> <action idref="update_file" /> <action idref="preview_file" /> <action idref="cut_avm_node" /> <action idref="copy_avm_node" /> <action idref="file_details" /> <action idref="unlock_file" /> <action idref="delete_file_browse" /> <action idref="tranform_image" /> </action-group> </actions> </config>
transform_image.js
will look as follows:logger.log("called :: " + args["id"]); var path = args["id"]; var node = avm.lookupNode(path) logger.log("node : "+ node); var thumbImage = node.transformImage("image/png", "-resize 120",node.parent);
image/png
and also can specify the required options as the second argument in transformImage
API.In this section, we will see how we can use this action, which we configured in the previous section.
Expand-snap.JPG
image file available. We will apply the Transform Image action on this and we will have the same image transformed into PNG as Expand-snap.png
file with resizing.