Skip to content

Commit 2eabb78

Browse files
committed
fix ImageTools
1 parent cb83ff5 commit 2eabb78

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ThisBuild / organization := "app.softnetwork"
3030

3131
name := "generic-persistence-api"
3232

33-
ThisBuild / version := "0.3.5"
33+
ThisBuild / version := "0.3.5.1"
3434

3535
ThisBuild / scalaVersion := "2.12.11"
3636

common/src/main/scala/app/softnetwork/utils/ImageTools.scala

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package app.softnetwork.utils
22

3+
import com.typesafe.scalalogging.StrictLogging
4+
35
import java.awt.Color
46
import java.awt.image.BufferedImage
57
import 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
23.4 KB
Loading
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package app.softnetwork.utils
2+
3+
import app.softnetwork.utils.ImageTools.{Icon, Small}
4+
import app.softnetwork.utils.MimeTypeTools.toFormat
5+
import org.scalatest.wordspec.AnyWordSpecLike
6+
7+
import java.nio.file.{Files, Path, Paths}
8+
9+
class ImageToolsSpec extends AnyWordSpecLike {
10+
11+
val path: Path =
12+
Paths.get(Thread.currentThread().getContextClassLoader.getResource("mars.jpeg").getPath)
13+
14+
"ImageTools" should {
15+
"check if it is an image" in {
16+
assert(ImageTools.isAnImage(path))
17+
}
18+
"resize an image" in {
19+
val sizes = Seq(Icon, Small)
20+
assert(ImageTools.generateImages(path, replace = false, sizes))
21+
val format = toFormat(path).getOrElse("jpeg")
22+
for (size <- sizes) {
23+
assert(Files.exists(size.resizedPath(path, Option(format))))
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)