Skip to content

Commit

Permalink
Yay rescaling
Browse files Browse the repository at this point in the history
  • Loading branch information
RhythmicSys committed Jul 19, 2024
1 parent 7515197 commit 7daf4bd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
54 changes: 41 additions & 13 deletions src/main/java/simplexity/entityicons/ImageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;


@RestController
@RequestMapping("/api/v1/textures")
public class ImageController {
Expand All @@ -31,25 +36,48 @@ public ImageController(ResourceLoader resourceLoader) {
}

@GetMapping(value = "/{category}/{filename}/{size}", produces = MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<byte[]> getImage(@PathVariable String category, @PathVariable String filename, @PathVariable int size) {
public ResponseEntity<byte[]> getImage(
@PathVariable String category,
@PathVariable String filename,
@PathVariable int size,
@RequestParam(required = false, defaultValue = "1") int scale) {
String filePath = "file:" + assetsBasePath + "/png_files/" + category + "/" + size + "x" + size + "/" + filename + ".png";
Resource resource = resourceLoader.getResource(filePath);
if (resource.exists()) {
try (InputStream inputStream = resource.getInputStream()) {
byte[] imageBytes = StreamUtils.copyToByteArray(inputStream);
String downloadName = filename + "_" + size + "x" + size;
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, "image/png");
headers.add(HttpHeaders.CONTENT_DISPOSITION, String.format("inline; filename=%s", downloadName));

return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK);
if (!resource.exists()) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();

}
byte[] imageBytes;
try (InputStream inputStream = resource.getInputStream()) {
imageBytes = StreamUtils.copyToByteArray(inputStream);
} catch (IOException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.ALREADY_REPORTED).build();
}
String downloadName = filename + "_" + size + "x" + size;
if (scale > 1) {
BufferedImage rescaledImage = Rescaler.rescaleImage(filePath, scale, size);
if (rescaledImage == null) {
return ResponseEntity.status(HttpStatus.CONFLICT).build();
}
ByteArrayOutputStream imageByteArrayOutputStream = new ByteArrayOutputStream();
try {
ImageIO.write(rescaledImage, "png", imageByteArrayOutputStream);
} catch (IOException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
return ResponseEntity.status(HttpStatus.FAILED_DEPENDENCY).build();
}
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
imageBytes = imageByteArrayOutputStream.toByteArray();
downloadName = filename + "_" + (size * scale) + "x" + (size * scale);

}

HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, "image/png");
headers.add(HttpHeaders.CONTENT_DISPOSITION, String.format("inline; filename=%s", downloadName));
return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK);


}

}
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/simplexity/entityicons/Rescaler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package simplexity.entityicons;

import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Rescaler {

public static BufferedImage rescaleImage(String path, int multiplier, int originalSize) {
BufferedImage originalImage;
try {
path = path.substring(5);
File file = new File(path);
originalImage = ImageIO.read(file);
} catch (IOException e) {
return null;
}
int size = multiplier * originalSize;
BufferedImage resizedImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphicsImage = resizedImage.createGraphics();
graphicsImage.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
graphicsImage.drawImage(originalImage, 0,0, size, size,null);
graphicsImage.dispose();
return resizedImage;
}
}

0 comments on commit 7daf4bd

Please sign in to comment.