> For the complete documentation index, see [llms.txt](https://imgo.gitbook.io/cn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://imgo.gitbook.io/cn/usage/overview.md).

# 使用概览

## 基本用法

### 链式调用（推荐）

链式调用看起来非常简洁。

```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")
}
```

### 非链式调用

```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")
}
```

这个例子和上个例子是等效的。

## 读取图像

使用 ImGo 读取图像非常简单，你只要调用 `imgo.Load()` 方法即可。

```go
imgo.Load("gopher.png")
```

该方法不仅可以读取本地文件，还支持以下输入格式。

* 文件系统中图像的绝对路径或相对路径
* 图片的 URL
* Base64 编码的图像数据
* `*os.File` 类型的文件实例
* 实现了 `image.Image` 接口的类型实例
* `*imgo.Image` 类型的实例

## 创建图像

使用 `Canvas` 方法可以创建一个指定长宽和颜色的画布。

```go
package main

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

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

## 编辑图像

使用 `Load` 或 `Canvas` 方法获得图像实例后，就可以调用方法编辑图像了。

编辑图像的方法都会返回 `*imgo.Image` 类型的实例，因此才可以链式的调用。

## 图像输出

### 输出为文件

只要调用 `Save` 方法就可以将编辑好的图像保存为文件。

`Save` 方法接收一个字符串类型的文件路径参数，可以是绝对路径也可以是相对路径。

该路径参数需要带图像格式后缀，如 `out.png` 。

ImGo 会根据图像格式后缀输出相应格式的图像，无需其他操作。

支持的图像格式格式如下。

<table><thead><tr><th width="156.6756096947123">格式</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 格式的图像输出原因是 `golang.org/x/image/webp` 包只提供了 `Decode` 方法，没有提供 `Encode` 方法。

### HTTP响应

ImGo 支持将编辑好的图像直接作为 HTTP 响应，使用方法如下。

```go
package main

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

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

在本机运行上例，在浏览器访问 `http://localhost/gopher` 将能看到 `gopher.png` 图像。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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/cn/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.
