ImgscalrImageManipulator extends AbstractImageManipulator { protected BufferedImage resizeIfRequested(BufferedImage originalImage, ImageManipulationParameters parameters) { int originalHeight = originalImage.getHeight(); int originalWidth = originalImage.getWidth(); double aspectRatio = (double) originalWidth / (double) originalHeight; Integer pHeight = NumberUtils.toInt(parameters.getHeight(), 0); Integer pWidth = NumberUtils.toInt(parameters.getWidth(), 0); if (pHeight != 0 || pWidth != 0) { if (pHeight == 0 && pWidth == originalWidth) { return originalImage; } if (pWidth == 0 && pHeight == originalHeight) { return originalImage; } if (pWidth != 0 && pWidth == originalWidth && pHeight != 0 && pHeight == originalHeight) { return originalImage; } ResizeMode resizeMode = parameters.getResizeMode(); if (resizeMode == ResizeMode.SMART) { if (parameters.getWidth() != null && parameters.getWidth().equals("auto")) { pWidth = originalWidth; } if (parameters.getHeight() != null && parameters.getHeight().equals("auto")) { pHeight = originalHeight; } } int width = pWidth == 0 ? (int) (pHeight * aspectRatio) : pWidth; int height = pHeight == 0 ? (int) (width / aspectRatio) : pHeight; if (pWidth != 0 && pHeight != 0 && resizeMode == ResizeMode.SMART) { double pAspectRatio = (double) pWidth / (double) pHeight; if (aspectRatio != pAspectRatio) { if (aspectRatio < pAspectRatio) { int resizeHeight = Math.max(1, (int) (originalWidth / pAspectRatio)); int startY = (int) (originalHeight * 0.5 - resizeHeight * 0.5); originalImage = crop(originalImage, new ImageRect(0, startY, resizeHeight, originalWidth)); } else { int resizeWidth = Math.max(1, (int) (originalHeight * pAspectRatio)); int startX = (int) (originalWidth * 0.5 - resizeWidth * 0.5); originalImage = crop(originalImage, new ImageRect(startX, 0, originalHeight, resizeWidth)); } } } if (pWidth != 0 && pHeight != 0 && resizeMode == ResizeMode.PROP) { double pAspectRatio = (double) pWidth / (double) pHeight; if (aspectRatio < pAspectRatio) { width = Math.max(1, (int) (pHeight * aspectRatio)); height = Math.max(1, pHeight); } else { width = Math.max(1, pWidth); height = Math.max(1, (int) (pWidth / aspectRatio)); } if (width == originalWidth && height == originalHeight) { return originalImage; } } try { BufferedImage image = Scalr.apply(originalImage, new ResampleOp(width, height, options.getResampleFilter().getFilter())); originalImage.flush(); return image; } catch (IllegalArgumentException e) { throw error(BAD_REQUEST, "image_error_resizing_failed", e); } } return originalImage; } ImgscalrImageManipulator(Vertx vertx, MeshOptions options, BootstrapInitializer boot);   ImgscalrImageManipulator(Vertx vertx, ImageManipulatorOptions options, BootstrapInitializer boot); @Override Single<String> handleResize(Binary binary, ImageManipulationParameters parameters); @Override int[] calculateDominantColor(BufferedImage image); @Override Single<Map<String, String>> getMetadata(InputStream ins);  }
@Test public void testResizeImage() { BufferedImage bi = new BufferedImage(100, 200, BufferedImage.TYPE_INT_ARGB); bi = manipulator.resizeIfRequested(bi, new ImageManipulationParametersImpl().setWidth(200)); assertEquals(200, bi.getWidth()); assertEquals(400, bi.getHeight()); bi = new BufferedImage(100, 200, BufferedImage.TYPE_INT_ARGB); BufferedImage outputImage = manipulator.resizeIfRequested(bi, new ImageManipulationParametersImpl().setWidth(100)); assertEquals(100, bi.getWidth()); assertEquals(200, bi.getHeight()); assertEquals("The image should not have been resized since the parameters match the source image dimension.", bi.hashCode(), outputImage .hashCode()); bi = new BufferedImage(100, 200, BufferedImage.TYPE_INT_ARGB); bi = manipulator.resizeIfRequested(bi, new ImageManipulationParametersImpl().setHeight(50)); assertEquals(25, bi.getWidth()); assertEquals(50, bi.getHeight()); bi = new BufferedImage(100, 200, BufferedImage.TYPE_INT_ARGB); outputImage = manipulator.resizeIfRequested(bi, new ImageManipulationParametersImpl().setHeight(200)); assertEquals(100, bi.getWidth()); assertEquals(200, bi.getHeight()); assertEquals("The image should not have been resized since the parameters match the source image dimension.", bi.hashCode(), outputImage .hashCode()); bi = new BufferedImage(100, 200, BufferedImage.TYPE_INT_ARGB); bi = manipulator.resizeIfRequested(bi, new ImageManipulationParametersImpl().setWidth(200).setHeight(300)); assertEquals(200, bi.getWidth()); assertEquals(300, bi.getHeight()); bi = new BufferedImage(100, 200, BufferedImage.TYPE_INT_ARGB); outputImage = manipulator.resizeIfRequested(bi, new ImageManipulationParametersImpl()); assertEquals(100, outputImage.getWidth()); assertEquals(200, outputImage.getHeight()); assertEquals("The image should not have been resized since no parameters were set.", bi.hashCode(), outputImage.hashCode()); bi = new BufferedImage(100, 200, BufferedImage.TYPE_INT_ARGB); outputImage = manipulator.resizeIfRequested(bi, new ImageManipulationParametersImpl().setWidth(100).setHeight(200)); assertEquals(100, bi.getWidth()); assertEquals(200, bi.getHeight()); assertEquals("The image should not have been resized since the parameters match the source image dimension.", bi.hashCode(), outputImage .hashCode()); }