Broadleaf Microservices
  • v1.0.0-latest-prod

Image Operation Service

Overview

In the Asset Microservice, there is an ImageOperationService interface, which is used to perform various operations on uploaded assets such as optimizing and resizing images. The default implementation is the ImageMagickImageOperationService. Internally, it requires that the ImageMagick commandline utility is installed on the host machine. Installation instructions can be found at https://imagemagick.org/index.php.

If the ImageMagick utility is not available on the host machine, an alternate ImageOperationService implementation will be registered: DoNothingImageOperationService. This implementation effectively does nothing whenever any of its methods are called. It is intended to ensure any logic that has a hard requirement on ImageOperationService will not fail if ImageMagick is not available.

Named Operations

If the ImageMagickImageOperationService is configured, you can specify custom named operations to support dynamic image transformation on high resolution assets served by the Asset Service.

Below are some typical configurations that you could include within your project.

Browse Example

/**
 * Operation to convert an image to a category or search/browse-appropriate size.
 *
 */
@Component
public class Browse implements NamedOperation {

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE - 1000;
    }

    @Override
    public String getName() {
        return "browse";
    }

    @Override
    public Map<String, String> getOperations() {
        Map<String, String> ops = new HashMap<>(2);
        ops.put("resize", "432x432>");
        // basing this value on one provided here
        // https://www.imagemagick.org/Usage/resize/#resize_unsharp
        ops.put("unsharp", "0x0.75+0.75+0.008");

        return ops;
    }
}

ProductMD Example

/**
 * Operation to resize a product image to its "medium" version. This is likely suitable for
 * desktops.
 */
@Component
public class ProductMD implements NamedOperation {

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE - 1000;
    }

    @Override
    public String getName() {
        return "product-md";
    }

    @Override
    public Map<String, String> getOperations() {
        Map<String, String> ops = new HashMap<>(2);
        ops.put("resize", "768x768>");
        // basing this value on one provided here
        // https://www.imagemagick.org/Usage/resize/#resize_unsharp
        ops.put("unsharp", "0x0.75+0.75+0.008");

        return ops;
    }
}

Zoom Example

/**
 * Operation to convert an image to a zoomed-in version. This has lower precedence than other
 * operations like {@link ProductLG}, {@link ProductMD}, etc. so that it can be combined with them.
 */
@Component
public class Zoom implements NamedOperation {

    @Override
    public String getName() {
        return "zoom";
    }

    @Override
    public Map<String, String> getOperations() {
        Map<String, String> ops = new HashMap<>(2);
        // basing this value on one provided here
        // https://www.imagemagick.org/Usage/resize/#resize_unsharp
        ops.put("scale", "200%");
        ops.put("unsharp", "0x0.75+0.75+0.008");

        return ops;
    }
}