# 插入本地图片

支持两种方式插入 PNG 或 JPEG 图像：图像位于本地磁盘时使用路径方式，已经持有字节流（上传、对象存储、流式读取等）时使用缓冲区方式。

## **函数原型**

```php
insertImage(int $row, int $column, string $localImagePath, double $widthScale = 1.0, double $heightScale = 1.0): self

insertImageBuffer(int $row, int $column, string $bytes, ?array $options = null): self
```

### **int $row**

> 单元格所在行

### **int $column**

> 单元格所在列

### **string $localImagePath**

> 图片路径（PNG 或 JPEG）

### **double $widthScale**

> 对图像 X 轴进行缩放处理；默认为 1，保持原始宽度；值为 0.5 时，图像宽度为原图的 1/2。

### **double $heightScale**

> 对图像 Y 轴进行缩放处理；默认为 1，保持原始高度；值为 0.5 时，图像高度为原图的 1/2。

### **string $bytes**

> 原始图片字节流，例如 `file_get_contents()` 或网络流读取的返回值。

### **array $options**（`insertImageBuffer` 可选参数）

> 全部为可选键：
>
> * `x_offset` *int* —— 横向像素偏移
> * `y_offset` *int* —— 纵向像素偏移
> * `x_scale` *double* —— 横向缩放比例（默认 `1.0`）
> * `y_scale` *double* —— 纵向缩放比例（默认 `1.0`）
> * `object_position` *int* —— 锚定模式（默认 `2`）
> * `url` *string* —— 图像链接地址
> * `description` *string* —— 无障碍 / 替代文本描述

## 示例 —— 本地路径

```php
$excel = new \Vtiful\Kernel\Excel($config);

$excel->fileName('tutorial.xlsx')
      ->insertImage(5, 0, '/vagrant/logo.png')
      ->output();
```

## 示例 —— 内存字节流

```php
$excel = new \Vtiful\Kernel\Excel($config);

$bytes = file_get_contents('/vagrant/logo.png');

$excel->fileName('tutorial.xlsx')
      ->insertImageBuffer(5, 0, $bytes, [
          'x_scale'     => 0.5,
          'y_scale'     => 0.5,
          'url'         => 'https://github.com/viest/php-ext-xlswriter',
          'description' => 'xlswriter logo',
      ])
      ->output();
```


---

# 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://xlswriter-docs.viest.me/zhong-wen-jian-ti/cell/insert-image.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.
