Usage Overview

Basic Usage

Method Chaining(Recommend)

package main

import "github.com/fishtailstudio/imgo"

func main() {
    imgo.Load("background.png").
        Resize(250, 350).
        Insert("gopher.png", 50, 50).
        Save("out.png")
}

Not Method Chaining

package main

import "github.com/fishtailstudio/imgo"

func main() {
    background := imgo.Load("background.png")
    background.Resize(250, 350)
    background.Insert("gopher.png", 50, 50)
    background.Save("out.png")
}

The above two examples are equivalent.

Reading Images

Using ImGo to read an image is very simple. You just call imgo.Load() method.

imgo.Load("gopher.png")

This method can not only read local files, but also support the following input formats.

  • Path of the image in filesystem.

  • URL of an image.

  • Base64 encoded image data.

  • The instance of *os.File .

  • The instance of the types that implement the image.Image interface.

  • The instance of *imgo.Image .

Creating Images

Use the Canvas method to create a canvas with a specified width, height and color.

package main

import (
    "awesomeProject/imgo"
    "image/color"
)

func main() {
    imgo.Canvas(500, 500, color.White).
        Save("out.png")
}

Editing Images

After using the Load or Canvas method to obtain an image instance, now you can call the method to edit the image.

Methods for editing images will return instances of *imgo.Image, so they can be called in a chain.

Image Output

Output as a file

You can save the edited image as a file by calling the Save method.

The Save method receives a string parameter as file path, which can be either an absolute path or a relative path. The file path parameter needs to have an image format suffix, such as out.png 。ImGo will output the image in the corresponding format according to the image format suffix, and no other operation is required.

The supported image formats to save are as follows.

Webp format is not supported to output because the golang.org/x/image/webp package only provides the Decode method, not the Encode method.

HTTP Response

ImGo supports directly using the edited image as an HTTP response as follows.

package main

import (
    "awesomeProject/imgo"
    "net/http"
)

func main() {
    http.HandleFunc("/gopher", imgo.Load("gopher.png").HttpHandler)
    http.ListenAndServe(":8080", nil)
}

Run the above example on the local computer, and access http://localhost/gopher in browser, you will see gopher.png image.

Last updated