Golang client for the Tinify API, used for TinyPNG and TinyJPG. Tinify compresses or resize your images intelligently. Read more at http://tinify.com.
Go to the documentation for the HTTP client.
Install the API client with go get
.
go get -u github.com/gwpp/tinify-go
-
About key
Get your API key from https://tinypng.com/developers
-
Compress
func TestCompressFromFile(t *testing.T) { Tinify.SetKey(Key) source, err := Tinify.FromFile("./test.jpg") if err != nil { t.Error(err) return } err = source.ToFile("./test_output/CompressFromFile.jpg") if err != nil { t.Error(err) return } t.Log("Compress successful") }
-
Resize
func TestResizeFromBuffer(t *testing.T) { Tinify.SetKey(Key) buf, err := ioutil.ReadFile("./test.jpg") if err != nil { t.Error(err) return } source, err := Tinify.FromBuffer(buf) if err != nil { t.Error(err) return } err = source.Resize(&Tinify.ResizeOption{ Method: Tinify.ResizeMethodScale, Width: 200, }) if err != nil { t.Error(err) return } err = source.ToFile("./test_output/ResizesFromBuffer.jpg") if err != nil { t.Error(err) return } t.Log("Resize successful") }
-
Notice:
Tinify.ResizeMethod support
scale
,fit
andcover
. If used fit or cover, you must provideboth a width and a height
. But used scale, you must provide either a target width or a target height,but not both
. -
More usage, please see tinify_test.go
cd $GOPATH/src/github.com/gwpp/tinify-go
go test
This software is licensed under the MIT License. View the license.