Class ImageToolUtil

Object
com.liferay.portal.image.ImageToolUtil

public class ImageToolUtil extends Object
The Image utility class.
Author:
Brian Wing Shun Chan, Alexander Chow
  • Constructor Details

    • ImageToolUtil

      public ImageToolUtil()
  • Method Details

    • convertCMYKtoRGB

      public static Future<RenderedImage> convertCMYKtoRGB(byte[] bytes, String type)
      Returns the CMYK image converted to RGB using ImageMagick. This must be run against the original byte[] and not one extracted from a RenderedImage. The latter may potentially have been already been read incorrectly.
      Parameters:
      bytes - the image to convert
      type - 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

      public static BufferedImage convertImageType(BufferedImage sourceImage, int type)
      Returns the image converted to the type.
      Parameters:
      sourceImage - the image to convert
      type - the image type to convert to (e.g., "gif", "jpg", etc.)
      Returns:
      the converted image
    • crop

      public static RenderedImage crop(RenderedImage renderedImage, int height, int width, int x, int y)
    • encodeGIF

      public static void encodeGIF(RenderedImage renderedImage, OutputStream outputStream) throws IOException
      Encodes the image using the GIF format.
      Parameters:
      renderedImage - the image to encode
      outputStream - 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 encode
      outputStream - the stream to write to
      Throws:
      IOException - if an IO exception occurred
    • flipHorizontal

      public static RenderedImage flipHorizontal(RenderedImage renderedImage)
    • flipVertical

      public static RenderedImage flipVertical(RenderedImage renderedImage)
    • getBufferedImage

      public static BufferedImage getBufferedImage(RenderedImage renderedImage)
      Returns the rendered image as a BufferedImage.
      Parameters:
      renderedImage - the original image
      Returns:
      the converted image
    • getBytes

      public static byte[] getBytes(RenderedImage renderedImage, String contentType) throws IOException
      Returns the image as a byte[].
      Parameters:
      renderedImage - the image to read
      contentType - 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 an ImageBag containing the RenderedImage 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 properties image.tool.image.max.height and image.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 an ImageBag containing the RenderedImage 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 properties image.tool.image.max.height and image.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

      public static RenderedImage rotate(RenderedImage renderedImage, int degrees)
    • scale

      public static RenderedImage scale(RenderedImage renderedImage, int width)
      Returns the scaled image based on the given width with the height calculated to preserve aspect ratio.
      Parameters:
      renderedImage - the image to scale
      width - the new width; also used to calculate the new height
      Returns:
      the scaled image
    • scale

      public 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. If the image is already larger in both dimensions, the image will not be scaled.
      Parameters:
      renderedImage - the image to scale
      maxHeight - the maximum height allowed for image
      maxWidth - 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 encode
      contentType - the content type (e.g., "image/jpeg") or image type (e.g., "jpg") to use during encoding
      outputStream - the stream to write to
      Throws:
      IOException - if an IO exception occurred