Class ImageMagickImageOperationService
- All Implemented Interfaces:
ImageOperationService
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 Summary
FieldsModifier and TypeFieldDescriptionprotected final ImagePropertiesprotected List<NamedOperation>protected static final Stringprotected final org.im4java.core.Operationprotected final StorageProviderprotected static final String -
Constructor Summary
ConstructorsConstructorDescriptionImageMagickImageOperationService(StorageProvider storageProvider, ImageProperties imageProperties, jakarta.activation.FileTypeMap mimeFileTypeMap) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddNamedOperation(org.im4java.core.Operation operation, NamedOperation namedOperation) protected voidaddNamedOperations(org.im4java.core.Operation operation, List<String> namedOperationNames) protected voidaddOperation(org.im4java.core.Operation operation, String name, String value) applyEffects(ResourceWithMetadata imageResource, org.springframework.util.MultiValueMap<String, String> imageEffectsTags) Apply the effects to theimageResourceand save it to the temp file.applyEffects(File file, org.springframework.util.MultiValueMap<String, String> imageEffectsTags) Apply the effects to thefileand save it to the temp file.applyEffectsAndStoreModifiedFile(File file, String targetUrl, org.springframework.util.MultiValueMap<String, String> imageEffectsTags) Apply the effects to thefileand store the new file to thetargetUrlapplyEffectsAndStoreModifiedResource(ResourceWithMetadata imageResource, String targetUrl, org.springframework.util.MultiValueMap<String, String> imageEffectsTags) Apply the effects to theimageResourceand store the new resource to thetargetUrlprotected org.im4java.core.OperationbuildOperation(File file, org.springframework.util.MultiValueMap<String, String> imageEffectsTags) protected org.im4java.core.OperationbuildOperation(String fileExtension, org.springframework.util.MultiValueMap<String, String> imageEffectsTags) protected FilecreateTempFile(String fileName) protected StringgetContentType(org.springframework.web.multipart.MultipartFile file) protected org.im4java.core.DynamicOperationorg.im4java.utils.BatchConverterorg.im4java.core.ConvertCmdgetImgInfo(String image) protected org.im4java.core.IMOperationgetImgOptimizationOperation(List<String> imgInfoOut) protected org.im4java.core.IMOperationgetJpegImOperation(List<String> imgInfoOut) protected jakarta.activation.FileTypeMapprotected org.im4java.core.IMOperationgetPngGifImgOperation(List<String> imgInfoOut) protected org.im4java.core.Operationprotected booleanisSupportedContentType(String contentType) booleanisValidImageEffectTags(org.springframework.util.MultiValueMap<String, String> imageEffectsTags) Check that theimageEffectsTagsis validoptimizeImage(org.springframework.web.multipart.MultipartFile toOptimize) Optimizes a GIF, PNG, or JPEG in an attempt to reduce its size.optimizeImageBatch(Map<String, File> imagesToOptimize) This method will batch-optimize any GIF, PNG, and JPEG image files in the input map in an attempt to reduce their sizes.voidsetNamedOperations(List<NamedOperation> namedOperations) Set theListofNamedOperation
-
Field Details
-
TEMP_FILE_PREFIX
- See Also:
-
OPTIMIZED_IMG_NAME_PATTERN
-
optimizeImgOperation
protected final org.im4java.core.Operation optimizeImgOperation -
storageProvider
-
imageProperties
-
namedOperations
-
-
Constructor Details
-
ImageMagickImageOperationService
public ImageMagickImageOperationService(StorageProvider storageProvider, ImageProperties imageProperties, jakarta.activation.FileTypeMap mimeFileTypeMap)
-
-
Method Details
-
setNamedOperations
Description copied from interface:ImageOperationServiceSet theListofNamedOperation- Specified by:
setNamedOperationsin interfaceImageOperationService- Parameters:
namedOperations- theListofNamedOperationto 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:ImageOperationServiceApply the effects to thefileand store the new file to thetargetUrl- Specified by:
applyEffectsAndStoreModifiedFilein interfaceImageOperationService- Parameters:
file- the image file to apply effectstargetUrl- the target url to save fileimageEffectsTags- 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:ImageOperationServiceApply the effects to theimageResourceand store the new resource to thetargetUrl- Specified by:
applyEffectsAndStoreModifiedResourcein interfaceImageOperationService- Parameters:
imageResource- the image resource to apply effectstargetUrl- the target url to save fileimageEffectsTags- 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:ImageOperationServiceApply the effects to thefileand save it to the temp file. Note: The temp file will be deleted on close.StandardOpenOption.DELETE_ON_CLOSE- Specified by:
applyEffectsin interfaceImageOperationService- Parameters:
file- the image file to apply effectsimageEffectsTags- the image effects- Returns:
- the
InputStreamto the new temp file.
-
applyEffects
public InputStream applyEffects(ResourceWithMetadata imageResource, org.springframework.util.MultiValueMap<String, String> imageEffectsTags) Description copied from interface:ImageOperationServiceApply the effects to theimageResourceand save it to the temp file. Note: The temp file will be deleted on close.StandardOpenOption.DELETE_ON_CLOSE- Specified by:
applyEffectsin interfaceImageOperationService- Parameters:
imageResource- the image resource to apply effectsimageEffectsTags- the image effects- Returns:
- the
InputStreamto the new temp file.
-
isValidImageEffectTags
public boolean isValidImageEffectTags(org.springframework.util.MultiValueMap<String, String> imageEffectsTags) Description copied from interface:ImageOperationServiceCheck that theimageEffectsTagsis valid- Specified by:
isValidImageEffectTagsin interfaceImageOperationService- Parameters:
imageEffectsTags- the image effects- Returns:
- false if
imageEffectsTagscontains the effect names which is not configured by broadleaf.image.availableEffects property orNamedOperationand true otherwise
-
optimizeImage
Description copied from interface:ImageOperationServiceOptimizes a GIF, PNG, or JPEG in an attempt to reduce its size.If
toOptimizeis 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 byStandardOpenOption.DELETE_ON_CLOSE.The given file itself is not modified, as the optimization is applied to a copy.
If
toOptimizewas 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:
optimizeImagein interfaceImageOperationService- Parameters:
toOptimize- the file to be optimized- Returns:
- the
InputStreamof the optimized file, or of the unmodified file if not a GIF, PNG, or JPEG
-
getContentType
-
optimizeImageBatch
Description copied from interface:ImageOperationServiceThis 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:
optimizeImageBatchin interfaceImageOperationService- 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
-
createTempFile
- Throws:
IOException
-
buildOperation
-
buildOperation
-
addNamedOperations
-
addNamedOperation
protected void addNamedOperation(org.im4java.core.Operation operation, NamedOperation namedOperation) -
addOperation
-
isSupportedContentType
-
initOptimizeImgOperation
protected org.im4java.core.Operation initOptimizeImgOperation() -
getDynamicImgOptimizationOperation
protected org.im4java.core.DynamicOperation getDynamicImgOptimizationOperation() -
getImgOptimizationOperation
-
getPngGifImgOperation
-
getJpegImOperation
-
getImgInfo
-
getMimeFileTypeMap
@NonNull protected jakarta.activation.FileTypeMap getMimeFileTypeMap()
-