Skip to content

gwpp/tinify-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tinify API client for Golang

📖 国内的朋友看这里


Golang client for the Tinify API, used for TinyPNG and TinyJPG. Tinify compresses or resize your images intelligently. Read more at http://tinify.com.

Documentation

Go to the documentation for the HTTP client.

Installation

Install the API client with go get.

go get -u github.com/gwpp/tinify-go

Usage

  • 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 and cover. If used fit or cover, you must provide both 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

Running tests

cd $GOPATH/src/github.com/gwpp/tinify-go
go test

License

This software is licensed under the MIT License. View the license.