Class ImageHelper

java.lang.Object
org.ametys.core.util.ImageHelper

public final class ImageHelper extends Object
Helper for manipulating images.
  • Method Details

    • read

      public static BufferedImage read(InputStream is) throws IOException
      Returns a BufferedImage as from the supplied input stream
      Parameters:
      is - The input stream
      Returns:
      The buffered image
      Throws:
      IOException - if an error occurs during reading.
    • generateThumbnail

      public static void generateThumbnail(InputStream is, OutputStream os, String format, int height, int width, int maxHeight, int maxWidth, int cropHeight, int cropWidth) throws IOException
      Generates a thumbnail from a source InputStream. Note that if final width and height are equals to source width and height, the stream is just copied. If the image should be both cropped and resized, the resizing will be done after the cropping.
      Parameters:
      is - the source.
      os - the destination.
      format - the image format. Must be one of "gif", "png" or "jpg".
      height - the specified height. Ignored if negative.
      width - the specified width. Ignored if negative.
      maxHeight - the maximum image height. Ignored if height or width is specified.
      maxWidth - the maximum image width. Ignored if height or width is specified.
      cropHeight - the height of the cropped image. Ignore if negative.
      cropWidth - the width of the cropped image. Ignore if negative.
      Throws:
      IOException - if an error occurs when manipulating streams.
    • generateThumbnail

      public static BufferedImage generateThumbnail(BufferedImage src, int height, int width, int maxHeight, int maxWidth, int cropHeight, int cropWidth) throws IOException
      Generates a BufferedImage with specified size instructions, scaling if necessary.
      If the image should be both cropped and resized, the resizing will be done after the cropping.
      Parameters:
      src - the source image.
      height - the specified height. Ignored if negative.
      width - the specified width. Ignored if negative.
      maxHeight - the maximum image height. Ignored if height or width is specified.
      maxWidth - the maximum image width. Ignored if height or width is specified.
      cropHeight - the height of the cropped image. Ignore if negative.
      cropWidth - the width of the cropped image. Ignore if negative.
      Returns:
      a scaled BufferedImage. If no size modification is required, this will return the src image.
      Throws:
      IOException - If the source image is not readable
    • generateThumbnail

      public static void generateThumbnail(InputStream is, OutputStream os, String format, int height, int width, int maxHeight, int maxWidth) throws IOException
      Generates a thumbnail from a source InputStream. Note that if final width and height are equals to source width and height, the stream is just copied.
      Parameters:
      is - the source.
      os - the destination.
      format - the image format. Must be one of "gif", "png" or "jpg".
      height - the specified height. Ignored if negative.
      width - the specified width. Ignored if negative.
      maxHeight - the maximum image height. Ignored if height or width is specified.
      maxWidth - the maximum image width. Ignored if height or width is specified.
      Throws:
      IOException - if an error occurs when manipulating streams.
    • generateThumbnail

      public static BufferedImage generateThumbnail(BufferedImage src, int height, int width, int maxHeight, int maxWidth) throws IOException
      Generates a BufferedImage with specified size instructions, scaling if necessary.
      Parameters:
      src - the source image.
      height - the specified height. Ignored if negative.
      width - the specified width. Ignored if negative.
      maxHeight - the maximum image height. Ignored if height or width is specified.
      maxWidth - the maximum image width. Ignored if height or width is specified.
      Returns:
      a scaled BufferedImage. If no size modification is required, this will return the src image.
      Throws:
      IOException - If the source image is not readable
    • generateThumbnail

      public static void generateThumbnail(InputStream is, OutputStream os, String format, double ratio) throws IOException
      Generates a thumbnail from a source InputStream. Note that if final width and height are equals to source width and height, the stream is just copied.
      Parameters:
      is - the source.
      os - the destination.
      format - the image format. Must be one of "gif", "png" or "jpg".
      ratio - ratio to resize the image (1 = same size)
      Throws:
      IOException - if an error occurs when manipulating streams.
    • generateThumbnail

      public static BufferedImage generateThumbnail(BufferedImage src, double ratio) throws IOException
      Generates a BufferedImage with specified size instructions, scaling if necessary.
      Parameters:
      src - the source image.
      ratio - ratio to resize the image (1 = same size)
      Returns:
      a scaled BufferedImage. If no size modification is required, this will return the src image.
      Throws:
      IOException - If the source image is not readable
    • generateCroppedImage

      public static void generateCroppedImage(InputStream is, OutputStream os, String format, int x, int y, int height, int width) throws IOException
      Crop an image from a source InputStream. Note that if any of the coordinate and size are negatives, the image will just be copied.
      Parameters:
      is - the source.
      os - the destination.
      format - the image format. Must be one of "gif", "png" or "jpg".
      x - The X coordinate of the upper-left corner of the specified rectangular region
      y - the Y coordinate of the upper-left corner of the specified rectangular region
      height - the width of the specified rectangular region
      width - the height of the specified rectangular region
      Throws:
      IOException - If an error occurs
    • generateCroppedImage

      public static BufferedImage generateCroppedImage(BufferedImage src, int height, int width) throws IOException
      Crop the image in the center, at the specified dimensions. The returned BufferedImage shares the same data array as the original image.
      Parameters:
      src - the source image.
      height - the width of the specified rectangular region
      width - the height of the specified rectangular region
      Returns:
      a scaled BufferedImage. If no size modification is required, this will return the src image.
      Throws:
      IOException - If the source image is not readable
    • generateCroppedImage

      public static BufferedImage generateCroppedImage(BufferedImage src, int x, int y, int height, int width) throws IOException
      Crop the image by a specified rectangular region. The returned BufferedImage shares the same data array as the original image.
      Parameters:
      src - the source image.
      x - The X coordinate of the upper-left corner of the specified rectangular region
      y - the Y coordinate of the upper-left corner of the specified rectangular region
      height - the width of the specified rectangular region
      width - the height of the specified rectangular region
      Returns:
      a scaled BufferedImage. If no size modification is required, this will return the src image.
      Throws:
      IOException - If the source image is not readable
    • _cropImage

      protected static BufferedImage _cropImage(BufferedImage src, int height, int width) throws IOException
      Crop the image to the size specified to the center of the image.
      Parameters:
      src - the source image.
      height - the width of the specified rectangular region
      width - the height of the specified rectangular region
      Returns:
      The cropped image as a BufferedImage
      Throws:
      IOException - If the source image is not readable
    • _cropImage

      protected static BufferedImage _cropImage(BufferedImage src, int x, int y, int height, int width) throws IOException
      Crop the image by a specified rectangular region. The returned BufferedImage shares the same data array as the original image.
      Parameters:
      src - the source image.
      x - The X coordinate of the upper-left corner of the specified rectangular region.
      y - the Y coordinate of the upper-left corner of the specified rectangular region.
      height - the width of the specified rectangular region
      width - the height of the specified rectangular region
      Returns:
      a scaled BufferedImage. If no size modification is required, this will return the src image.
      Throws:
      IOException - If the source image is not readable
    • _resizeImage

      protected static BufferedImage _resizeImage(BufferedImage src, int height, int width, int maxHeight, int maxWidth) throws IOException
      Resize the buffered image
      Parameters:
      src - the source image
      height - the specified height. Ignored if negative.
      width - the specified width. Ignored if negative.
      maxHeight - the maximum image height. Ignored if height or width is specified.
      maxWidth - the maximum image width. Ignored if height or width is specified.
      Returns:
      a scaled BufferedImage. If no size modification is required, this will return the src image.
      Throws:
      IOException - If the source image is not readable
    • _resizeImage

      protected static BufferedImage _resizeImage(BufferedImage src, double ratio) throws IOException
      Resize the buffered image
      Parameters:
      src - the source image
      ratio - ratio to resize the image (1 = same size)
      Returns:
      a scaled BufferedImage. If no size modification is required, this will return the src image.
      Throws:
      IOException - If the source image is not readable