ImageServiceImpl extends BaseGenericServiceImpl<SeoImage> implements ImageService { int[] cropImageToCenter(final int targetX, final int targetY, final int originalX, final int originalY) { BigDecimal sourceRatio = new BigDecimal(originalX).divide(new BigDecimal(originalY), 10, BigDecimal.ROUND_HALF_UP); BigDecimal targetRatio = new BigDecimal(targetX).divide(new BigDecimal(targetY), 10, BigDecimal.ROUND_HALF_UP); final int cropWidth, cropHeight, cropX, cropY; if (sourceRatio.compareTo(targetRatio) < 0) { cropWidth = originalX; cropHeight = new BigDecimal(originalY).divide((targetRatio.divide(sourceRatio, 10, BigDecimal.ROUND_HALF_UP)), 0, BigDecimal.ROUND_HALF_UP).intValue(); cropX = 0; cropY = (originalY - cropHeight) / 2; } else { cropHeight = originalY; cropWidth = new BigDecimal(originalX).divide((sourceRatio.divide(targetRatio, 10, BigDecimal.ROUND_HALF_UP)), 0, BigDecimal.ROUND_HALF_UP).intValue(); cropX = (originalX - cropWidth) / 2; cropY = 0; } return new int[] { cropX, cropY, cropWidth, cropHeight }; } ImageServiceImpl(final GenericDAO<SeoImage, Long> seoImageDao,
                            final MediaFileNameStrategyResolver mediaFileNameStrategyResolver,
                            final String allowedSizes,
                            final int borderColorR,
                            final int borderColorG,
                            final int borderColorB,
                            final boolean cropToFit,
                            final int forceCropToFitOnSize,
                            final boolean replaceFilesModeOn,
                            final IOProvider ioProvider); void setConfig(final Resource config); @Override byte[] resizeImage(final String filename,
                              final byte[] content,
                              final String width,
                              final String height); @Override byte[] resizeImage(final String filename,
                              final byte[] content,
                              final String width,
                              final String height,
                              final boolean cropToFit); @Override boolean isSizeAllowed(final String size); @Override boolean isSizeAllowed(final String width, final String height); @Override MediaFileNameStrategy getImageNameStrategy(final String url); @Override byte[] resizeImage(final String original,
                              final String resized,
                              final String width,
                              final String height); @Override byte[] resizeImage(final String original,
                              final String resized,
                              final String width,
                              final String height,
                              final boolean cropToFit); @Override boolean isImageInRepository(final String fullFileName,
                                       final String code,
                                       final String storagePrefix,
                                       final String pathToRepository); @Override String addImageToRepository(final String fullFileName,
                                       final String code,
                                       final byte[] imgBody,
                                       final String storagePrefix,
                                       final String pathToRepository); @Override byte[] imageToByteArray(final String fileName,
                                   final String code,
                                   final String storagePrefix,
                                   final String pathToRepository); @Override boolean deleteImage(final String imageFileName,
                               final String storagePrefix,
                               final String pathToRepository); @Override SeoImage getSeoImage(final String imageName); SystemService getSystemService();  }
@Test public void testCropImageToCenter() throws Exception { final ImageServiceImpl srv = new ImageServiceImpl(null, null, "", 0, 0, 0, true, 0, true, null); final int[] croppedExactEven = srv.cropImageToCenter(100, 100, 100, 100); assertNotNull(croppedExactEven); assertArrayEquals(croppedExactEven, new int[] { 0, 0, 100, 100}); final int[] croppedExactOdd = srv.cropImageToCenter(51, 51, 51, 51); assertNotNull(croppedExactOdd); assertArrayEquals(croppedExactOdd, new int[] { 0, 0, 51, 51}); final int[] croppedExactScaleEven = srv.cropImageToCenter(100, 100, 1000, 1000); assertNotNull(croppedExactScaleEven); assertArrayEquals(croppedExactScaleEven, new int[] { 0, 0, 1000, 1000}); final int[] croppedExactScaleOdd = srv.cropImageToCenter(51, 51, 510, 510); assertNotNull(croppedExactScaleOdd); assertArrayEquals(croppedExactScaleOdd, new int[] { 0, 0, 510, 510}); final int[] croppedByHeightEven = srv.cropImageToCenter(80, 80, 150, 250); assertNotNull(croppedByHeightEven); assertArrayEquals(croppedByHeightEven, new int[] { 0, 50, 150, 150}); final int[] croppedByHeightOdd = srv.cropImageToCenter(80, 80, 151, 251); assertNotNull(croppedByHeightOdd); assertArrayEquals(croppedByHeightOdd, new int[] { 0, 50, 151, 151}); final int[] croppedByWidthEven = srv.cropImageToCenter(80, 80, 250, 150); assertNotNull(croppedByWidthEven); assertArrayEquals(croppedByWidthEven, new int[] { 50, 0, 150, 150}); final int[] croppedByWidthOdd = srv.cropImageToCenter(80, 80, 251, 151); assertNotNull(croppedByWidthOdd); assertArrayEquals(croppedByWidthOdd, new int[] { 50, 0, 151, 151}); }