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 BufferedImage
convertImageType
(BufferedImage sourceImage, int type) Returns the image converted to the type.static RenderedImage
crop
(RenderedImage renderedImage, int height, int width, int x, int y) static void
encodeGIF
(RenderedImage renderedImage, OutputStream outputStream) Encodes the image using the GIF format.static void
encodeWBMP
(RenderedImage renderedImage, OutputStream outputStream) Encodes the image using the WBMP format.static RenderedImage
flipHorizontal
(RenderedImage renderedImage) static RenderedImage
flipVertical
(RenderedImage renderedImage) static BufferedImage
getBufferedImage
(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.Image
static com.liferay.portal.kernel.model.Image
static com.liferay.portal.kernel.model.Image
static com.liferay.portal.kernel.model.Image
static com.liferay.portal.kernel.model.Image
static com.liferay.portal.kernel.model.Image
static com.liferay.portal.kernel.model.Image
getImage
(byte[] bytes) static com.liferay.portal.kernel.model.Image
static com.liferay.portal.kernel.model.Image
getImage
(InputStream inputStream) static com.liferay.portal.kernel.model.Image
getImage
(InputStream inputStream, boolean cleanUpStream) static boolean
isNullOrDefaultSpacer
(byte[] bytes) static com.liferay.portal.kernel.image.ImageBag
read
(byte[] bytes) Detects the image format and creates anImageBag
containing theRenderedImage
and image type.static com.liferay.portal.kernel.image.ImageBag
Detects the image format and creates anImageBag
containing theRenderedImage
and image type.static com.liferay.portal.kernel.image.ImageBag
read
(InputStream inputStream) static RenderedImage
rotate
(RenderedImage renderedImage, int degrees) static RenderedImage
scale
(RenderedImage renderedImage, int width) Returns the scaled image based on the given width with the height calculated to preserve aspect ratio.static RenderedImage
scale
(RenderedImage renderedImage, int maxHeight, int maxWidth) Returns the scaled image based on the maximum height and width given while preserving the aspect ratio.static void
write
(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
null
if 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.ImageResolutionException
IOException
-
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.ImageResolutionException
IOException
-
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.ImageResolutionException
IOException
-
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.ImageResolutionException
IOException
-
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 anImageBag
containing theRenderedImage
and 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.height
andimage.tool.image.max.width
IOException
- 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 anImageBag
containing theRenderedImage
and 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.height
andimage.tool.image.max.width
IOException
- 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.ImageResolutionException
IOException
-
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
-