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 ImageProperties
protected List<NamedOperation>
protected static final String
protected final org.im4java.core.Operation
protected final StorageProvider
protected static final String
-
Constructor Summary
ConstructorsConstructorDescriptionImageMagickImageOperationService
(StorageProvider storageProvider, ImageProperties imageProperties, jakarta.activation.FileTypeMap mimeFileTypeMap) -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addNamedOperation
(org.im4java.core.Operation operation, NamedOperation namedOperation) protected void
addNamedOperations
(org.im4java.core.Operation operation, List<String> namedOperationNames) protected void
addOperation
(org.im4java.core.Operation operation, String name, String value) applyEffects
(ResourceWithMetadata imageResource, org.springframework.util.MultiValueMap<String, String> imageEffectsTags) Apply the effects to theimageResource
and save it to the temp file.applyEffects
(File file, org.springframework.util.MultiValueMap<String, String> imageEffectsTags) Apply the effects to thefile
and save it to the temp file.applyEffectsAndStoreModifiedFile
(File file, String targetUrl, org.springframework.util.MultiValueMap<String, String> imageEffectsTags) Apply the effects to thefile
and store the new file to thetargetUrl
applyEffectsAndStoreModifiedResource
(ResourceWithMetadata imageResource, String targetUrl, org.springframework.util.MultiValueMap<String, String> imageEffectsTags) Apply the effects to theimageResource
and store the new resource to thetargetUrl
protected org.im4java.core.Operation
buildOperation
(File file, org.springframework.util.MultiValueMap<String, String> imageEffectsTags) protected org.im4java.core.Operation
buildOperation
(String fileExtension, org.springframework.util.MultiValueMap<String, String> imageEffectsTags) protected File
createTempFile
(String fileName) protected String
getContentType
(org.springframework.web.multipart.MultipartFile file) protected org.im4java.core.DynamicOperation
org.im4java.utils.BatchConverter
org.im4java.core.ConvertCmd
getImgInfo
(String image) protected org.im4java.core.IMOperation
getImgOptimizationOperation
(List<String> imgInfoOut) protected org.im4java.core.IMOperation
getJpegImOperation
(List<String> imgInfoOut) protected jakarta.activation.FileTypeMap
protected org.im4java.core.IMOperation
getPngGifImgOperation
(List<String> imgInfoOut) protected org.im4java.core.Operation
protected boolean
isSupportedContentType
(String contentType) boolean
isValidImageEffectTags
(org.springframework.util.MultiValueMap<String, String> imageEffectsTags) Check that theimageEffectsTags
is 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.void
setNamedOperations
(List<NamedOperation> namedOperations) Set theList
ofNamedOperation
-
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:ImageOperationService
Set theList
ofNamedOperation
- Specified by:
setNamedOperations
in interfaceImageOperationService
- Parameters:
namedOperations
- theList
ofNamedOperation
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 thefile
and store the new file to thetargetUrl
- Specified by:
applyEffectsAndStoreModifiedFile
in 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:ImageOperationService
Apply the effects to theimageResource
and store the new resource to thetargetUrl
- Specified by:
applyEffectsAndStoreModifiedResource
in 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:ImageOperationService
Apply the effects to thefile
and save it to the temp file. Note: The temp file will be deleted on close.StandardOpenOption.DELETE_ON_CLOSE
- Specified by:
applyEffects
in interfaceImageOperationService
- Parameters:
file
- the image file to apply effectsimageEffectsTags
- 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 theimageResource
and save it to the temp file. Note: The temp file will be deleted on close.StandardOpenOption.DELETE_ON_CLOSE
- Specified by:
applyEffects
in interfaceImageOperationService
- Parameters:
imageResource
- the image resource to apply effectsimageEffectsTags
- 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 theimageEffectsTags
is valid- Specified by:
isValidImageEffectTags
in interfaceImageOperationService
- Parameters:
imageEffectsTags
- the image effects- Returns:
- false if
imageEffectsTags
contains the effect names which is not configured by broadleaf.image.availableEffects property orNamedOperation
and true otherwise
-
optimizeImage
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 byStandardOpenOption.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 interfaceImageOperationService
- 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
-
optimizeImageBatch
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 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()
-