DefaultImageService implements ImageService, Initializable { public Optional<Rectangle> getFittingRectangle(Attachment attachment, Dimension dimension) throws IOException { int imageWidth = -1; int imageHeight = -1; if (attachment.getMetadata().containsKey("imageDimensions")) { imageWidth = (int) attachment.getMetadata().get("imageDimensions").get("width"); imageHeight = (int) attachment.getMetadata().get("imageDimensions").get("height"); } else { LoadedAttachment loadedAttachment = this.attachmentStore.get().findAndLoadById(attachment.getId()); Image image = loadedAttachment.getData().getObject(loadImage, Image.class); imageWidth = image.getWidth(null); imageHeight = image.getHeight(null); } if (imageWidth < 0 || imageHeight < 0) { return Optional.absent(); } double aspectRatio = (double) imageWidth / (double) imageHeight; double dimensionRatio = dimension.getWidth() / dimension.getHeight(); if (aspectRatio == dimensionRatio) { return Optional.absent(); } int width; int height; int x; int y; if (aspectRatio < dimensionRatio) { x = 0; width = imageWidth; height = Double.valueOf(width / dimensionRatio).intValue(); y = (imageHeight - height) / 2; } else { y = 0; height = imageHeight; width = Double.valueOf(height * dimensionRatio).intValue(); x = (imageWidth - width) / 2; } return Optional.of(new Rectangle(x, y, width, height)); }  void initialize(); InputStream getImage(Attachment attachment, Dimension dimension); InputStream getImage(Attachment attachment, Dimension dimension, Rectangle rectangle); InputStream getImage(Attachment attachment, Rectangle rectangle); Optional<Rectangle> getFittingRectangle(Attachment attachment, Dimension dimension); Optional<Dimension> newDimension(Attachment attachment, Optional<Integer> width, Optional<Integer> height); @Override Optional<Dimension> newDimension(Rectangle boundaries, Optional<Integer> width, Optional<Integer> height);  }
@Test public void testGetFittingBoxWhenWidthIsLimiting() throws Exception { InputStream data = this.getClass().getResourceAsStream("/120x200.gif"); LoadedAttachment attachment = new LoadedAttachment(); attachment.setData(new AttachmentData(data)); when(attachmentStore.findAndLoadById((UUID) anyObject())) .thenReturn(attachment); Rectangle expected = new Rectangle(0, 50, 120, 100); Optional<Rectangle> box = this.mocker.getComponentUnderTest().getFittingRectangle(attachment, new Dimension(120, 100)); Assert.assertEquals(expected, box.get()); }
@Test @Ignore public void testGetFittingBoxWhenHeightIsLimiting() throws Exception { InputStream data = this.getClass().getResourceAsStream("/600x400.gif"); LoadedAttachment attachment = new LoadedAttachment(); attachment.setData(new AttachmentData(data)); when(attachmentStore.findAndLoadById((UUID) anyObject())) .thenReturn(attachment); Rectangle expected = new Rectangle(100, 0, 400, 400); Optional<Rectangle> box = this.mocker.getComponentUnderTest().getFittingRectangle(attachment, new Dimension(150, 150)); Assert.assertEquals(expected, box.get()); expected = new Rectangle(50, 0, 300, 300); box = this.mocker.getComponentUnderTest().getFittingRectangle(attachment, new Dimension(150, 150)); Assert.assertEquals(expected, box.get()); }
@Test public void testGetFittingBoxWhenAspectRatioMatchesDimensions() throws Exception { InputStream data = this.getClass().getResourceAsStream("/120x100.gif"); LoadedAttachment attachment = new LoadedAttachment(); attachment.setData(new AttachmentData(data)); when(attachmentStore.findAndLoadById((UUID) anyObject())) .thenReturn(attachment); Optional<Rectangle> box = this.mocker.getComponentUnderTest().getFittingRectangle(attachment, new Dimension(12, 10)); Assert.assertNull(box.orNull()); }