Introducing the /script/run Robot for code evaluation
We're happy to have the chance to reveal the shiny new /script/run Robot to you. Thanks to the introduction of code evaluation into the Transloadit toolkit that it brings, your Assemblies now have the expansive capabilities of modern JavaScript!
By including custom code within your parameters, using ${...}
, we'll automatically execute the
code and return the result value to be used within your Assembly.
Simple variable lookups such as ${file.meta.width}
are classified to not require the
Robot's JavaScript interpreter, and remain free. More complex expansions will require us
to invoke the /script/run Robot under the hood, with
there being a single charge for each Dynamic Evaluation.
Creating a Template
As a small demonstration, let's create a Template that resizes an image to fit the golden ratio.
{
"steps": {
":original": {
"robot": "/upload/handle"
},
"golden_ratio_resized": {
"use": ":original",
"robot": "/image/resize",
"width": "${file.meta.height * (1 + Math.sqrt(5))/2}",
"height": "${file.meta.height}",
"resize_strategy": "fillcrop",
"result": true,
"imagemagick_stack": "v3.0.1"
},
"exported": {
"robot": "/s3/store",
"use": "golden_ratio_resized",
"result": true,
"credentials": "s3defaultBucket",
"path": "golden_ratio/${file.name}"
}
}
}
Looking at the JSON above, you'll notice that the golden_ratio_resized
Step contains
the /script/run functionality contained within our
/image/resize Robot. This keeps our JSON as efficient
as possible.
To resize our image to the golden ratio, we leave the height
the same as the original height of
the file, then change the width
according to the golden ratio based on that original height. We
also use the fillcrop
resize strategy so the image can expand to fill the frame.
Testing
To ensure that everything works as expected, let's upload a picture of a friendly-looking cat and check out the new aspect ratio.
Before | After | |
---|---|---|
Just like that, we're automatically altering our images with no need to run prerequisite calculations on our backend.
Finishing up
And that brings us to the end of this demonstration. We look forward to seeing the ingenious ways that you'll find to utilize the /script/run Robot in your projects. We're also interested in your feedback on this latest addition to our Robot family, and do let us know if there are any other features you'd like to see included in the future!