package cc.fascinated.common; import jakarta.validation.constraints.NotNull; import lombok.SneakyThrows; import lombok.extern.log4j.Log4j2; import javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; @Log4j2 public class ImageUtils { /** * Scale the given image to the provided size. * * @param image the image to scale * @param size the size to scale the image to * @return the scaled image */ public static BufferedImage resize(BufferedImage image, double size) { BufferedImage scaled = new BufferedImage((int) (image.getWidth() * size), (int) (image.getHeight() * size), BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = scaled.createGraphics(); graphics.drawImage(image, AffineTransform.getScaleInstance(size, size), null); graphics.dispose(); return scaled; } /** * Flip the given image. * * @param image the image to flip * @return the flipped image */ public static BufferedImage flip(@NotNull final BufferedImage image) { BufferedImage flipped = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = flipped.createGraphics(); graphics.drawImage(image, image.getWidth(), 0, 0, image.getHeight(), 0, 0, image.getWidth(), image.getHeight(), null); graphics.dispose(); return flipped; } /** * Convert an image to bytes. * * @param image the image to convert * @return the image as bytes */ @SneakyThrows public static byte[] imageToBytes(BufferedImage image) { try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { ImageIO.write(image, "png", outputStream); return outputStream.toByteArray(); } catch (Exception e) { throw new Exception("Failed to convert image to bytes", e); } } }