From 80dff3eeb32b197f4a356ebccd5ef1f4eaaefcde Mon Sep 17 00:00:00 2001 From: Steven Tracey Date: Thu, 22 Aug 2024 22:55:07 -0400 Subject: [PATCH] Added background color option, error handling --- .../nevets/igformatter/GenerateRoute.java | 28 +++++++-- .../java/tech/nevets/igformatter/Images.java | 62 ++++++++++--------- 2 files changed, 56 insertions(+), 34 deletions(-) diff --git a/src/main/java/tech/nevets/igformatter/GenerateRoute.java b/src/main/java/tech/nevets/igformatter/GenerateRoute.java index 02e39a7..fb00edc 100644 --- a/src/main/java/tech/nevets/igformatter/GenerateRoute.java +++ b/src/main/java/tech/nevets/igformatter/GenerateRoute.java @@ -12,14 +12,30 @@ import java.io.InputStream; public class GenerateRoute implements Route { @Override - public Object handle(Request request, Response response) throws Exception { - request.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("./temp")); - try (InputStream is = request.raw().getPart("image").getInputStream()) { - response.type("image/png"); + public Object handle(Request req, Response res) throws Exception { + req.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("./temp")); + try (InputStream is = req.raw().getPart("image").getInputStream()) { BufferedImage img = ImageIO.read(is); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ImageIO.write(Images.placeImageOnTop(img), "png", baos); + + int color; + boolean customDimensions; + int width; + int height; + try { + color = Integer.parseInt(req.queryParams("color")); + customDimensions = Boolean.parseBoolean(req.queryParams("customDimensions")); + width = Integer.parseInt(req.queryParams("width")); + height = Integer.parseInt(req.queryParams("height")); + } catch (final Exception e) { + res.type("plain/text"); + res.status(400); + return "Error in sent parameters: " + e.getMessage(); + } + + ImageIO.write(Images.renderImage(img, color, customDimensions, width, height), "png", baos); + + res.type("image/png"); return baos.toByteArray(); } } diff --git a/src/main/java/tech/nevets/igformatter/Images.java b/src/main/java/tech/nevets/igformatter/Images.java index f6cf486..a6d323d 100644 --- a/src/main/java/tech/nevets/igformatter/Images.java +++ b/src/main/java/tech/nevets/igformatter/Images.java @@ -4,38 +4,44 @@ import java.awt.*; import java.awt.image.BufferedImage; public class Images { - private static final BufferedImage BACKGROUND = blankWhiteBackground(); + public static BufferedImage renderImage(BufferedImage img, int color, boolean customDimensions, int customWidth, int customHeight) { + BufferedImage image = generateBackground(color); + Graphics2D g = image.createGraphics(); - public static BufferedImage placeImageOnTop(BufferedImage fgImage) { - BufferedImage image = BACKGROUND; - Graphics2D imageG2D = image.createGraphics(); - - int width = fgImage.getWidth(); - int height = fgImage.getHeight(); - int newWidth; - int newHeight; + int width; + int height; int xPos; int yPos; - if (width > height) { - newWidth = 1048; - newHeight = (newWidth * height) / width; - xPos = 16; - yPos = (1080 - newHeight) / 2; - } else if (height > width) { - newHeight = 1048; - newWidth = (width * newHeight) / height; - xPos = (1080 - newWidth) / 2; - yPos = 16; + if (customDimensions) { + width = customWidth; + height = customHeight; + xPos = 0; + yPos = 0; } else { - newWidth = 1048; - newHeight = 1048; - xPos = 16; - yPos = 16; + int oldWidth = img.getWidth(); + int oldHeight = img.getHeight(); + + if (oldWidth > oldHeight) { + width = 1048; + height = (width * oldHeight) / oldWidth; + xPos = 16; + yPos = (1080 - height) / 2; + } else if (oldHeight > oldWidth) { + height = 1048; + width = (oldWidth * height) / oldHeight; + xPos = (1080 - width) / 2; + yPos = 16; + } else { + width = 1048; + height = 1048; + xPos = 16; + yPos = 16; + } } - imageG2D.drawImage(resizeImage(fgImage, newWidth, newHeight), xPos, yPos, newWidth, newHeight, null); - imageG2D.dispose(); + g.drawImage(resizeImage(img, width, height), xPos, yPos, width, height, null); + g.dispose(); return image; } @@ -50,11 +56,11 @@ public class Images { return resizedImage; } - public static BufferedImage blankWhiteBackground() { + public static BufferedImage generateBackground(int color) { BufferedImage image = new BufferedImage(1080, 1080, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = image.createGraphics(); - g2d.setColor(Color.WHITE); - g2d.setPaint(Color.WHITE); + g2d.setColor(new Color(color)); + g2d.setPaint(new Color(color)); g2d.fillRect(0, 0, 1080, 1080); g2d.dispose(); return image;