Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
client
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
client
Commits
fcce59b1
Commit
fcce59b1
authored
3 years ago
by
Benjamin Wenger
Browse files
Options
Downloads
Patches
Plain Diff
added CompressJpegForPreview
parent
e2d6be67
No related branches found
No related tags found
1 merge request
!170
Release
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
api/utils.go
+54
-0
54 additions, 0 deletions
api/utils.go
with
54 additions
and
0 deletions
api/utils.go
+
54
−
0
View file @
fcce59b1
...
...
@@ -21,6 +21,8 @@ const (
maxSize
int64
=
12000000
// Desired number of pixels in output image
desiredSize
=
307200
// Desired number of pixels in output image for preview
desiredPreviewSize
=
165
)
// CompressJpeg takes a JPEG image in byte format
...
...
@@ -73,3 +75,55 @@ func CompressJpeg(imgBytes []byte) ([]byte, error) {
// Return the compressed image in byte form
return
newImgBuf
.
Bytes
(),
nil
}
// CompressJpeg takes a JPEG image in byte format
// and compresses it based on desired output size
func
CompressJpegForPreview
(
imgBytes
[]
byte
)
([]
byte
,
error
)
{
// Convert bytes to a reader
imgBuf
:=
bytes
.
NewReader
(
imgBytes
)
// Ensure the size of the image is under the limit
if
imgSize
:=
imgBuf
.
Size
();
imgSize
>
maxSize
{
return
nil
,
errors
.
Errorf
(
"Image is too large: %d/%d"
,
imgSize
,
maxSize
)
}
// Decode the image information
imgInfo
,
err
:=
jpeg
.
DecodeConfig
(
imgBuf
)
if
err
!=
nil
{
return
nil
,
errors
.
Errorf
(
"Unable to decode image config: %+v"
,
err
)
}
// If the dimensions of the image are below desiredSize, no compression is required
if
imgInfo
.
Width
*
imgInfo
.
Height
<
desiredSize
{
return
imgBytes
,
nil
}
// Reset the buffer to the beginning to begin decoding the image
_
,
err
=
imgBuf
.
Seek
(
0
,
0
)
if
err
!=
nil
{
return
nil
,
errors
.
Errorf
(
"Unable to reset image buffer: %+v"
,
err
)
}
// Decode image into image.Image object
img
,
err
:=
jpeg
.
Decode
(
imgBuf
)
if
err
!=
nil
{
return
nil
,
errors
.
Errorf
(
"Unable to decode image: %+v"
,
err
)
}
// Determine the new width of the image based on desiredSize
newWidth
:=
uint
(
math
.
Sqrt
(
float64
(
desiredSize
)
*
(
float64
(
imgInfo
.
Width
)
/
float64
(
imgInfo
.
Height
))))
// Resize the image based on newWidth while preserving aspect ratio
newImg
:=
resize
.
Resize
(
newWidth
,
0
,
img
,
resize
.
Bicubic
)
// Encode the new image to a buffer
newImgBuf
:=
new
(
bytes
.
Buffer
)
err
=
jpeg
.
Encode
(
newImgBuf
,
newImg
,
nil
)
if
err
!=
nil
{
return
nil
,
errors
.
Errorf
(
"Unable to encode image: %+v"
,
err
)
}
// Return the compressed image in byte form
return
newImgBuf
.
Bytes
(),
nil
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment