@Test public void testScaleImageToCenter() throws Exception { final ImageServiceImpl srv = new ImageServiceImpl(null, null, "", 0, 0, 0, true, 50, true, null); final int[] scaledExactEven = srv.scaleImageToCenter(100, 100, 100, 100); assertNotNull(scaledExactEven); assertArrayEquals(scaledExactEven, new int[] { 0, 0, 100, 100}); final int[] scaledExactOdd = srv.scaleImageToCenter(51, 51, 51, 51); assertNotNull(scaledExactOdd); assertArrayEquals(scaledExactOdd, new int[] { 0, 0, 51, 51}); final int[] scaledExactScaleEven = srv.scaleImageToCenter(100, 100, 1000, 1000); assertNotNull(scaledExactScaleEven); assertArrayEquals(scaledExactScaleEven, new int[] { 0, 0, 100, 100}); final int[] scaledExactScaleOdd = srv.scaleImageToCenter(51, 51, 510, 510); assertNotNull(scaledExactScaleOdd); assertArrayEquals(scaledExactScaleOdd, new int[] { 0, 0, 51, 51}); final int[] scaledByHeightEven = srv.scaleImageToCenter(80, 80, 150, 250); assertNotNull(scaledByHeightEven); assertArrayEquals(scaledByHeightEven, new int[] { 16, 0, 48, 80}); final int[] scaledByHeightOdd = srv.scaleImageToCenter(80, 80, 151, 251); assertNotNull(scaledByHeightOdd); assertArrayEquals(scaledByHeightOdd, new int[] { 16, 0, 48, 80}); final int[] scaledByWidthEven = srv.scaleImageToCenter(80, 80, 250, 150); assertNotNull(scaledByWidthEven); assertArrayEquals(scaledByWidthEven, new int[] { 0, 16, 80, 48}); final int[] scaledByWidthOdd = srv.scaleImageToCenter(80, 80, 251, 151); assertNotNull(scaledByWidthOdd); assertArrayEquals(scaledByWidthOdd, new int[] { 0, 16, 80, 48}); }
int[] scaleImageToCenter(final int targetX, final int targetY, final int originalX, final int originalY) { final BigDecimal xScale = new BigDecimal(targetX).divide(new BigDecimal(originalX), 10, BigDecimal.ROUND_HALF_UP); final BigDecimal yScale = new BigDecimal(targetY).divide(new BigDecimal(originalY), 10, BigDecimal.ROUND_HALF_UP); final int scaleWidth, scaleHeight, padX, padY; if (xScale.compareTo(yScale) < 0) { scaleWidth = targetX; scaleHeight = new BigDecimal(originalY).multiply(xScale).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); padX = 0; padY = (targetY - scaleHeight) / 2; } else { scaleHeight = targetY; scaleWidth = new BigDecimal(originalX).multiply(yScale).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); padX = (targetX - scaleWidth) / 2; padY = 0; } return new int[] { padX, padY, scaleWidth, scaleHeight }; }
ImageServiceImpl extends BaseGenericServiceImpl<SeoImage> implements ImageService { int[] scaleImageToCenter(final int targetX, final int targetY, final int originalX, final int originalY) { final BigDecimal xScale = new BigDecimal(targetX).divide(new BigDecimal(originalX), 10, BigDecimal.ROUND_HALF_UP); final BigDecimal yScale = new BigDecimal(targetY).divide(new BigDecimal(originalY), 10, BigDecimal.ROUND_HALF_UP); final int scaleWidth, scaleHeight, padX, padY; if (xScale.compareTo(yScale) < 0) { scaleWidth = targetX; scaleHeight = new BigDecimal(originalY).multiply(xScale).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); padX = 0; padY = (targetY - scaleHeight) / 2; } else { scaleHeight = targetY; scaleWidth = new BigDecimal(originalX).multiply(yScale).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); padX = (targetX - scaleWidth) / 2; padY = 0; } return new int[] { padX, padY, scaleWidth, scaleHeight }; } }
ImageServiceImpl extends BaseGenericServiceImpl<SeoImage> implements ImageService { int[] scaleImageToCenter(final int targetX, final int targetY, final int originalX, final int originalY) { final BigDecimal xScale = new BigDecimal(targetX).divide(new BigDecimal(originalX), 10, BigDecimal.ROUND_HALF_UP); final BigDecimal yScale = new BigDecimal(targetY).divide(new BigDecimal(originalY), 10, BigDecimal.ROUND_HALF_UP); final int scaleWidth, scaleHeight, padX, padY; if (xScale.compareTo(yScale) < 0) { scaleWidth = targetX; scaleHeight = new BigDecimal(originalY).multiply(xScale).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); padX = 0; padY = (targetY - scaleHeight) / 2; } else { scaleHeight = targetY; scaleWidth = new BigDecimal(originalX).multiply(yScale).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); padX = (targetX - scaleWidth) / 2; padY = 0; } return new int[] { padX, padY, scaleWidth, scaleHeight }; } 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); }
ImageServiceImpl extends BaseGenericServiceImpl<SeoImage> implements ImageService { int[] scaleImageToCenter(final int targetX, final int targetY, final int originalX, final int originalY) { final BigDecimal xScale = new BigDecimal(targetX).divide(new BigDecimal(originalX), 10, BigDecimal.ROUND_HALF_UP); final BigDecimal yScale = new BigDecimal(targetY).divide(new BigDecimal(originalY), 10, BigDecimal.ROUND_HALF_UP); final int scaleWidth, scaleHeight, padX, padY; if (xScale.compareTo(yScale) < 0) { scaleWidth = targetX; scaleHeight = new BigDecimal(originalY).multiply(xScale).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); padX = 0; padY = (targetY - scaleHeight) / 2; } else { scaleHeight = targetY; scaleWidth = new BigDecimal(originalX).multiply(yScale).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); padX = (targetX - scaleWidth) / 2; padY = 0; } return new int[] { padX, padY, scaleWidth, scaleHeight }; } 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(); }
ImageServiceImpl extends BaseGenericServiceImpl<SeoImage> implements ImageService { int[] scaleImageToCenter(final int targetX, final int targetY, final int originalX, final int originalY) { final BigDecimal xScale = new BigDecimal(targetX).divide(new BigDecimal(originalX), 10, BigDecimal.ROUND_HALF_UP); final BigDecimal yScale = new BigDecimal(targetY).divide(new BigDecimal(originalY), 10, BigDecimal.ROUND_HALF_UP); final int scaleWidth, scaleHeight, padX, padY; if (xScale.compareTo(yScale) < 0) { scaleWidth = targetX; scaleHeight = new BigDecimal(originalY).multiply(xScale).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); padX = 0; padY = (targetY - scaleHeight) / 2; } else { scaleHeight = targetY; scaleWidth = new BigDecimal(originalX).multiply(yScale).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); padX = (targetX - scaleWidth) / 2; padY = 0; } return new int[] { padX, padY, scaleWidth, scaleHeight }; } 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();  }