Lesson 3: Develop custom worker calling Photoshop APIs
Now you got the development environment set up locally and can run a basic worker, let's enhance it to do something more complex.
Adobe Photoshop APIs enable you to build plugins and integrations that harness the power of the world’s best image editing and graphic design software to transform creative workflows for users everywhere. In this codelab, you leverage the Photoshop APIs to generate custom renditions in AEM Assets.
You will need the App Builder Files SDK to store images, the Photoshop API SDK to call Photoshop APIs, and UUID to generate unique folder names for renditions of different images. Add them as dependencies in your package.json
file.
Copied to your clipboard"dependencies": {"@adobe/aio-lib-photoshop-api": "0.0.4-beta.4","@adobe/aio-sdk": "^2.3.0","@adobe/asset-compute-sdk": "^2.2.1","uuid": "^8.0.0"}
Note: Photoshop API SDK is currently in beta release and its version is constantly changing. Please visit its GitHub repo to find out the latest version.
Make sure you run npm install
after updating the package.json
file, so that all the npm modules are installed.
Then update your action code in actions/<worker-name>/index.js
to use the Photoshop API SDK. In this example, it calls the cutout service to remove the background of an image.
Copied to your clipboardconst { worker, SourceCorruptError } = require('@adobe/asset-compute-sdk');const fs = require('fs').promises;const { Files } = require('@adobe/aio-sdk');const Photoshop = require('@adobe/aio-lib-photoshop-api');const { v4: uuid4 } = require('uuid');exports.main = worker(async (source, rendition, params) => {const files = await Files.init();// obtain access token and org IDconst accessToken = params.auth && params.auth.accessToken;const orgId = params.auth && params.auth.orgId;// init Photoshop SDK clientconst psClient = await Photoshop.init(orgId, params.apiKey, accessToken, files);// create a new directory in aio-lib-files with unique nameconst imageId = uuid4();const aioSourcePath = `${imageId}/source.png`;const aioRenditionPath = `${imageId}/rendition.png`;await files.copy(source.path, aioSourcePath, { localSrc: true });// call Photoshop API to do cutout processing, and poll status until it's successfulconst result = await psClient.createCutout(aioSourcePath, aioRenditionPath);await result.pollUntilDone(1000);// download the rendition to local AEM Assets destinationawait files.copy(aioRenditionPath, rendition.path, { localDest: true });// clean up files processing folder in aio-lib-filesawait files.delete(`${imageId}/`);});
Because the SDK requires apiKey
to be passed in the input params, update your ext.config.yaml
in the src/dx-asset-compute-worker-1/
folder to include it.
Copied to your clipboardoperations:workerProcess:- type: actionimpl: dx-asset-compute-worker-1/workerhooks:post-app-run: adobe-asset-compute devtooltest: adobe-asset-compute test-workeractions: actionsruntimeManifest:packages:dx-asset-compute-worker-1:license: Apache-2.0actions:worker:function: actions/worker/index.jsweb: 'yes'runtime: 'nodejs:14'limits:concurrency: 10inputs:apiKey: $SERVICE_API_KEYannotations:require-adobe-auth: true
Your worker should now be set.
Run the aio app deploy
command to deploy your action to test in the development tool UI.
Then execute the command aio app run
to test it. In the development tool UI, select an existing or upload a new test image, define the rendition request and click the Run button. You will see the rendition result with a removed background.
There are various options of other photo magics that you can use to enhance your custom worker, such as Auto Tone and Photoshop actions. Be creative!