2020-10-09 11:09:17 +00:00
|
|
|
package images
|
|
|
|
|
|
|
|
import (
|
2020-10-22 15:27:58 +00:00
|
|
|
"bytes"
|
2020-11-18 12:41:36 +00:00
|
|
|
"encoding/base64"
|
2022-08-08 15:22:22 +00:00
|
|
|
"errors"
|
2020-10-22 15:27:58 +00:00
|
|
|
"fmt"
|
2020-10-09 11:09:17 +00:00
|
|
|
"image"
|
|
|
|
"image/jpeg"
|
|
|
|
"io"
|
2022-08-08 15:22:22 +00:00
|
|
|
"regexp"
|
2022-09-02 13:59:52 +00:00
|
|
|
|
|
|
|
"github.com/nfnt/resize"
|
2020-10-09 11:09:17 +00:00
|
|
|
)
|
|
|
|
|
2020-10-22 15:27:58 +00:00
|
|
|
type EncodeConfig struct {
|
2020-10-27 14:42:42 +00:00
|
|
|
Quality int
|
2020-10-22 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Encode(w io.Writer, img image.Image, config EncodeConfig) error {
|
2020-10-09 11:09:17 +00:00
|
|
|
// Currently a wrapper for renderJpeg, but this function is useful if multiple render formats are needed
|
2020-10-22 15:27:58 +00:00
|
|
|
return renderJpeg(w, img, config)
|
2020-10-09 11:09:17 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 15:27:58 +00:00
|
|
|
func renderJpeg(w io.Writer, m image.Image, config EncodeConfig) error {
|
2020-10-09 11:09:17 +00:00
|
|
|
o := new(jpeg.Options)
|
2020-10-22 15:27:58 +00:00
|
|
|
o.Quality = config.Quality
|
2020-10-09 11:09:17 +00:00
|
|
|
|
|
|
|
return jpeg.Encode(w, m, o)
|
|
|
|
}
|
2020-10-22 15:27:58 +00:00
|
|
|
|
2022-09-05 14:02:10 +00:00
|
|
|
type FileSizeError struct {
|
|
|
|
expected int
|
|
|
|
received int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *FileSizeError) Error() string {
|
|
|
|
return fmt.Sprintf("image size after processing exceeds max, expected < '%d', received < '%d'", e.expected, e.received)
|
|
|
|
}
|
|
|
|
|
2022-09-02 13:59:52 +00:00
|
|
|
func EncodeToLimits(bb *bytes.Buffer, img image.Image, bounds FileSizeLimits) error {
|
2020-10-22 15:27:58 +00:00
|
|
|
q := MaxJpegQuality
|
|
|
|
for q > MinJpegQuality-1 {
|
|
|
|
|
|
|
|
err := Encode(bb, img, EncodeConfig{Quality: q})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-15 18:20:12 +00:00
|
|
|
if bounds.Ideal > bb.Len() {
|
2020-10-22 15:27:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if q == MinJpegQuality {
|
2022-04-15 18:20:12 +00:00
|
|
|
if bounds.Max > bb.Len() {
|
2020-10-22 15:27:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-09-05 14:02:10 +00:00
|
|
|
return &FileSizeError{expected: bounds.Max, received: bb.Len()}
|
2020-10-22 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 15:59:01 +00:00
|
|
|
bb.Reset()
|
2020-10-22 15:27:58 +00:00
|
|
|
q -= 2
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-18 12:41:36 +00:00
|
|
|
|
2022-09-02 13:59:52 +00:00
|
|
|
// CompressToFileLimits takes an image.Image and analyses the pixel dimensions, if the longest side is greater
|
|
|
|
// than the `longSideMax` image.Image will be resized, before compression begins.
|
|
|
|
// Next the image.Image is repeatedly encoded and resized until the data fits within
|
|
|
|
// the given FileSizeLimits. There is no limit on the number of times the cycle is performed, the image.Image
|
|
|
|
// is reduced to 95% of its size at the end of every round the file size exceeds the given limits.
|
|
|
|
func CompressToFileLimits(bb *bytes.Buffer, img image.Image, bounds FileSizeLimits) error {
|
|
|
|
longSideMax := 2000
|
|
|
|
|
|
|
|
// Do we need to do a pre-compression resize?
|
|
|
|
if img.Bounds().Max.X > img.Bounds().Max.Y {
|
|
|
|
// X is longer
|
|
|
|
if img.Bounds().Max.X > longSideMax {
|
|
|
|
img = resize.Resize(uint(longSideMax), 0, img, resize.Bilinear)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Y is longer or equal
|
|
|
|
if img.Bounds().Max.Y > longSideMax {
|
|
|
|
img = resize.Resize(0, uint(longSideMax), img, resize.Bilinear)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
err := EncodeToLimits(bb, img, bounds)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-05 14:02:10 +00:00
|
|
|
// If error is not a FileSizeError then we need to return it up
|
|
|
|
if fse := (*FileSizeError)(nil); !errors.As(err, &fse) {
|
2022-09-02 13:59:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
img = ResizeTo(95, img)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 18:20:12 +00:00
|
|
|
func EncodeToBestSize(bb *bytes.Buffer, img image.Image, size ResizeDimension) error {
|
|
|
|
return EncodeToLimits(bb, img, DimensionSizeLimit[size])
|
|
|
|
}
|
|
|
|
|
2020-11-18 12:41:36 +00:00
|
|
|
func GetPayloadDataURI(payload []byte) (string, error) {
|
2020-11-24 13:13:46 +00:00
|
|
|
if len(payload) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2020-11-18 12:41:36 +00:00
|
|
|
mt, err := GetMimeType(payload)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
b64 := base64.StdEncoding.EncodeToString(payload)
|
|
|
|
|
|
|
|
return "data:image/" + mt + ";base64," + b64, nil
|
|
|
|
}
|
2022-08-08 15:22:22 +00:00
|
|
|
|
|
|
|
func GetPayloadFromURI(uri string) ([]byte, error) {
|
|
|
|
re := regexp.MustCompile("^data:image/(.*?);base64,(.*?)$")
|
|
|
|
res := re.FindStringSubmatch(uri)
|
|
|
|
if len(res) != 3 {
|
|
|
|
return nil, errors.New("wrong uri format")
|
|
|
|
}
|
|
|
|
return base64.StdEncoding.DecodeString(res[2])
|
|
|
|
}
|