Fixed caching issue, added extra parameters

This commit is contained in:
Steven Tracey 2024-08-23 08:04:20 -04:00
parent 80dff3eeb3
commit f40dde4c55
2 changed files with 56 additions and 37 deletions

View File

@ -6,6 +6,7 @@ import spark.Route;
import javax.imageio.ImageIO;
import javax.servlet.MultipartConfigElement;
import javax.servlet.http.Part;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
@ -14,29 +15,46 @@ public class GenerateRoute implements Route {
@Override
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()) {
Part part = req.raw().getPart("image");
if (part == null) {
res.status(400);
res.type("text/plain");
return "No image uploaded";
}
try (InputStream is = part.getInputStream()) {
BufferedImage img = ImageIO.read(is);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int color;
boolean customDimensions;
String color;
int width;
int height;
int minBorder;
try {
color = Integer.parseInt(req.queryParams("color"));
customDimensions = Boolean.parseBoolean(req.queryParams("customDimensions"));
color = req.queryParams("color");
width = Integer.parseInt(req.queryParams("width"));
height = Integer.parseInt(req.queryParams("height"));
minBorder = Integer.parseInt(req.queryParams("minBorder"));
} 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);
BufferedImage finalImg = Images.renderImage(img, color, width, height, minBorder);
if (finalImg == null) {
res.type("text/plain");
res.status(400);
return "Dimensions too large, max 8000x6000";
}
ImageIO.write(finalImg, "png", baos);
res.type("image/png");
return baos.toByteArray();
} catch (Exception e) {
res.type("plain/text");
res.status(500);
return "Error creating image: " + e.getMessage();
}
}
}

View File

@ -4,8 +4,13 @@ import java.awt.*;
import java.awt.image.BufferedImage;
public class Images {
public static BufferedImage renderImage(BufferedImage img, int color, boolean customDimensions, int customWidth, int customHeight) {
BufferedImage image = generateBackground(color);
public static BufferedImage renderImage(BufferedImage img, String color, int totalWidth, int totalHeight, int minBorder) {
if (totalWidth > 8000 || totalHeight > 6000) {
System.out.println("Input too large");
return null;
}
BufferedImage image = generateBackground(color, totalWidth, totalHeight);
Graphics2D g = image.createGraphics();
int width;
@ -13,31 +18,23 @@ public class Images {
int xPos;
int yPos;
if (customDimensions) {
width = customWidth;
height = customHeight;
xPos = 0;
yPos = 0;
int oldWidth = img.getWidth();
int oldHeight = img.getHeight();
if (oldWidth > oldHeight) {
width = (totalWidth - (minBorder * 2));
height = (width * oldHeight) / oldWidth;
xPos = minBorder;
yPos = (totalHeight - height) / 2;
} else if (oldHeight > oldWidth) {
height = (totalWidth - (minBorder * 2));
width = (oldWidth * height) / oldHeight;
xPos = (totalWidth - width) / 2;
yPos = minBorder;
} else {
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;
}
width = (totalWidth - (minBorder * 2));
height = (totalHeight - (minBorder * 2));
xPos = minBorder;
yPos = minBorder;
}
g.drawImage(resizeImage(img, width, height), xPos, yPos, width, height, null);
@ -56,12 +53,16 @@ public class Images {
return resizedImage;
}
public static BufferedImage generateBackground(int color) {
BufferedImage image = new BufferedImage(1080, 1080, BufferedImage.TYPE_INT_ARGB);
private static int hexToInt(String hex) {
return Integer.parseInt(hex, 16);
}
public static BufferedImage generateBackground(String color, int width, int height) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = image.createGraphics();
g2d.setColor(new Color(color));
g2d.setPaint(new Color(color));
g2d.fillRect(0, 0, 1080, 1080);
g2d.setColor(new Color(hexToInt(color.substring(1))));
g2d.setPaint(new Color(hexToInt(color.substring(1))));
g2d.fillRect(0, 0, width, height);
g2d.dispose();
return image;
}