But AssetManager enables to create new Asset. In my case, I already have the asset and I'm just creating a few new renditions for the asset. Please take a look at the code below.
private static final String CONTENT_PATH = "/" + JcrConstants.JCR_CONTENT;
private static final String DAM_THUMBNAIL_FORMAT = "cq5dam.thumbnail.%s.%s.png";
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
throws WorkflowException {
org.apache.sling.api.resource.Resource imgResource = getResourceFromPayload(workItem, workflowSession);
com.day.cq.dam.api.Asset asset = imgResource.adaptTo(Asset.class);
String mimeType = asset.getMimeType();
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
com.day.image.Layer layer = new Layer(asset.getOriginal().getStream());
int width = layer.getWidth();
int height = layer.getHeight();
int resizedWidth = width / 2;
int resizedHeight = height / 2;
layer.resize(resizedWidth, resizedHeight);
layer.write(mimeType, 1, outputStream);
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray())) {
String renditionName = String.format(DAM_THUMBNAIL_FORMAT, 1, 1);
asset.addRendition(renditionName, inputStream, mimeType);
}
} catch (IOException e) {
LOG.error("IOException while creating custom rendition for an image", e);
}
}
Do you have any bits of advice for me?
Thanks