Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageManipulationAPI methods now create a new image instead of modifying the existing one #676

Merged
merged 12 commits into from
Mar 20, 2023
Merged
33 changes: 16 additions & 17 deletions java/src/main/java/com/genexus/GxImageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,69 +87,68 @@ public static int getImageWidth(String imageFile) {
public static String crop(String imageFile, int x, int y, int width, int height) {
if (!isValidInput(imageFile))
return "";

try {
BufferedImage image = createBufferedImageFromURI(imageFile);
BufferedImage croppedImage = image.getSubimage(x, y, width, height);
writeImage(croppedImage, imageFile);
return writeImage(croppedImage, imageFile);
}
catch (Exception e) {
log.error("crop " + imageFile + " failed" , e);
}
return imageFile;
return "";
}

private static void writeImage(BufferedImage croppedImage, String destinationFilePathOrUrl) throws IOException {
private static String writeImage(BufferedImage croppedImage, String destinationFilePathOrUrl) throws IOException {
String newFileName = PrivateUtilities.getTempFileName(CommonUtil.getFileType(destinationFilePathOrUrl));
try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
ImageIO.write(croppedImage, CommonUtil.getFileType(destinationFilePathOrUrl), outStream);
ImageIO.write(croppedImage, CommonUtil.getFileType(newFileName), outStream);
try (ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray())) {
GXFile file = getGXFile(destinationFilePathOrUrl);
String filePath = Preferences.getDefaultPreferences().getPRIVATE_PATH() + newFileName;
GXFile file = getGXFile(filePath);
file.create(inStream, true);
file.close();
return filePath;
}
}
}

public static String flipHorizontally(String imageFile) {
if (!isValidInput(imageFile))
return "";

try {
BufferedImage image = createBufferedImageFromURI(imageFile);
AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
tx.translate(-image.getWidth(null), 0);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage flippedImage = op.filter(image, null);
writeImage(flippedImage, imageFile);
return writeImage(flippedImage, imageFile);
}
catch (Exception e) {
log.error("flip horizontal " + imageFile + " failed" , e);
}
return imageFile;
return "";
}

public static String flipVertically(String imageFile) {
if (!isValidInput(imageFile))
return "";

try {
BufferedImage image = createBufferedImageFromURI(imageFile);
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -image.getHeight(null));
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage flippedImage = op.filter(image, null);
writeImage(flippedImage, imageFile);
return writeImage(flippedImage, imageFile);
}
catch (Exception e) {
log.error("flip vertical " + imageFile + " failed" , e);
}
return imageFile;
return "";
}

public static String resize(String imageFile, int width, int height, boolean keepAspectRatio) {
if (!isValidInput(imageFile))
return "";

try {
BufferedImage image = createBufferedImageFromURI(imageFile);
if (keepAspectRatio) {
Expand All @@ -166,12 +165,12 @@ public static String resize(String imageFile, int width, int height, boolean kee
Graphics2D g2d = resizedImage.createGraphics();
g2d.drawImage(image, 0, 0, width, height, null);
g2d.dispose();
writeImage(resizedImage, imageFile);
return writeImage(resizedImage, imageFile);
}
catch (Exception e) {
log.error("resize " + imageFile + " failed" , e);
}
return imageFile;
return "";
}

public static String scale(String imageFile, short percent) {
Expand All @@ -194,12 +193,12 @@ public static String rotate(String imageFile, short angle) {
try {
BufferedImage image = createBufferedImageFromURI(imageFile);
BufferedImage rotatedImage = rotateImage(image, angle);
writeImage(rotatedImage, imageFile);
return writeImage(rotatedImage, imageFile);
}
catch (Exception e) {
log.error("rotate " + imageFile + " failed" , e);
}
return imageFile;
return "";
}

private static BufferedImage rotateImage(BufferedImage buffImage, double angle) {
Expand Down