11package app .softnetwork .utils
22
3+ import com .typesafe .scalalogging .StrictLogging
4+
35import java .awt .Color
46import java .awt .image .BufferedImage
57import java .io .ByteArrayOutputStream
@@ -13,7 +15,7 @@ import scala.util.{Failure, Success, Try}
1315
1416/** Created by smanciot on 06/07/2018.
1517 */
16- object ImageTools {
18+ object ImageTools extends StrictLogging {
1719
1820 import MimeTypeTools ._
1921
@@ -62,7 +64,7 @@ object ImageTools {
6264
6365 def generateImages (
6466 originalImage : Path ,
65- replace : Boolean = true ,
67+ replace : Boolean = false ,
6668 imageSizes : Seq [ImageSize ] = Seq (Icon , Small )
6769 ): Boolean = {
6870 val mimeType = detectMimeType(originalImage)
@@ -96,17 +98,22 @@ object ImageTools {
9698 def getImage (
9799 originalPath : Path ,
98100 size : Option [ImageSize ] = None ,
99- replace : Boolean = true
101+ replace : Boolean = false
100102 ): Path = {
101103 size match {
102104 case Some (s) =>
103105 val format = toFormat(originalPath).getOrElse(" jpeg" )
104106 val out = s.resizedPath(originalPath, Option (format))
105107 if (! Files .exists(out)) {
106- val src : BufferedImage = ImageIO .read(Files .newInputStream(originalPath))
107- val originalWidth = src.getWidth
108- val originalHeight = src.getHeight
109- resizeImage(src, originalWidth, originalHeight, originalPath, format, s, replace)
108+ Try (ImageIO .read(Files .newInputStream(originalPath))) match {
109+ case Success (src) =>
110+ resizeImage(src, src.getWidth, src.getHeight, originalPath, format, s, replace)
111+ case Failure (f) =>
112+ s """ an error occurred while trying to resize image $originalPath to
113+ | ${s.width}x ${s.height} :
114+ | ${f.getMessage}""" .stripMargin
115+ originalPath
116+ }
110117 } else {
111118 out
112119 }
@@ -124,7 +131,7 @@ object ImageTools {
124131 replace : Boolean
125132 ): Path = {
126133 import imageSize ._
127- val out = imageSize.resizedPath(originalPath, Option (format))
134+ var out = imageSize.resizedPath(originalPath, Option (format))
128135 if (! Files .exists(out) || replace) {
129136 if (width == originalWidth && height == originalHeight) {
130137 Files .copy(originalPath, out, REPLACE_EXISTING )
@@ -135,15 +142,26 @@ object ImageTools {
135142 var leftMargin = 0
136143 if (originalWidth > originalHeight) {
137144 imgHeight = originalHeight * width / originalWidth
138- topMargin = (imgWidth - imgHeight) / 2
145+ topMargin = Math .abs (imgWidth - imgHeight) / 2
139146 } else {
140147 imgWidth = originalWidth * height / originalHeight
141- leftMargin = (imgHeight - imgWidth) / 2
148+ leftMargin = Math .abs (imgHeight - imgWidth) / 2
142149 }
143150
144151 val dest = Scalr .resize(src, Scalr .Method .ULTRA_QUALITY , imgWidth, imgHeight)
145152 val dest2 = Scalr .move(dest, leftMargin, topMargin, width, height, Color .WHITE )
146- ImageIO .write(dest2, format, Files .newOutputStream(out))
153+ Try (ImageIO .write(dest2, format, Files .newOutputStream(out))) match {
154+ case Success (_) =>
155+ case Failure (f) =>
156+ logger.error(
157+ s """ an error occurred while trying to resize image $originalPath to
158+ | ${imageSize.width}x ${imageSize.height} :
159+ | ${f.getMessage}""" .stripMargin
160+ )
161+ if (! Files .exists(out)) {
162+ out = originalPath
163+ }
164+ }
147165 }
148166 }
149167 out
0 commit comments