Class ImageMagickImageOperationService

java.lang.Object
com.broadleafcommerce.asset.service.image.ImageMagickImageOperationService
All Implemented Interfaces:
ImageOperationService

public class ImageMagickImageOperationService extends Object implements ImageOperationService
The default implementation of the ImageOperationService. Relies on ImageMagick for image processing and IM4Java as the Java API to ImageMagick.

Note: ImageMagick is a commandline utility and IM4Java is simply a wrapper that ultimately executes ImageMagick commands. As a result, checks should be in place to ensure this service is only registered as a bean if ImageMagick is available on the system. Otherwise, unexpected behavior and errors may occur.

Author:
Dima Myroniuk (dmyroniuk), Samarth Dhruva (samarthd)
  • Field Details

    • TEMP_FILE_PREFIX

      protected static final String TEMP_FILE_PREFIX
      See Also:
    • OPTIMIZED_IMG_NAME_PATTERN

      protected static final String OPTIMIZED_IMG_NAME_PATTERN
    • optimizeImgOperation

      protected final org.im4java.core.Operation optimizeImgOperation
    • storageProvider

      protected final StorageProvider storageProvider
    • imageProperties

      protected final ImageProperties imageProperties
    • namedOperations

      protected List<NamedOperation> namedOperations
  • Constructor Details

    • ImageMagickImageOperationService

      public ImageMagickImageOperationService(StorageProvider storageProvider, ImageProperties imageProperties, jakarta.activation.FileTypeMap mimeFileTypeMap)
  • Method Details

    • setNamedOperations

      @Autowired(required=false) public void setNamedOperations(List<NamedOperation> namedOperations)
      Description copied from interface: ImageOperationService
      Set the List of NamedOperation
      Specified by:
      setNamedOperations in interface ImageOperationService
      Parameters:
      namedOperations - the List of NamedOperation to set
    • getImgConvertCmd

      @Lookup public org.im4java.core.ConvertCmd getImgConvertCmd()
    • getImgBatchConverter

      @Lookup public org.im4java.utils.BatchConverter getImgBatchConverter()
    • applyEffectsAndStoreModifiedFile

      public File applyEffectsAndStoreModifiedFile(File file, String targetUrl, org.springframework.util.MultiValueMap<String,String> imageEffectsTags)
      Description copied from interface: ImageOperationService
      Apply the effects to the file and store the new file to the targetUrl
      Specified by:
      applyEffectsAndStoreModifiedFile in interface ImageOperationService
      Parameters:
      file - the image file to apply effects
      targetUrl - the target url to save file
      imageEffectsTags - the image effects
      Returns:
      the new saved file
    • applyEffectsAndStoreModifiedResource

      public ResourceWithMetadata applyEffectsAndStoreModifiedResource(ResourceWithMetadata imageResource, String targetUrl, org.springframework.util.MultiValueMap<String,String> imageEffectsTags)
      Description copied from interface: ImageOperationService
      Apply the effects to the imageResource and store the new resource to the targetUrl
      Specified by:
      applyEffectsAndStoreModifiedResource in interface ImageOperationService
      Parameters:
      imageResource - the image resource to apply effects
      targetUrl - the target url to save file
      imageEffectsTags - the image effects
      Returns:
      the new saved resource
    • applyEffects

      public InputStream applyEffects(File file, org.springframework.util.MultiValueMap<String,String> imageEffectsTags)
      Description copied from interface: ImageOperationService
      Apply the effects to the file and save it to the temp file. Note: The temp file will be deleted on close. StandardOpenOption.DELETE_ON_CLOSE
      Specified by:
      applyEffects in interface ImageOperationService
      Parameters:
      file - the image file to apply effects
      imageEffectsTags - the image effects
      Returns:
      the InputStream to the new temp file.
    • applyEffects

      public InputStream applyEffects(ResourceWithMetadata imageResource, org.springframework.util.MultiValueMap<String,String> imageEffectsTags)
      Description copied from interface: ImageOperationService
      Apply the effects to the imageResource and save it to the temp file. Note: The temp file will be deleted on close. StandardOpenOption.DELETE_ON_CLOSE
      Specified by:
      applyEffects in interface ImageOperationService
      Parameters:
      imageResource - the image resource to apply effects
      imageEffectsTags - the image effects
      Returns:
      the InputStream to the new temp file.
    • isValidImageEffectTags

      public boolean isValidImageEffectTags(org.springframework.util.MultiValueMap<String,String> imageEffectsTags)
      Description copied from interface: ImageOperationService
      Check that the imageEffectsTags is valid
      Specified by:
      isValidImageEffectTags in interface ImageOperationService
      Parameters:
      imageEffectsTags - the image effects
      Returns:
      false if imageEffectsTags contains the effect names which is not configured by broadleaf.image.availableEffects property or NamedOperation and true otherwise
    • optimizeImage

      public InputStream optimizeImage(org.springframework.web.multipart.MultipartFile toOptimize)
      Description copied from interface: ImageOperationService
      Optimizes a GIF, PNG, or JPEG in an attempt to reduce its size.

      If toOptimize is a GIF, PNG, or JPEG, it will be copied to a temporary file, optimized, and its stream will be returned. When this stream is closed, the temporary file will be deleted, as documented by StandardOpenOption.DELETE_ON_CLOSE.

      The given file itself is not modified, as the optimization is applied to a copy.

      If toOptimize was not a GIF, PNG, or JPEG, the returned value will just be its unmodified stream. The temp file will be deleted on close. StandardOpenOption.DELETE_ON_CLOSE

      Specified by:
      optimizeImage in interface ImageOperationService
      Parameters:
      toOptimize - the file to be optimized
      Returns:
      the InputStream of the optimized file, or of the unmodified file if not a GIF, PNG, or JPEG
    • getContentType

      protected String getContentType(org.springframework.web.multipart.MultipartFile file)
    • optimizeImageBatch

      @NonNull public Map<String,File> optimizeImageBatch(Map<String,File> imagesToOptimize)
      Description copied from interface: ImageOperationService
      This method will batch-optimize any GIF, PNG, and JPEG image files in the input map in an attempt to reduce their sizes.

      This operation will write over the given files themselves.

      Specified by:
      optimizeImageBatch in interface ImageOperationService
      Parameters:
      imagesToOptimize - a map of normalized URLs to files that should be optimized. The content type of the files will be determined purely by examining the URLs (key), and this will be used to determine whether the file is eligible for optimization.
      Returns:
      a map of the given URLs to the corresponding optimized files. Only files that were successfully optimized will be present in this result. While the resulting map will be a new instance, the instances of any keys/values will be the same as those that were supplied.
    • getCustomOperationNames

      protected List<String> getCustomOperationNames()
    • createTempFile

      protected File createTempFile(String fileName) throws IOException
      Throws:
      IOException
    • buildOperation

      protected org.im4java.core.Operation buildOperation(File file, org.springframework.util.MultiValueMap<String,String> imageEffectsTags)
    • buildOperation

      protected org.im4java.core.Operation buildOperation(String fileExtension, org.springframework.util.MultiValueMap<String,String> imageEffectsTags)
    • addNamedOperations

      protected void addNamedOperations(org.im4java.core.Operation operation, List<String> namedOperationNames)
    • addNamedOperation

      protected void addNamedOperation(org.im4java.core.Operation operation, NamedOperation namedOperation)
    • addOperation

      protected void addOperation(org.im4java.core.Operation operation, String name, String value)
    • isSupportedContentType

      protected boolean isSupportedContentType(String contentType)
    • initOptimizeImgOperation

      protected org.im4java.core.Operation initOptimizeImgOperation()
    • getDynamicImgOptimizationOperation

      protected org.im4java.core.DynamicOperation getDynamicImgOptimizationOperation()
    • getImgOptimizationOperation

      protected org.im4java.core.IMOperation getImgOptimizationOperation(List<String> imgInfoOut)
    • getPngGifImgOperation

      protected org.im4java.core.IMOperation getPngGifImgOperation(List<String> imgInfoOut)
    • getJpegImOperation

      protected org.im4java.core.IMOperation getJpegImOperation(List<String> imgInfoOut)
    • getImgInfo

      protected List<String> getImgInfo(String image)
    • getMimeFileTypeMap

      @NonNull protected jakarta.activation.FileTypeMap getMimeFileTypeMap()