> 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/api/insert.md).

# Insert

在已有图像上插入一个图像。

## 参数

<table><thead><tr><th width="198.4826833846656">参数名</th><th width="208.18338610093963">类型</th><th>说明</th></tr></thead><tbody><tr><td>source</td><td><code>interface{}</code></td><td>待插入的图像</td></tr><tr><td>x</td><td><code>int</code></td><td>待插入图像左上角坐标 x 轴</td></tr><tr><td>y</td><td><code>int</code></td><td>待插入图像左上角坐标 y 轴</td></tr></tbody></table>

`source` 参数支持以下类型：

<table><thead><tr><th width="236.7279029462738">支持类型</th><th>数据类型</th></tr></thead><tbody><tr><td>文件系统中图像的绝对路径或相对路径</td><td><code>string</code> 或 <code>[]byte</code></td></tr><tr><td>图片的 URL</td><td><code>string</code> 或 <code>[]byte</code></td></tr><tr><td>Base64 编码的图像数据</td><td><code>string</code> 或 <code>[]byte</code></td></tr><tr><td><code>*os.File</code> 类型的文件实例</td><td><code>*os.File</code></td></tr><tr><td>实现了 <code>image.Image</code> 接口的类型实例</td><td><code>image.Image</code> 以及实现了 <code>image.Image</code> 接口的类型</td></tr><tr><td><code>*imgo.Image</code> 类型的实例</td><td><code>*imgo.Image</code></td></tr></tbody></table>

## 返回值

`*imgo.Image` 类型的实例。

## 例子

### 图像文件路径

```go
package main

import (
    "github.com/fishtailstudio/imgo"
    "image/color"
)

func main() {
    imgo.Canvas(500, 500, color.Black).
        Insert("gopher.png", 100, 100).
        Save("out.png")
}
```

### 图像 URL

```go
package main

import (
    "github.com/fishtailstudio/imgo"
    "image/color"
)

func main() {
    url := "https://www.baidu.com/img/flexible/logo/pc/result.png"
    imgo.Canvas(500, 500, color.Black).
        Insert(url, 100, 100).
        Save("out.png")
}
```

### Base64 编码的图像数据

```go
package main

import (
    "github.com/fishtailstudio/imgo"
    "image/color"
)

func main() {
    base64Img := imgo.Load("gopher.png").ToBase64()
    imgo.Canvas(500, 500, color.Black).
        Insert(base64Img, 50, 50).
        Save("out.png")
}
```

### `*os.File` 类型的文件实例

```go
package main

import (
    "github.com/fishtailstudio/imgo"
    "image/color"
    "os"
)

func main() {
    file, err := os.Open("gopher.png")
    if err != nil {
        panic(err)
    }
    defer file.Close()
    
    imgo.Canvas(500, 500, color.Black).
        Insert(file, 100, 100).
        Save("out.png")
}
```

### 实现了 `image.Image` 接口的类型实例

```go
package main

import (
    "github.com/fishtailstudio/imgo"
    "image/color"
    "image/png"
    "os"
)

func main() {
    file, err := os.Open("gopher.png")
    if err != nil {
        panic(err)
    }
    defer file.Close()
    
    img, err := png.Decode(file)
    if err != nil {
        panic(err)
    }
    
    imgo.Canvas(500, 500, color.Black).
        Insert(img, 100, 100).
        Save("out.png")
}
```

### `*imgo.Image` 类型的实例

```go
package main

import (
    "github.com/fishtailstudio/imgo"
    "image/color"
)

func main() {
    img := imgo.Load("gopher.png")
    imgo.Canvas(500, 500, color.Black).
        Insert(img, 100, 100).
        Save("out.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/api/insert.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.
