@@ -63,59 +63,88 @@ object ImageTools extends StrictLogging {
6363 }
6464
6565 def generateImages (
66- originalImage : Path ,
67- replace : Boolean = false ,
66+ originalPath : Path ,
6867 imageSizes : Seq [ImageSize ] = Seq (Icon , Small )
6968 ): Boolean = {
70- val mimeType = detectMimeType(originalImage)
71- if (isAnImage(mimeType)) {
72- mimeType match {
73- case Some (mimeType) =>
74- val originalPath = originalImage.toAbsolutePath
75- val format = toFormat(Some (mimeType)).getOrElse(" jpeg" )
76- val src : BufferedImage = ImageIO .read(Files .newInputStream(originalImage))
77- val originalWidth = src.getWidth
78- val originalHeight = src.getHeight
79- for (imageSize <- imageSizes) {
80- resizeImage(
81- src,
82- originalWidth,
83- originalHeight,
84- originalPath,
85- format,
86- imageSize,
87- replace
88- )
69+ detectMimeType(originalPath) match {
70+ case Some (mimeType) if isAnImage(Option (mimeType)) =>
71+ val format = toFormat(Some (mimeType)).getOrElse(" jpeg" )
72+ val sizes = imageSizes.filter(size => {
73+ val out = size.resizedPath(originalPath, Option (format))
74+ ! Files .exists(out) || originalPath.toFile.lastModified() > out.toFile.lastModified()
75+ })
76+ if (sizes.nonEmpty) {
77+ logger.info(
78+ s """ Trying to resize image $originalPath to
79+ | ${sizes.map(s => s " {s.width}x ${s.height}" ).mkString(" ," )}""" .stripMargin
80+ )
81+ Try (ImageIO .read(Files .newInputStream(originalPath))) match {
82+ case Success (src) =>
83+ val originalWidth = src.getWidth
84+ val originalHeight = src.getHeight
85+ for (size <- sizes) {
86+ resizeImage(
87+ src,
88+ originalWidth,
89+ originalHeight,
90+ originalPath,
91+ format,
92+ size
93+ )
94+ }
95+ true
96+ case Failure (f) =>
97+ logger.error(
98+ s """ an error occurred while trying to resize image $originalPath to
99+ | ${sizes.map(s => s " {s.width}x ${s.height}" ).mkString(" ," )} :
100+ | ${f.getMessage}""" .stripMargin
101+ )
102+ false
89103 }
104+ } else {
90105 true
91- case _ => false
92- }
93- } else {
94- false
106+ }
107+ case _ =>
108+ logger.error(
109+ s """ an error occurred while trying to resize $originalPath to
110+ | ${imageSizes.map(s => s " {s.width}x ${s.height}" ).mkString(" ," )}""" .stripMargin
111+ )
112+ false
95113 }
96114 }
97115
98116 def getImage (
99117 originalPath : Path ,
100- size : Option [ImageSize ] = None ,
101- replace : Boolean = false
118+ size : Option [ImageSize ] = None
102119 ): Path = {
103120 size match {
104121 case Some (s) =>
105- val format = toFormat(originalPath).getOrElse(" jpeg" )
106- val out = s.resizedPath(originalPath, Option (format))
107- if (! Files .exists(out)) {
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- }
117- } else {
118- out
122+ detectMimeType(originalPath) match {
123+ case Some (mimeType) if isAnImage(Option (mimeType)) =>
124+ val format = toFormat(originalPath).getOrElse(" jpeg" )
125+ val out = s.resizedPath(originalPath, Option (format))
126+ if (
127+ ! Files .exists(out) || originalPath.toFile.lastModified() > out.toFile.lastModified()
128+ ) {
129+ Try (ImageIO .read(Files .newInputStream(originalPath))) match {
130+ case Success (src) =>
131+ resizeImage(src, src.getWidth, src.getHeight, originalPath, format, s)
132+ case Failure (f) =>
133+ logger.error(
134+ s """ an error occurred while trying to resize image $originalPath to
135+ | ${s.width}x ${s.height} :
136+ | ${f.getMessage}""" .stripMargin
137+ )
138+ originalPath
139+ }
140+ } else {
141+ out
142+ }
143+ case _ =>
144+ logger.error(
145+ s " an error occurred while trying to resize $originalPath to ${s.width}x ${s.height}"
146+ )
147+ originalPath
119148 }
120149 case _ => originalPath
121150 }
@@ -127,41 +156,38 @@ object ImageTools extends StrictLogging {
127156 originalHeight : Int ,
128157 originalPath : Path ,
129158 format : String ,
130- imageSize : ImageSize ,
131- replace : Boolean
159+ imageSize : ImageSize
132160 ): Path = {
133161 import imageSize ._
134162 var out = imageSize.resizedPath(originalPath, Option (format))
135- if (! Files .exists(out) || replace) {
136- if (width == originalWidth && height == originalHeight) {
137- Files .copy(originalPath, out, REPLACE_EXISTING )
163+ if (width == originalWidth && height == originalHeight) {
164+ Files .copy(originalPath, out, REPLACE_EXISTING )
165+ } else {
166+ var imgWidth = width
167+ var imgHeight = height
168+ var topMargin = 0
169+ var leftMargin = 0
170+ if (originalWidth > originalHeight) {
171+ imgHeight = originalHeight * width / originalWidth
172+ topMargin = Math .abs(imgWidth - imgHeight) / 2
138173 } else {
139- var imgWidth = width
140- var imgHeight = height
141- var topMargin = 0
142- var leftMargin = 0
143- if (originalWidth > originalHeight) {
144- imgHeight = originalHeight * width / originalWidth
145- topMargin = Math .abs(imgWidth - imgHeight) / 2
146- } else {
147- imgWidth = originalWidth * height / originalHeight
148- leftMargin = Math .abs(imgHeight - imgWidth) / 2
149- }
174+ imgWidth = originalWidth * height / originalHeight
175+ leftMargin = Math .abs(imgHeight - imgWidth) / 2
176+ }
150177
151- val dest = Scalr .resize(src, Scalr .Method .ULTRA_QUALITY , imgWidth, imgHeight)
152- val dest2 = Scalr .move(dest, leftMargin, topMargin, width, height, Color .WHITE )
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
178+ val dest = Scalr .resize(src, Scalr .Method .ULTRA_QUALITY , imgWidth, imgHeight)
179+ val dest2 = Scalr .move(dest, leftMargin, topMargin, width, height, Color .WHITE )
180+ Try (ImageIO .write(dest2, format, Files .newOutputStream(out))) match {
181+ case Success (_) =>
182+ case Failure (f) =>
183+ logger.error(
184+ s """ an error occurred while trying to resize image $originalPath to
158185 | ${imageSize.width}x ${imageSize.height} :
159186 | ${f.getMessage}""" .stripMargin
160- )
161- if (! Files .exists(out)) {
162- out = originalPath
163- }
164- }
187+ )
188+ if (! Files .exists(out)) {
189+ out = originalPath
190+ }
165191 }
166192 }
167193 out
0 commit comments