1

I am trying to resize a given JCR image resource and store it as a new rendition. The use case is to generate thumbnails in "any" scale.

I wanted to use the com.day.cq.dam.core.process.CreateThumbnailProcess, but this it is not available in the project, i am working on.

damoeb
  • 154
  • 3
  • 9

2 Answers2

3

I found a quite low level approach, to resize an image identified by jcrPathToImage to int targetWidth and int targetHeight.

  1. Resize Image

    Resource resource = getResourceResolver().getResource(jcrPathToImage);
    Asset asset = resource.adaptTo(Asset.class);
    Layer layer = new Layer(asset.getOriginal().getStream())
    layer.resize(targetWidth, targetHeight);
    
  2. Create new rendition in JCR

    Extract mime type of the original image

    Image image = new Image(resource);
    String mimeType = image.getMimeType();
    

    Store the resized Image using its asset representation.

    ByteArrayOutputStream bout = null;
    ByteArrayInputStream bin = null;
    
    try {
        bout = new ByteArrayOutputStream(2048);
        layer.write(mimeType, 1, bout);
    
        bin = new ByteArrayInputStream(bout.toByteArray());
    
        asset.addRendition(resizedImgName, bin, mimeType);
    
    } finally {
        // close streams ...               
    }
    
damoeb
  • 154
  • 3
  • 9
1

you can configure the DAM Update Asset workflow to give the renditions you want to get created

http://localhost:4502/etc/workflow/models/dam/update_asset.html

in this workflow model select the thumbnail creation step and in the process tab of that step you can add your custom thumbnail values

[140:100],[48:48],[319:319],[90,90]