Cleaned up image resizing

This commit is contained in:
nathan 2017-09-03 15:15:31 +02:00
parent 0a2ffb451d
commit c8e88fb568

View File

@ -223,30 +223,16 @@ class ImageResizeUtil {
// resize the image, keep aspect ratio // resize the image, keep aspect ratio
Image image = ImageUtil.getImageImmediate(ImageIO.read(inputStream)); Image image = ImageUtil.getImageImmediate(ImageIO.read(inputStream));
int height = image.getHeight(null); // the smaller dimension have padding, so the larger dimension is the size of this image.
int width = image.getWidth(null); BufferedImage bufferedImage = ImageUtil.getSquareBufferedImage(image);
if (width > height) {
// fit on the width
image = image.getScaledInstance(size, -1, Image.SCALE_SMOOTH);
} else {
// fit on the height
image = image.getScaledInstance(-1, size, Image.SCALE_SMOOTH);
}
// now write out the new one // now write out the new one
BufferedImage bufferedImage = ImageUtil.getSquareBufferedImage(image);
ImageIO.write(bufferedImage, "png", newFile); // made up extension ImageIO.write(bufferedImage, "png", newFile); // made up extension
return newFile; return newFile;
} }
public static public static
File shouldResizeOrCache(final boolean isTrayImage, final File imageFile) { File shouldResizeOrCache(final boolean isTrayImage, final File imageFile) {
if (imageFile == null) { if (imageFile == null) {
@ -324,8 +310,8 @@ class ImageResizeUtil {
try { try {
final Image trayImage = ImageUtil.getImageImmediate(image); final Image trayImage = ImageUtil.getImageImmediate(image);
BufferedImage bufferedImage = ImageUtil.getBufferedImage(trayImage); BufferedImage bufferedImage = ImageUtil.getBufferedImage(trayImage);
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", os); ImageIO.write(bufferedImage, "png", os);
InputStream imageInputStream = new ByteArrayInputStream(os.toByteArray()); InputStream imageInputStream = new ByteArrayInputStream(os.toByteArray());