From 9943851e037f907623cd879dab2f0e8b31f260cf Mon Sep 17 00:00:00 2001 From: KillyMXI Date: Tue, 20 Jun 2017 01:50:33 +0300 Subject: All the image processing is moved to ImageUtils class. Small images keep their size and do not stretch to 512/1024px. --- juick-www/build.gradle | 1 - .../java/com/juick/www/controllers/NewMessage.java | 35 +++------------------- .../java/com/juick/www/controllers/Settings.java | 20 ++----------- 3 files changed, 6 insertions(+), 50 deletions(-) (limited to 'juick-www') diff --git a/juick-www/build.gradle b/juick-www/build.gradle index ab0480f4..55a15a83 100644 --- a/juick-www/build.gradle +++ b/juick-www/build.gradle @@ -20,7 +20,6 @@ apply plugin: 'org.akhikhl.gretty' dependencies { compile project(':juick-server') compile 'com.github.scribejava:scribejava-apis:4.1.1' - compile 'org.imgscalr:imgscalr-lib:4.2' compile 'com.github.ooxi:serialized-php-parser:0.5.0' compile 'com.sun.mail:javax.mail:1.5.6' compile "org.springframework:spring-webmvc:${rootProject.springFrameworkVersion}" diff --git a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java index 1931b1d9..4dcf6c10 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java @@ -24,15 +24,14 @@ import com.juick.server.util.HttpBadRequestException; import com.juick.server.util.HttpForbiddenException; import com.juick.server.util.HttpNotFoundException; import com.juick.server.util.HttpUtils; +import com.juick.server.util.ImageUtils; import com.juick.service.*; import com.juick.util.UserUtils; import com.juick.www.Utils; import com.juick.www.WebApp; -import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.CharEncoding; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; -import org.imgscalr.Scalr; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.env.Environment; @@ -47,11 +46,9 @@ import rocks.xmpp.core.stanza.model.Message; import rocks.xmpp.extensions.nick.model.Nickname; import rocks.xmpp.extensions.oob.model.x.OobX; -import javax.imageio.ImageIO; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.awt.image.BufferedImage; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; @@ -59,9 +56,6 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.net.URLEncoder; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.List; /** @@ -226,18 +220,7 @@ public class NewMessage { String fname = mid + "." + attachmentType; String attachmentURL = "http://i.juick.com/photos-1024/" + fname; - Path origName = Paths.get(webApp.getImgDir(), "p", fname); - Files.move(Paths.get(webApp.getTmpDir(), attachmentFName), origName); - BufferedImage originalImage = ImageIO.read(origName.toFile()); - ImageIO.write(Scalr.resize(originalImage, 1024), - FilenameUtils.getExtension(origName.toString()), - Paths.get(webApp.getImgDir(), "photos-1024", fname).toFile()); - ImageIO.write(Scalr.resize(originalImage, 512), - FilenameUtils.getExtension(origName.toString()), - Paths.get(webApp.getImgDir(), "photos-512", fname).toFile()); - ImageIO.write(Scalr.resize(originalImage, 160), - FilenameUtils.getExtension(origName.toString()), - Paths.get(webApp.getImgDir(), "ps", fname).toFile()); + ImageUtils.saveImageWithPreviews(attachmentFName, fname, webApp.getTmpDir(), webApp.getImgDir()); body = attachmentURL + "\n" + body; try { @@ -385,18 +368,8 @@ public class NewMessage { String fname = mid + "-" + ridnew + "." + attachmentType; String attachmentURL = "http://i.juick.com/photos-1024/" + fname; - Path origName = Paths.get(webApp.getImgDir(), "p", fname); - Files.move(Paths.get(webApp.getTmpDir(), attachmentFName), origName); - BufferedImage originalImage = ImageIO.read(origName.toFile()); - ImageIO.write(Scalr.resize(originalImage, 1024), - FilenameUtils.getExtension(origName.toString()), - Paths.get(webApp.getImgDir(), "photos-1024", fname).toFile()); - ImageIO.write(Scalr.resize(originalImage, 512), - FilenameUtils.getExtension(origName.toString()), - Paths.get(webApp.getImgDir(), "photos-512", fname).toFile()); - ImageIO.write(Scalr.resize(originalImage, 160), - FilenameUtils.getExtension(origName.toString()), - Paths.get(webApp.getImgDir(), "ps", fname).toFile()); + ImageUtils.saveImageWithPreviews(attachmentFName, fname, webApp.getTmpDir(), webApp.getImgDir()); + body = attachmentURL + "\n" + body; try { xmsg.addExtension(new OobX(new URI(attachmentURL))); diff --git a/juick-www/src/main/java/com/juick/www/controllers/Settings.java b/juick-www/src/main/java/com/juick/www/controllers/Settings.java index 3c4511c1..d2b65661 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/Settings.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Settings.java @@ -21,12 +21,11 @@ import com.juick.server.helpers.NotifyOpts; import com.juick.server.helpers.UserInfo; import com.juick.server.util.HttpBadRequestException; import com.juick.server.util.HttpUtils; +import com.juick.server.util.ImageUtils; import com.juick.service.*; import com.juick.util.UserUtils; import com.juick.www.WebApp; -import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; -import org.imgscalr.Scalr; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; @@ -36,7 +35,6 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; -import javax.imageio.ImageIO; import javax.inject.Inject; import javax.mail.Message; import javax.mail.MessagingException; @@ -48,12 +46,7 @@ import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -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; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -167,16 +160,7 @@ public class Settings { info.setDescription(request.getParameter("descr")); String avatarTmpPath = HttpUtils.receiveMultiPartFile(avatar, webApp.getTmpDir()); if (StringUtils.isNotEmpty(avatarTmpPath)) { - String originalExtension = FilenameUtils.getExtension(avatarTmpPath); - String originalName = String.format("%s.%s", visitor.getUid(), originalExtension); - String targetName = String.format("%s.png", visitor.getUid()); - Path ao = Paths.get(webApp.getImgDir(), "ao", originalName); - Path a = Paths.get(webApp.getImgDir(), "a", targetName); - Path as = Paths.get(webApp.getImgDir(), "as", targetName); - Files.move(Paths.get(webApp.getTmpDir(), avatarTmpPath), ao, StandardCopyOption.REPLACE_EXISTING); - BufferedImage originalImage = ImageIO.read(ao.toFile()); - ImageIO.write(Scalr.resize(originalImage, 96), "png", a.toFile()); - ImageIO.write(Scalr.resize(originalImage, 32), "png", as.toFile()); + ImageUtils.saveAvatar(avatarTmpPath, visitor.getUid(), webApp.getTmpDir(), webApp.getImgDir()); } if (userService.updateUserInfo(visitor, info)) { result = String.format("

Your info is updated.

Back to blog.

", visitor.getName()); -- cgit v1.2.3