Package com.liferay.portal.image
Class ImageToolUtil
Object
com.liferay.portal.image.ImageToolUtil
The Image utility class.
- Author:
- Brian Wing Shun Chan, Alexander Chow
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Future<RenderedImage>convertCMYKtoRGB(byte[] bytes, String type) Returns the CMYK image converted to RGB using ImageMagick.static BufferedImageconvertImageType(BufferedImage sourceImage, int type) Returns the image converted to the type.static RenderedImagecrop(RenderedImage renderedImage, int height, int width, int x, int y) static voidencodeGIF(RenderedImage renderedImage, OutputStream outputStream) Encodes the image using the GIF format.static voidencodeWBMP(RenderedImage renderedImage, OutputStream outputStream) Encodes the image using the WBMP format.static RenderedImageflipHorizontal(RenderedImage renderedImage) static RenderedImageflipVertical(RenderedImage renderedImage) static BufferedImagegetBufferedImage(RenderedImage renderedImage) Returns the rendered image as aBufferedImage.static byte[]getBytes(RenderedImage renderedImage, String contentType) Returns the image as abyte[].static com.liferay.portal.kernel.model.Imagestatic com.liferay.portal.kernel.model.Imagestatic com.liferay.portal.kernel.model.Imagestatic com.liferay.portal.kernel.model.Imagestatic com.liferay.portal.kernel.model.Imagestatic com.liferay.portal.kernel.model.Imagestatic com.liferay.portal.kernel.model.ImagegetImage(byte[] bytes) static com.liferay.portal.kernel.model.Imagestatic com.liferay.portal.kernel.model.ImagegetImage(InputStream inputStream) static com.liferay.portal.kernel.model.ImagegetImage(InputStream inputStream, boolean cleanUpStream) static booleanisNullOrDefaultSpacer(byte[] bytes) static com.liferay.portal.kernel.image.ImageBagread(byte[] bytes) Detects the image format and creates anImageBagcontaining theRenderedImageand image type.static com.liferay.portal.kernel.image.ImageBagDetects the image format and creates anImageBagcontaining theRenderedImageand image type.static com.liferay.portal.kernel.image.ImageBagread(InputStream inputStream) static RenderedImagerotate(RenderedImage renderedImage, int degrees) static RenderedImagescale(RenderedImage renderedImage, int width) Returns the scaled image based on the given width with the height calculated to preserve aspect ratio.static RenderedImagescale(RenderedImage renderedImage, int maxHeight, int maxWidth) Returns the scaled image based on the maximum height and width given while preserving the aspect ratio.static voidwrite(RenderedImage renderedImage, String contentType, OutputStream outputStream) Encodes the image using the content or image type.
-
Constructor Details
-
ImageToolUtil
public ImageToolUtil()
-
-
Method Details
-
convertCMYKtoRGB
Returns the CMYK image converted to RGB using ImageMagick. This must be run against the originalbyte[]and not one extracted from aRenderedImage. The latter may potentially have been already been read incorrectly.- Parameters:
bytes- the image to converttype- the image type (e.g., "gif", "jpg", etc.)- Returns:
- the asynchronous process converting the image or
nullif ImageMagick was disabled or if the conversion could not be completed. The conversion may not complete if (1) the image was not in the CMYK colorspace to begin with or (2) there was an error in the conversion process.
-
convertImageType
Returns the image converted to the type.- Parameters:
sourceImage- the image to converttype- the image type to convert to (e.g., "gif", "jpg", etc.)- Returns:
- the converted image
-
crop
-
encodeGIF
public static void encodeGIF(RenderedImage renderedImage, OutputStream outputStream) throws IOException Encodes the image using the GIF format.- Parameters:
renderedImage- the image to encodeoutputStream- the stream to write to- Throws:
IOException- if an IO exception occurred
-
encodeWBMP
public static void encodeWBMP(RenderedImage renderedImage, OutputStream outputStream) throws IOException Encodes the image using the WBMP format.- Parameters:
renderedImage- the image to encodeoutputStream- the stream to write to- Throws:
IOException- if an IO exception occurred
-
flipHorizontal
-
flipVertical
-
getBufferedImage
Returns the rendered image as aBufferedImage.- Parameters:
renderedImage- the original image- Returns:
- the converted image
-
getBytes
Returns the image as abyte[].- Parameters:
renderedImage- the image to readcontentType- the content type (e.g., "image/jpeg") or image type (e.g., "jpg") to use during encoding- Returns:
- the encoded image
- Throws:
IOException- if an IO exception occurred
-
getDefaultCompanyLogo
public static com.liferay.portal.kernel.model.Image getDefaultCompanyLogo() -
getDefaultOrganizationLogo
public static com.liferay.portal.kernel.model.Image getDefaultOrganizationLogo() -
getDefaultSpacer
public static com.liferay.portal.kernel.model.Image getDefaultSpacer() -
getDefaultUserFemalePortrait
public static com.liferay.portal.kernel.model.Image getDefaultUserFemalePortrait() -
getDefaultUserMalePortrait
public static com.liferay.portal.kernel.model.Image getDefaultUserMalePortrait() -
getDefaultUserPortrait
public static com.liferay.portal.kernel.model.Image getDefaultUserPortrait() -
getImage
public static com.liferay.portal.kernel.model.Image getImage(byte[] bytes) throws com.liferay.portal.kernel.exception.ImageResolutionException, IOException - Throws:
com.liferay.portal.kernel.exception.ImageResolutionExceptionIOException
-
getImage
public static com.liferay.portal.kernel.model.Image getImage(File file) throws com.liferay.portal.kernel.exception.ImageResolutionException, IOException - Throws:
com.liferay.portal.kernel.exception.ImageResolutionExceptionIOException
-
getImage
public static com.liferay.portal.kernel.model.Image getImage(InputStream inputStream) throws com.liferay.portal.kernel.exception.ImageResolutionException, IOException - Throws:
com.liferay.portal.kernel.exception.ImageResolutionExceptionIOException
-
getImage
public static com.liferay.portal.kernel.model.Image getImage(InputStream inputStream, boolean cleanUpStream) throws com.liferay.portal.kernel.exception.ImageResolutionException, IOException - Throws:
com.liferay.portal.kernel.exception.ImageResolutionExceptionIOException
-
isNullOrDefaultSpacer
public static boolean isNullOrDefaultSpacer(byte[] bytes) -
read
public static com.liferay.portal.kernel.image.ImageBag read(byte[] bytes) throws com.liferay.portal.kernel.exception.ImageResolutionException, IOException Detects the image format and creates anImageBagcontaining theRenderedImageand image type.- Parameters:
bytes- the bytes to read- Returns:
- the
ImageBag - Throws:
com.liferay.portal.kernel.exception.ImageResolutionException- if the image's dimensions were larger than those specified by portal propertiesimage.tool.image.max.heightandimage.tool.image.max.widthIOException- if an IO exception occurred
-
read
public static com.liferay.portal.kernel.image.ImageBag read(File file) throws com.liferay.portal.kernel.exception.ImageResolutionException, IOException Detects the image format and creates anImageBagcontaining theRenderedImageand image type.- Parameters:
file- the file to read- Returns:
- the
ImageBag - Throws:
com.liferay.portal.kernel.exception.ImageResolutionException- if the image's dimensions were larger than those specified by portal propertiesimage.tool.image.max.heightandimage.tool.image.max.widthIOException- if an IO exception occurred
-
read
public static com.liferay.portal.kernel.image.ImageBag read(InputStream inputStream) throws com.liferay.portal.kernel.exception.ImageResolutionException, IOException - Throws:
com.liferay.portal.kernel.exception.ImageResolutionExceptionIOException
-
rotate
-
scale
Returns the scaled image based on the given width with the height calculated to preserve aspect ratio.- Parameters:
renderedImage- the image to scalewidth- the new width; also used to calculate the new height- Returns:
- the scaled image
-
scale
Returns the scaled image based on the maximum height and width given while preserving the aspect ratio. If the image is already larger in both dimensions, the image will not be scaled.- Parameters:
renderedImage- the image to scalemaxHeight- the maximum height allowed for imagemaxWidth- the maximum width allowed for image- Returns:
- the scaled image
-
write
public static void write(RenderedImage renderedImage, String contentType, OutputStream outputStream) throws IOException Encodes the image using the content or image type.- Parameters:
renderedImage- the image to encodecontentType- the content type (e.g., "image/jpeg") or image type (e.g., "jpg") to use during encodingoutputStream- the stream to write to- Throws:
IOException- if an IO exception occurred
-