Vivek Dhiman
Vivek Dhiman

Reputation: 1997

Image Compression Using MagickQuick in AEM

I am using ImageQuick Tool to compress the DAM images. I am using below snippet for conversion.

try {
            System.out.println(" Image is Processing :: " + assetDetails.getName() + "Original Size :: " + assetDetails.getSize());
            ProcessBuilder pb = new ProcessBuilder("C:\\Program Files\\ImageMagick-6.9.9-Q16\\convert.exe", "-quality", "85%", assetDetails.getAssetNode().getPath(), assetDetails.getAssetNode().getPath());
            pb.redirectErrorStream(true);

            Process p = pb.start();
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
            System.out.println(p.waitFor());
            session.save();

        } catch (final IOException | RepositoryException | InterruptedException ex) {
            ex.printStackTrace();
        }

Getting an error as below :

 Image is Processing :: A.jpgOriginal Size :: 323.8 KB
convert.exe: unable to open image `/content/dam/A.jpg': No such file or directory @ error/blob.c/OpenBlob/2761.
convert.exe: no images defined `/content/dam/A.jpg' @ error/convert.c/ConvertImageCommand/3258.

Here code is looking for the file from /content/dam from local. How can I do this operation on existing images.

In case, I add the server name before path, I am getting below error :

convert.exe: no decode delegate for this image format `' @ error/constitute.c/ReadImage/504.
convert.exe: no data returned `http://localhost:4502/content/dam/A.jpg' @ error/url.c/ReadURLImage/247.
convert.exe: no images defined `http://localhost:4502/content/dam/A.jpg' @ error/convert.c/ConvertImageCommand/3258.

PFA* enter image description here

Upvotes: 1

Views: 874

Answers (1)

iusting
iusting

Reputation: 8383

The path /content/dam/A.jpg is relative to the JCR repository, not to the actual file system, so an image not found exception is launched. Also the hardcoded value of your ImageMagick installation path, "C:\\Program Files\\ImageMagick-6.9.9-Q16\\convert.exe doesn't seem too flexible IMHO, as it will be probably different in other environments (test, pre-prod, prod, ...).

Actually, if you want to use IM to improve image compression, I would recommend you to add custom Process Steps to the DAM Update Asset workflow.

For example, for creating a fixed 1140x1140 PNG compressed rendition I would use:

<node1
        jcr:primaryType="cq:WorkflowNode"
        description="This process step uses IM PNG compression."
        title="IM PNG Mime Type"
        type="PROCESS">
        <metaData
            jcr:primaryType="nt:unstructured"
            COMMANDS="convert ${directory}/${filename} -resize 1140x1140> -strip -depth 24 -define png:compression-filter=2 -define png:compression-level=9 -define png:compression-strategy=1 cq5dam.web.1140.1140.${extension}"
            MIME_TYPES="image/png"
            PROCESS="com.day.cq.dam.core.process.CommandLineProcess"
            PROCESS_AUTO_ADVANCE="true"/>
</node1>

You can read more about the command in the IM doc and if you are interesting on integrating this in the OOTB AEM workflow, take the node model content from above as an example or take a look at this pretty well described AEM Assets - best practices article.

Upvotes: 1

Related Questions