Skip to content

Commit 7daf4bd

Browse files
committed
Yay rescaling
1 parent 7515197 commit 7daf4bd

File tree

2 files changed

+70
-13
lines changed

2 files changed

+70
-13
lines changed

src/main/java/simplexity/entityicons/ImageController.java

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
import org.springframework.web.bind.annotation.GetMapping;
1313
import org.springframework.web.bind.annotation.PathVariable;
1414
import org.springframework.web.bind.annotation.RequestMapping;
15+
import org.springframework.web.bind.annotation.RequestParam;
1516
import org.springframework.web.bind.annotation.RestController;
1617

18+
import javax.imageio.ImageIO;
19+
import java.awt.image.BufferedImage;
20+
import java.io.ByteArrayOutputStream;
1721
import java.io.IOException;
1822
import java.io.InputStream;
1923

24+
2025
@RestController
2126
@RequestMapping("/api/v1/textures")
2227
public class ImageController {
@@ -31,25 +36,48 @@ public ImageController(ResourceLoader resourceLoader) {
3136
}
3237

3338
@GetMapping(value = "/{category}/{filename}/{size}", produces = MediaType.IMAGE_PNG_VALUE)
34-
public ResponseEntity<byte[]> getImage(@PathVariable String category, @PathVariable String filename, @PathVariable int size) {
39+
public ResponseEntity<byte[]> getImage(
40+
@PathVariable String category,
41+
@PathVariable String filename,
42+
@PathVariable int size,
43+
@RequestParam(required = false, defaultValue = "1") int scale) {
3544
String filePath = "file:" + assetsBasePath + "/png_files/" + category + "/" + size + "x" + size + "/" + filename + ".png";
3645
Resource resource = resourceLoader.getResource(filePath);
37-
if (resource.exists()) {
38-
try (InputStream inputStream = resource.getInputStream()) {
39-
byte[] imageBytes = StreamUtils.copyToByteArray(inputStream);
40-
String downloadName = filename + "_" + size + "x" + size;
41-
HttpHeaders headers = new HttpHeaders();
42-
headers.add(HttpHeaders.CONTENT_TYPE, "image/png");
43-
headers.add(HttpHeaders.CONTENT_DISPOSITION, String.format("inline; filename=%s", downloadName));
44-
45-
return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK);
46+
if (!resource.exists()) {
47+
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
48+
49+
}
50+
byte[] imageBytes;
51+
try (InputStream inputStream = resource.getInputStream()) {
52+
imageBytes = StreamUtils.copyToByteArray(inputStream);
53+
} catch (IOException e) {
54+
e.printStackTrace();
55+
return ResponseEntity.status(HttpStatus.ALREADY_REPORTED).build();
56+
}
57+
String downloadName = filename + "_" + size + "x" + size;
58+
if (scale > 1) {
59+
BufferedImage rescaledImage = Rescaler.rescaleImage(filePath, scale, size);
60+
if (rescaledImage == null) {
61+
return ResponseEntity.status(HttpStatus.CONFLICT).build();
62+
}
63+
ByteArrayOutputStream imageByteArrayOutputStream = new ByteArrayOutputStream();
64+
try {
65+
ImageIO.write(rescaledImage, "png", imageByteArrayOutputStream);
4666
} catch (IOException e) {
4767
e.printStackTrace();
48-
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
68+
return ResponseEntity.status(HttpStatus.FAILED_DEPENDENCY).build();
4969
}
50-
} else {
51-
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
70+
imageBytes = imageByteArrayOutputStream.toByteArray();
71+
downloadName = filename + "_" + (size * scale) + "x" + (size * scale);
72+
5273
}
74+
75+
HttpHeaders headers = new HttpHeaders();
76+
headers.add(HttpHeaders.CONTENT_TYPE, "image/png");
77+
headers.add(HttpHeaders.CONTENT_DISPOSITION, String.format("inline; filename=%s", downloadName));
78+
return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK);
79+
80+
5381
}
5482

5583
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package simplexity.entityicons;
2+
3+
import javax.imageio.ImageIO;
4+
import java.awt.Graphics2D;
5+
import java.awt.RenderingHints;
6+
import java.awt.image.BufferedImage;
7+
import java.io.File;
8+
import java.io.IOException;
9+
10+
public class Rescaler {
11+
12+
public static BufferedImage rescaleImage(String path, int multiplier, int originalSize) {
13+
BufferedImage originalImage;
14+
try {
15+
path = path.substring(5);
16+
File file = new File(path);
17+
originalImage = ImageIO.read(file);
18+
} catch (IOException e) {
19+
return null;
20+
}
21+
int size = multiplier * originalSize;
22+
BufferedImage resizedImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
23+
Graphics2D graphicsImage = resizedImage.createGraphics();
24+
graphicsImage.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
25+
graphicsImage.drawImage(originalImage, 0,0, size, size,null);
26+
graphicsImage.dispose();
27+
return resizedImage;
28+
}
29+
}

0 commit comments

Comments
 (0)