Package org.ametys.core.util
Class ImageHelper
- java.lang.Object
-
- org.ametys.core.util.ImageHelper
-
public final class ImageHelper extends Object
Helper for manipulating images.
-
-
Field Summary
Fields Modifier and Type Field Description private static float
IMAGE_QUALITY
-
Constructor Summary
Constructors Modifier Constructor Description private
ImageHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description protected static BufferedImage
_cropImage(BufferedImage src, int height, int width)
Crop the image to the size specified to the center of the image.protected static BufferedImage
_cropImage(BufferedImage src, int x, int y, int height, int width)
Crop the image by a specified rectangular region.private static boolean
_isJpeg(String format)
private static boolean
_needsChanges(int height, int width, int maxHeight, int maxWidth, int cropHeight, int cropWidth)
protected static BufferedImage
_resizeImage(BufferedImage src, double ratio)
Resize the buffered imageprotected static BufferedImage
_resizeImage(BufferedImage src, int height, int width, int maxHeight, int maxWidth)
Resize the buffered imagestatic BufferedImage
generateCroppedImage(BufferedImage src, int height, int width)
Crop the image in the center, at the specified dimensions.static BufferedImage
generateCroppedImage(BufferedImage src, int x, int y, int height, int width)
Crop the image by a specified rectangular region.static void
generateCroppedImage(InputStream is, OutputStream os, String format, int x, int y, int height, int width)
Crop an image from a source InputStream.static BufferedImage
generateThumbnail(BufferedImage src, double ratio)
Generates a BufferedImage with specified size instructions, scaling if necessary.static BufferedImage
generateThumbnail(BufferedImage src, int height, int width, int maxHeight, int maxWidth)
Generates a BufferedImage with specified size instructions, scaling if necessary.static BufferedImage
generateThumbnail(BufferedImage src, int height, int width, int maxHeight, int maxWidth, int cropHeight, int cropWidth)
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.static void
generateThumbnail(InputStream is, OutputStream os, String format, double ratio)
Generates a thumbnail from a source InputStream.static void
generateThumbnail(InputStream is, OutputStream os, String format, int height, int width, int maxHeight, int maxWidth)
Generates a thumbnail from a source InputStream.static void
generateThumbnail(InputStream is, OutputStream os, String format, int height, int width, int maxHeight, int maxWidth, int cropHeight, int cropWidth)
Generates a thumbnail from a source InputStream.static BufferedImage
read(InputStream is)
Returns a BufferedImage as from the supplied input stream
-
-
-
Field Detail
-
IMAGE_QUALITY
private static final float IMAGE_QUALITY
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ImageHelper
private ImageHelper()
-
-
Method Detail
-
_needsChanges
private static boolean _needsChanges(int height, int width, int maxHeight, int maxWidth, int cropHeight, int cropWidth)
-
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 regiony
- the Y coordinate of the upper-left corner of the specified rectangular regionheight
- the width of the specified rectangular regionwidth
- 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 returnedBufferedImage
shares the same data array as the original image.- Parameters:
src
- the source image.height
- the width of the specified rectangular regionwidth
- 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 returnedBufferedImage
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 regiony
- the Y coordinate of the upper-left corner of the specified rectangular regionheight
- the width of the specified rectangular regionwidth
- 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 regionwidth
- 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 returnedBufferedImage
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 regionwidth
- 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 imageheight
- 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 imageratio
- 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
-
-