Added basic code from https://github.com/status-im/image-resizer
Made some modifications to suit a general approach, more changes need to be applied
This commit is contained in:
parent
d7eac44c77
commit
17f0bac6ae
2
go.mod
2
go.mod
|
@ -43,8 +43,10 @@ require (
|
|||
github.com/multiformats/go-multibase v0.0.1
|
||||
github.com/multiformats/go-varint v0.0.5
|
||||
github.com/mutecomm/go-sqlcipher v0.0.0-20190227152316-55dbde17881f
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
||||
github.com/okzk/sdnotify v0.0.0-20180710141335-d9becc38acbd
|
||||
github.com/olekukonko/tablewriter v0.0.2 // indirect
|
||||
github.com/oliamb/cutter v0.2.2
|
||||
github.com/onsi/ginkgo v1.10.3 // indirect
|
||||
github.com/onsi/gomega v1.7.1 // indirect
|
||||
github.com/pborman/uuid v1.2.0
|
||||
|
|
4
go.sum
4
go.sum
|
@ -541,6 +541,8 @@ github.com/mutecomm/go-sqlcipher v0.0.0-20190227152316-55dbde17881f/go.mod h1:My
|
|||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
github.com/nakagami/firebirdsql v0.0.0-20190310045651-3c02a58cfed8/go.mod h1:86wM1zFnC6/uDBfZGNwB65O+pR2OFi5q/YQaEUid1qA=
|
||||
github.com/naoina/toml v0.0.0-20170918210437-9fafd6967416/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E=
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
|
||||
github.com/nsf/termbox-go v0.0.0-20170211012700-3540b76b9c77/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
|
||||
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
|
||||
github.com/okzk/sdnotify v0.0.0-20180710141335-d9becc38acbd h1:+iAPaTbi1gZpcpDwe/BW1fx7Xoesv69hLNGPheoyhBs=
|
||||
|
@ -551,6 +553,8 @@ github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8u
|
|||
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
|
||||
github.com/olekukonko/tablewriter v0.0.2 h1:sq53g+DWf0J6/ceFUHpQ0nAEb6WgM++fq16MZ91cS6o=
|
||||
github.com/olekukonko/tablewriter v0.0.2/go.mod h1:rSAaSIOAGT9odnlyGlUfAJaoc5w2fSBUmeGDbRWPxyQ=
|
||||
github.com/oliamb/cutter v0.2.2 h1:Lfwkya0HHNU1YLnGv2hTkzHfasrSMkgv4Dn+5rmlk3k=
|
||||
github.com/oliamb/cutter v0.2.2/go.mod h1:4BenG2/4GuRBDbVm/OPahDVqbrOemzpPiG5mi1iryBU=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w=
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
package images
|
||||
|
||||
const ImageDir = "assets/"
|
|
@ -0,0 +1,40 @@
|
|||
package images
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Get(fileName string) (image.Image, error) {
|
||||
file, err := os.Open(fileName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
img, err := jpeg.Decode(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
file.Close()
|
||||
|
||||
return img, nil
|
||||
}
|
||||
|
||||
func Render(img image.Image, imgDetail *Details) error {
|
||||
out, err := os.Create(imgDetail.FileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
o := new(jpeg.Options)
|
||||
o.Quality = imgDetail.Quality
|
||||
|
||||
jpeg.Encode(out, img, o)
|
||||
|
||||
fi, _ := out.Stat()
|
||||
imgDetail.SizeFile = fi.Size()
|
||||
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
package images
|
|
@ -0,0 +1,20 @@
|
|||
package images
|
||||
|
||||
import (
|
||||
"image"
|
||||
|
||||
"github.com/nfnt/resize"
|
||||
"github.com/oliamb/cutter"
|
||||
)
|
||||
|
||||
func ResizeSquare(size uint, img image.Image) image.Image {
|
||||
return resize.Resize(size, 0, img, resize.Bilinear)
|
||||
}
|
||||
|
||||
func CropSquare(img image.Image, size int, anchor image.Point) (image.Image, error) {
|
||||
return cutter.Crop(img, cutter.Config{
|
||||
Width: size,
|
||||
Height: size,
|
||||
Anchor: anchor,
|
||||
})
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package images
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// Raster image types
|
||||
JPEG FileType = 1 + iota
|
||||
PNG
|
||||
GIF
|
||||
WEBP
|
||||
|
||||
// Vector image types
|
||||
SVG
|
||||
AI
|
||||
)
|
||||
|
||||
type Details struct {
|
||||
SizePixel uint
|
||||
SizeFile int64
|
||||
Quality int
|
||||
FileName string
|
||||
FileType FileType
|
||||
Properties string
|
||||
}
|
||||
|
||||
type FileType uint
|
||||
|
||||
func MakeDetails(imageName string, size uint, quality int, properties string) Details {
|
||||
return Details{
|
||||
SizePixel: size,
|
||||
Quality: quality,
|
||||
Properties: properties,
|
||||
FileName: makeOutputName(imageName, size, quality, properties),
|
||||
}
|
||||
}
|
||||
|
||||
func makeOutputName(imageName string, size uint, i int, properties string) string {
|
||||
if properties != "" {
|
||||
properties = "_" + strings.ReplaceAll(properties, " ", "-")
|
||||
}
|
||||
return fmt.Sprintf(ImageDir+"%s_s-%d_q-%d%s.jpg", imageName, size, i, properties)
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package images
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFileTypes(t *testing.T) {
|
||||
fmt.Printf("%d\n", WEBP)
|
||||
fmt.Printf("%d\n", SVG)
|
||||
fmt.Printf("%d\n", AI)
|
||||
}
|
Loading…
Reference in New Issue