1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-10 10:00:46 +09:00

GO-230 Convert heic to jpeg

This commit is contained in:
kirillston 2023-01-16 14:50:00 +01:00
parent 7d372034d8
commit 2b16c09cf6
No known key found for this signature in database
GPG key ID: 88218A7F1109754B
5 changed files with 48 additions and 0 deletions

1
go.mod
View file

@ -100,6 +100,7 @@ require (
github.com/AndreasBriese/bbloom v0.0.0-20190825152654-46b345b51c96 // indirect
github.com/HdrHistogram/hdrhistogram-go v1.1.0 // indirect
github.com/RoaringBitmap/roaring v0.9.4 // indirect
github.com/adrium/goheif v0.0.0-20230113233934-ca402e77a786 // indirect
github.com/alecthomas/jsonschema v0.0.0-20191017121752-4bb6e3fae4f2 // indirect
github.com/alecthomas/units v0.0.0-20210927113745-59d0afb8317a // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect

2
go.sum
View file

@ -59,6 +59,8 @@ github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWX
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/Stebalien/go-bitfield v0.0.1/go.mod h1:GNjFpasyUVkHMsfEOk8EFLJ9syQ6SI+XWrX9Wf2XH0s=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/adrium/goheif v0.0.0-20230113233934-ca402e77a786 h1:zvgtcRb2B5gynWjm+Fc9oJZPHXwmcgyH0xCcNm6Rmo4=
github.com/adrium/goheif v0.0.0-20230113233934-ca402e77a786/go.mod h1:aKVJoQ0cc9K5Xb058XSnnAxXLliR97qbSqWBlm5ca1E=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=

View file

@ -13,6 +13,7 @@ import (
"io"
"strconv"
"github.com/adrium/goheif"
"github.com/disintegration/imaging"
"github.com/dsoprea/go-exif/v3"
jpegstructure "github.com/dsoprea/go-jpeg-image-structure/v2"
@ -20,6 +21,7 @@ import (
"github.com/anytypeio/go-anytype-middleware/pkg/lib/mill/ico"
// Import for image.DecodeConfig to support .webp format
_ "github.com/adrium/goheif"
_ "golang.org/x/image/webp"
)
@ -36,6 +38,7 @@ const (
GIF Format = "gif"
ICO Format = "ico"
WEBP Format = "webp"
HEIC Format = "heic"
)
var ErrWEBPNotSupported = errors.New("webp image format is not supported")
@ -100,6 +103,8 @@ func (m *ImageResize) Mill(r io.ReadSeeker, name string) (*Result, error) {
return m.resizeWEBP(&imgConfig, r)
case GIF:
return m.resizeGIF(&imgConfig, r)
case HEIC:
return m.resizeHEIC(&imgConfig, r)
}
return nil, fmt.Errorf("unknown format")
@ -298,6 +303,39 @@ func (m *ImageResize) resizeGIF(imgConfig *image.Config, r io.ReadSeeker) (*Resu
}, nil
}
func (m *ImageResize) resizeHEIC(imgConfig *image.Config, r io.ReadSeeker) (*Result, error) {
goheif.SafeEncoding = true
img, err := goheif.Decode(r)
var height int
width, err := strconv.Atoi(m.Opts.Width)
if err != nil {
return nil, fmt.Errorf("invalid width: " + m.Opts.Width)
}
resized := imaging.Resize(img, width, 0, imaging.Lanczos)
width, height = resized.Rect.Max.X, resized.Rect.Max.Y
quality, err := strconv.Atoi(m.Opts.Quality)
if err != nil {
return nil, fmt.Errorf("invalid quality: " + m.Opts.Quality)
}
buff := &bytes.Buffer{}
if err = jpeg.Encode(buff, resized, &jpeg.Options{Quality: quality}); err != nil {
return nil, err
}
return &Result{
File: buff,
Meta: map[string]interface{}{
"width": width,
"height": height,
},
}, nil
}
func getExifData(r io.ReadSeeker) (data []byte, err error) {
exifData, err := exif.SearchAndExtractExifWithReader(r)
if err != nil {

BIN
pkg/lib/mill/testdata/image.heic vendored Normal file

Binary file not shown.

View file

@ -44,4 +44,11 @@ var Images = []TestImage{
Width: 300,
Height: 187,
},
{
Path: "testdata/image.heic",
Format: "heic",
HasExif: false,
Width: 1440,
Height: 960,
},
}