Skip to content

Commit

Permalink
Merge pull request #1495 from anyproto/go-3889-adjust-file-extension-…
Browse files Browse the repository at this point in the history
…names

GO-3889 psd, tiff support
  • Loading branch information
requilence authored Aug 27, 2024
2 parents 1ad957d + df02ecd commit 18489bb
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 13 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/avast/retry-go/v4 v4.6.0
github.com/blevesearch/bleve/v2 v2.3.10
github.com/chai2010/webp v1.1.1
github.com/chai2010/webp v1.1.2-0.20240612091223-aa1b379218b7
github.com/cheggaaa/mb v1.0.3
github.com/cheggaaa/mb/v3 v3.0.2
github.com/dave/jennifer v1.7.0
Expand Down Expand Up @@ -71,6 +71,7 @@ require (
github.com/multiformats/go-multiaddr-dns v0.3.1
github.com/multiformats/go-multibase v0.2.0
github.com/multiformats/go-multihash v0.2.3
github.com/oov/psd v0.0.0-20220121172623-5db5eafcecbb
github.com/opentracing/opentracing-go v1.2.0
github.com/otiai10/copy v1.14.0
github.com/otiai10/opengraph/v2 v2.1.0
Expand Down Expand Up @@ -174,6 +175,7 @@ require (
github.com/google/flatbuffers v1.12.1 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/pprof v0.0.0-20240402174815-29b9bb013b0f // indirect
github.com/gopherjs/gopherjs v0.0.0-20190430165422-3e4dfb77656c // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chai2010/webp v1.1.1 h1:jTRmEccAJ4MGrhFOrPMpNGIJ/eybIgwKpcACsrTEapk=
github.com/chai2010/webp v1.1.1/go.mod h1:0XVwvZWdjjdxpUEIf7b9g9VkHFnInUSYujwqTLEuldU=
github.com/chai2010/webp v1.1.2-0.20240612091223-aa1b379218b7 h1:EcFyQu4Hz/YC2lc3xWqn678e2FNfG0cgTr/EOA4ByWs=
github.com/chai2010/webp v1.1.2-0.20240612091223-aa1b379218b7/go.mod h1:0XVwvZWdjjdxpUEIf7b9g9VkHFnInUSYujwqTLEuldU=
github.com/cheggaaa/mb v1.0.3 h1:03ksWum+6kHclB+kjwKMaBtgl5gtNYUwNpxsHQciKe8=
github.com/cheggaaa/mb v1.0.3/go.mod h1:NUl0GBtFLlfg2o6iZwxzcG7Lslc2wV/ADTFbLXtVPE4=
github.com/cheggaaa/mb/v3 v3.0.2 h1:jd1Xx0zzihZlXL6HmnRXVCI1BHuXz/kY+VzX9WbvNDU=
Expand Down Expand Up @@ -1127,6 +1127,8 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/oov/psd v0.0.0-20220121172623-5db5eafcecbb h1:JF9kOhBBk4WPF7luXFu5yR+WgaFm9L/KiHJHhU9vDwA=
github.com/oov/psd v0.0.0-20220121172623-5db5eafcecbb/go.mod h1:GHI1bnmAcbp96z6LNfBJvtrjxhaXGkbsk967utPlvL8=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/opentracing-contrib/go-observer v0.0.0-20170622124052-a52f23424492/go.mod h1:Ngi6UdF0k5OKD5t5wlmGhe/EDKPoUM3BXZSSfIuJbis=
github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74=
Expand Down
6 changes: 3 additions & 3 deletions pkg/lib/mill/image_exif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ func TestImageExif_Mill(t *testing.T) {
}

if exif.Width != i.Width {
t.Errorf("wrong width")
t.Errorf("wrong width %v %v", exif.Width, i.Width)
}
if exif.Height != i.Height {
t.Errorf("wrong height")
t.Errorf("wrong height %v %v", exif.Height, i.Height)
}
if exif.Format != i.Format {
t.Errorf("wrong format")
t.Errorf("wrong format %v %v", exif.Format, i.Format)
}
})
}
Expand Down
62 changes: 55 additions & 7 deletions pkg/lib/mill/image_resize.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ func init() {
}

const (
JPEG Format = "jpeg"
PNG Format = "png"
GIF Format = "gif"
ICO Format = "vnd.microsoft.icon"
WEBP Format = "webp"
HEIC Format = "heic"
JPEG Format = "jpeg"
PNG Format = "png"
GIF Format = "gif"
ICO Format = "vnd.microsoft.icon"
WEBP Format = "webp"
HEIC Format = "heic"
PSD Format = "psd"
PSD_MIME Format = "vnd.adobe.photoshop"
TIFF Format = "tiff"
)

func IsImage(mime string) bool {
Expand All @@ -51,7 +54,7 @@ func IsImage(mime string) bool {

func isImageFormatSupported(format Format) bool {
switch format {
case JPEG, PNG, GIF, ICO, WEBP, HEIC:
case JPEG, PNG, GIF, ICO, WEBP, HEIC, PSD_MIME, PSD, TIFF:
return true
}
return false
Expand Down Expand Up @@ -120,6 +123,10 @@ func (m *ImageResize) Mill(r io.ReadSeeker, name string) (*Result, error) {
return m.resizeGIF(&imgConfig, r)
case HEIC:
return m.resizeHEIC(&imgConfig, r)
case TIFF:
return m.resizeTIFF(&imgConfig, r)
case PSD:
return m.resizePSD(&imgConfig, r)
}

return nil, fmt.Errorf("unknown format")
Expand Down Expand Up @@ -336,6 +343,47 @@ func (m *ImageResize) resizeGIF(imgConfig *image.Config, r io.ReadSeeker) (*Resu
}, nil
}

func (m *ImageResize) resizeTIFF(imgConfig *image.Config, r io.ReadSeeker) (*Result, error) {
// tiff resizes to jpeg
img, _, err := image.Decode(r)
if err != nil {
return nil, fmt.Errorf("decode tiff: %w", err)
}
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)
}

buf := pool.Get()
defer func() {
_ = buf.Close()
}()

if err = jpeg.Encode(buf, resized, &jpeg.Options{Quality: quality}); err != nil {
return nil, err
}
readSeekCloser, err := buf.GetReadSeekCloser()
if err != nil {
return nil, err
}
return &Result{
File: readSeekCloser,
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 {
Expand Down
54 changes: 54 additions & 0 deletions pkg/lib/mill/image_resize_psd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package mill

import (
"fmt"
"image"
"image/jpeg"
"io"
"strconv"

"github.com/disintegration/imaging"
"github.com/oov/psd"
)

func (m *ImageResize) resizePSD(imgConfig *image.Config, r io.ReadSeeker) (res *Result, err error) {
img, _, err := psd.Decode(r, &psd.DecodeOptions{SkipLayerImage: true})
if err != nil {
return nil, err
}

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.Picker, 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)
}

buf := pool.Get()
defer func() {
_ = buf.Close()
}()

// encode to jpeg encoding to increase compatibility on mobile devices
if err = jpeg.Encode(buf, resized, &jpeg.Options{Quality: quality}); err != nil {
return nil, err
}
readSeekCloser, err := buf.GetReadSeekCloser()
if err != nil {
return nil, err
}
return &Result{
File: readSeekCloser,
Meta: map[string]interface{}{
"width": width,
"height": height,
},
}, nil
}
Binary file added pkg/lib/mill/testdata/image.tif
Binary file not shown.
14 changes: 14 additions & 0 deletions pkg/lib/mill/testdata/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,18 @@ var Images = []TestImage{
Width: 102,
Height: 79,
},
{
Path: "testdata/text.psd",
Format: "psd",
HasExif: true,
Width: 800,
Height: 600,
},
{
Path: "testdata/image.tif",
Format: "tiff",
HasExif: true,
Width: 1728,
Height: 2376,
},
}
Binary file added pkg/lib/mill/testdata/text.psd
Binary file not shown.

0 comments on commit 18489bb

Please sign in to comment.