# Usage Overview

## Basic Usage

### **Method Chaining**（Recommend）

```go
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**

```go
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.

```go
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.

```go
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.

<table><thead><tr><th width="156.6756096947123">Formats</th><th>Mimetype</th></tr></thead><tbody><tr><td>jpg</td><td>image/jpeg</td></tr><tr><td>png</td><td>image/png</td></tr><tr><td>bmp</td><td>image/x-ms-bmp</td></tr><tr><td>tiff</td><td>image/tiff</td></tr></tbody></table>

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.

```go
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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://imgo.gitbook.io/en/usage/overview.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
