Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
Avatar
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
Avatar
Commits
060fb5f4
Commit
060fb5f4
authored
Jul 07, 2020
by
Tim Coates (CPVV)
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove alpha channel for jpeg and jfif formats
parent
405c43df
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
5 deletions
+16
-5
src/main/java/au/com/tpic/avatar/services/AvatarService.java
src/main/java/au/com/tpic/avatar/services/AvatarService.java
+16
-5
No files found.
src/main/java/au/com/tpic/avatar/services/AvatarService.java
View file @
060fb5f4
...
...
@@ -9,12 +9,12 @@ import org.springframework.stereotype.Service;
import
org.springframework.util.DigestUtils
;
import
javax.imageio.ImageIO
;
import
java.awt.*
;
import
java.awt.geom.AffineTransform
;
import
java.awt.image.AffineTransformOp
;
import
java.awt.image.BufferedImage
;
import
java.awt.image.WritableRaster
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
...
...
@@ -26,13 +26,14 @@ import java.util.stream.Stream;
public
class
AvatarService
{
private
final
HashMap
<
String
,
Path
>
fileList
;
private
final
BufferedImage
defaultImage
;
private
static
final
String
DEFAULT_FORMAT
=
"jpg"
;
private
static
final
Path
DEFAULT_IMAGE_PATH
=
new
File
(
AvatarService
.
class
.
getResource
(
"/default.jpg"
).
getFile
()).
toPath
();
public
AvatarService
(
AvatarServiceProperites
properites
)
throws
IOException
{
Stream
<
Path
>
avatars
=
Files
.
walk
(
properites
.
getDirectory
());
this
.
fileList
=
new
HashMap
<>();
Stream
<
Path
>
avatars
=
Files
.
walk
(
properites
.
getDirectory
());
avatars
.
filter
(
this
::
correctFormat
).
forEach
(
path
->
this
.
fileList
.
put
(
getFileHash
(
path
),
path
));
this
.
defaultImage
=
ImageIO
.
read
(
getClass
().
getResourceAsStream
(
"/default.jpg"
));
}
public
ResponseEntity
<
byte
[]>
getAvatar
(
String
request
,
String
size
)
throws
IOException
{
...
...
@@ -42,8 +43,8 @@ public class AvatarService {
BufferedImage
image
=
(
fileList
.
containsKey
(
hash
))
?
readImageToSize
(
fileList
.
get
(
hash
),
Integer
.
parseInt
(
size
))
:
generateIdenticons
(
hash
,
Integer
.
parseInt
(
size
));
return
ResponseEntity
.
ok
().
contentType
(
format
).
body
(
writeToFormat
(
image
,
format
.
getSubtype
()));
}
catch
(
Exception
ignored
)
{
BufferedImage
defaultImage
=
readImageToSize
(
DEFAULT_IMAGE_PATH
,
Integer
.
parseInt
(
size
));
return
ResponseEntity
.
ok
().
contentType
(
MediaType
.
IMAGE_JPEG
).
body
(
writeToFormat
(
d
efaultImage
,
DEFAULT_FORMAT
));
BufferedImage
resizedDefaultImage
=
resizeImage
(
defaultImage
,
Integer
.
parseInt
(
size
));
return
ResponseEntity
.
ok
().
contentType
(
MediaType
.
IMAGE_JPEG
).
body
(
writeToFormat
(
resizedD
efaultImage
,
DEFAULT_FORMAT
));
}
}
...
...
@@ -81,6 +82,10 @@ public class AvatarService {
private
BufferedImage
readImageToSize
(
Path
imagePath
,
int
size
)
throws
IOException
{
BufferedImage
inputImage
=
ImageIO
.
read
(
imagePath
.
toFile
());
return
resizeImage
(
inputImage
,
size
);
}
private
BufferedImage
resizeImage
(
BufferedImage
inputImage
,
int
size
)
{
int
newHeight
=
inputImage
.
getHeight
()
/
inputImage
.
getWidth
()
*
size
;
ResampleOp
resizeOp
=
new
ResampleOp
(
size
,
newHeight
);
resizeOp
.
setFilter
(
ResampleFilters
.
getLanczos3Filter
());
...
...
@@ -89,6 +94,12 @@ public class AvatarService {
private
byte
[]
writeToFormat
(
BufferedImage
image
,
String
format
)
throws
IOException
{
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
if
(
List
.
of
(
"jpg"
,
"jpeg"
,
"jfif"
).
contains
(
format
))
{
// Remove alpha channel
BufferedImage
result
=
new
BufferedImage
(
image
.
getWidth
(),
image
.
getHeight
(),
BufferedImage
.
TYPE_INT_RGB
);
result
.
createGraphics
().
drawImage
(
image
,
0
,
0
,
Color
.
BLACK
,
null
);
image
=
result
;
}
ImageIO
.
write
(
image
,
format
,
baos
);
return
baos
.
toByteArray
();
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment