aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick
diff options
context:
space:
mode:
authorGravatar KillyMXI2017-06-20 01:50:33 +0300
committerGravatar KillyMXI2017-06-20 01:50:33 +0300
commit9943851e037f907623cd879dab2f0e8b31f260cf (patch)
treead479d52100caeed458bd3d626d8432d2eb2e31e /juick-server/src/main/java/com/juick
parentf3312679fa538122cd81e0e9a0a251ebb296b80e (diff)
All the image processing is moved to ImageUtils class.
Small images keep their size and do not stretch to 512/1024px.
Diffstat (limited to 'juick-server/src/main/java/com/juick')
-rw-r--r--juick-server/src/main/java/com/juick/server/util/ImageUtils.java66
1 files changed, 66 insertions, 0 deletions
diff --git a/juick-server/src/main/java/com/juick/server/util/ImageUtils.java b/juick-server/src/main/java/com/juick/server/util/ImageUtils.java
new file mode 100644
index 00000000..61677750
--- /dev/null
+++ b/juick-server/src/main/java/com/juick/server/util/ImageUtils.java
@@ -0,0 +1,66 @@
+
+package com.juick.server.util;
+
+import org.apache.commons.io.FilenameUtils;
+import org.imgscalr.Scalr;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+
+public class ImageUtils {
+
+ /**
+ * Move attached image from temp folder to image folder.
+ * Create preview images in corresponding folders.
+ *
+ * @param tempFilename Name of the image file in temp folder.
+ * @param outputFilename Name that will be used in in image folder.
+ * @param tmpDir Path string for the temp folder.
+ * @param imgDir Path string for the image folder.
+ */
+ public static void saveImageWithPreviews(String tempFilename, String outputFilename, String tmpDir, String imgDir)
+ throws IOException {
+ String ext = FilenameUtils.getExtension(outputFilename);
+
+ Path outputImagePath = Paths.get(imgDir, "p", outputFilename);
+ Files.move(Paths.get(tmpDir, tempFilename), outputImagePath);
+ BufferedImage originalImage = ImageIO.read(outputImagePath.toFile());
+
+ int width = originalImage.getWidth();
+ int height = originalImage.getHeight();
+ int maxDimension = (width > height) ? width : height;
+ BufferedImage image1024 = (maxDimension > 1024) ? Scalr.resize(originalImage, 1024) : originalImage;
+ BufferedImage image0512 = (maxDimension > 512) ? Scalr.resize(originalImage, 512) : originalImage;
+ BufferedImage image0160 = (maxDimension > 160) ? Scalr.resize(originalImage, 160) : originalImage;
+ ImageIO.write(image1024, ext, Paths.get(imgDir, "photos-1024", outputFilename).toFile());
+ ImageIO.write(image0512, ext, Paths.get(imgDir, "photos-512", outputFilename).toFile());
+ ImageIO.write(image0160, ext, Paths.get(imgDir, "ps", outputFilename).toFile());
+ }
+
+ /**
+ * Save new avatar in all required sizes.
+ *
+ * @param tempFilename Name of the image file in temp folder.
+ * @param uid User id that is used to build image file names.
+ * @param tmpDir Path string for the temp folder.
+ * @param imgDir Path string for the image folder.
+ */
+ public static void saveAvatar(String tempFilename, int uid, String tmpDir, String imgDir)
+ throws IOException {
+ String ext = FilenameUtils.getExtension(tempFilename);
+ String originalName = String.format("%s.%s", uid, ext);
+ Path originalPath = Paths.get(imgDir, "ao", originalName);
+ Files.move(Paths.get(tmpDir, tempFilename), originalPath, StandardCopyOption.REPLACE_EXISTING);
+ BufferedImage originalImage = ImageIO.read(originalPath.toFile());
+
+ String targetExt = "png";
+ String targetName = String.format("%s.%s", uid, targetExt);
+ ImageIO.write(Scalr.resize(originalImage, 96), targetExt, Paths.get(imgDir, "a", targetName).toFile());
+ ImageIO.write(Scalr.resize(originalImage, 32), targetExt, Paths.get(imgDir, "as", targetName).toFile());
+ }
+} \ No newline at end of file