Commit 060fb5f4 authored by Tim Coates (CPVV)'s avatar Tim Coates (CPVV)

Remove alpha channel for jpeg and jfif formats

parent 405c43df
......@@ -9,12 +9,12 @@ import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
......@@ -26,13 +26,14 @@ import java.util.stream.Stream;
public class AvatarService {
private final HashMap<String, Path> fileList;
private final BufferedImage defaultImage;
private static final String DEFAULT_FORMAT = "jpg";
private static final Path DEFAULT_IMAGE_PATH = new File(AvatarService.class.getResource("/default.jpg").getFile()).toPath();
public AvatarService(AvatarServiceProperites properites) throws IOException {
Stream<Path> avatars = Files.walk(properites.getDirectory());
this.fileList = new HashMap<>();
Stream<Path> avatars = Files.walk(properites.getDirectory());
avatars.filter(this::correctFormat).forEach(path -> this.fileList.put(getFileHash(path), path));
this.defaultImage = ImageIO.read(getClass().getResourceAsStream("/default.jpg"));
}
public ResponseEntity<byte[]> getAvatar(String request, String size) throws IOException {
......@@ -42,8 +43,8 @@ public class AvatarService {
BufferedImage image = (fileList.containsKey(hash)) ? readImageToSize(fileList.get(hash), Integer.parseInt(size)) : generateIdenticons(hash, Integer.parseInt(size));
return ResponseEntity.ok().contentType(format).body(writeToFormat(image, format.getSubtype()));
} catch (Exception ignored) {
BufferedImage defaultImage = readImageToSize(DEFAULT_IMAGE_PATH, Integer.parseInt(size));
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(writeToFormat(defaultImage, DEFAULT_FORMAT));
BufferedImage resizedDefaultImage = resizeImage(defaultImage, Integer.parseInt(size));
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(writeToFormat(resizedDefaultImage, DEFAULT_FORMAT));
}
}
......@@ -81,6 +82,10 @@ public class AvatarService {
private BufferedImage readImageToSize(Path imagePath, int size) throws IOException {
BufferedImage inputImage = ImageIO.read(imagePath.toFile());
return resizeImage(inputImage, size);
}
private BufferedImage resizeImage(BufferedImage inputImage, int size) {
int newHeight = inputImage.getHeight() / inputImage.getWidth() * size;
ResampleOp resizeOp = new ResampleOp(size, newHeight);
resizeOp.setFilter(ResampleFilters.getLanczos3Filter());
......@@ -89,6 +94,12 @@ public class AvatarService {
private byte[] writeToFormat(BufferedImage image, String format) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (List.of("jpg", "jpeg", "jfif").contains(format)) {
// Remove alpha channel
BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
result.createGraphics().drawImage(image, 0, 0, Color.BLACK, null);
image = result;
}
ImageIO.write(image, format, baos);
return baos.toByteArray();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment