PHPackages                             zmxy/lattice - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. zmxy/lattice

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

zmxy/lattice
============

PHP生成点阵图组件.教程：https://github.com/YunMengs/LatticePHP

1.0(3y ago)319↑2900%PHP

Since Aug 25Pushed 2y ago1 watchersCompare

[ Source](https://github.com/YunMengs/LatticePHP)[ Packagist](https://packagist.org/packages/zmxy/lattice)[ RSS](/packages/zmxy-lattice/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)DependenciesVersions (5)Used By (0)

[![image-20230112134617375](https://camo.githubusercontent.com/aec8e2ab8e47a2aa6a490725d6c5912b709255daabf95470f7607cd306d6e997/68747470733a2f2f73322e6c6f6c692e6e65742f323032332f30312f31322f4c6735705963326f6d53765a3646572e706e67)](https://camo.githubusercontent.com/aec8e2ab8e47a2aa6a490725d6c5912b709255daabf95470f7607cd306d6e997/68747470733a2f2f73322e6c6f6c692e6e65742f323032332f30312f31322f4c6735705963326f6d53765a3646572e706e67)

入门
--

[](#入门)

### 这是什么

[](#这是什么)

LatticePHP是一个点阵图生成软件包，用于PHP生成点阵图。由于点阵图生成一般是嵌入式语言所需要的，而PHP几乎不使用，于是我开发了这一款冷门的软件包，希望能帮助你。

### 环境

[](#环境)

- PHP &gt;= 7.1

### 安装

[](#安装)

使用composer安装

```
composer require zmxy/lattice

```

或者前往[GitHub仓库](https://github.com/Chenilove/LatticePHP/)下载源文件，自行手动导入

### 使用

[](#使用)

#### 先创建一个画布

[](#先创建一个画布)

```
use Lattice\LatticePck\Lattice;

$width = 296;
$height = 128;
$lattice = new Lattice();
$lattice->createBlankImage($width, $height);
```

这样子我们就创建了一个296X128的一个画布

#### 接下来往上写字

[](#接下来往上写字)

```
$lattice->text('LatticePHP-使用PHP生成点阵图', [0, 0]);

```

效果如下

[![image-20230112142505111](https://camo.githubusercontent.com/21fbb3d16a039a48b76273930133dce7fa79f7f4bc1c08997ae4389f0bade05f/68747470733a2f2f73322e6c6f6c692e6e65742f323032332f30312f31322f574c4745557035514b6c4648744a432e706e67)](https://camo.githubusercontent.com/21fbb3d16a039a48b76273930133dce7fa79f7f4bc1c08997ae4389f0bade05f/68747470733a2f2f73322e6c6f6c692e6e65742f323032332f30312f31322f574c4745557035514b6c4648744a432e706e67)

#### 画一个矩形

[](#画一个矩形)

```
use Lattice\LatticePck\LatticeImg;

LatticeImg::Rectangle($lattice, 50, 50, [50, 50], 0, 1);
```

效果如下

[![image-20230112144256556](https://camo.githubusercontent.com/4f3bccfb8804533caa3590290595b05e7dce29ce4ac8415145a1359c049d7ded/68747470733a2f2f73322e6c6f6c692e6e65742f323032332f30312f31322f36717a326e564546624841474b43332e706e67)](https://camo.githubusercontent.com/4f3bccfb8804533caa3590290595b05e7dce29ce4ac8415145a1359c049d7ded/68747470733a2f2f73322e6c6f6c692e6e65742f323032332f30312f31322f36717a326e564546624841474b43332e706e67)

#### 画二维码

[](#画二维码)

```
use Lattice\LatticePck\LatticeImg;

$order = "http://weixin.qq.com/r/BRy0rI7EoNPfrcrP90kX";
LatticeImg::QrCode($lattice, $order, [0, 12, 'center'], "public/", 0);
```

#### 输出HTML来看看效果

[](#输出html来看看效果)

```
use Lattice\LatticePck\LatticeOutput;

$latticeOutput = (new LatticeOutput($lattice));
echo $latticeOutput->getHTML();
```

[![202301121346500.png](https://camo.githubusercontent.com/aec8e2ab8e47a2aa6a490725d6c5912b709255daabf95470f7607cd306d6e997/68747470733a2f2f73322e6c6f6c692e6e65742f323032332f30312f31322f4c6735705963326f6d53765a3646572e706e67)](https://camo.githubusercontent.com/aec8e2ab8e47a2aa6a490725d6c5912b709255daabf95470f7607cd306d6e997/68747470733a2f2f73322e6c6f6c692e6e65742f323032332f30312f31322f4c6735705963326f6d53765a3646572e706e67)

#### 完整Demo

[](#完整demo)

```
require 'vendor/autoload.php';

use Lattice\LatticePck\Lattice;
use Lattice\LatticePck\LatticeImg;
use Lattice\LatticePck\LatticeOutput;

$width = 296;
$height = 128;
$lattice = new Lattice();
$latticeOutput = (new LatticeOutput($lattice));
$lattice->createBlankImage($width, $height);

// 坐标
$x = 6;
$y = 6;
$lattice->text('LatticePHP-使用PHP生成点阵图', [$x, $y, 'top-center']);
// LatticeImg::Rectangle($lattice, 50, 50, [50, 50], 0, 1);

$order = "http://weixin.qq.com/r/BRy0rI7EoNPfrcrP90kX";
LatticeImg::QrCode($lattice, $order, [0, 12, 'center'], "public/", 0);

echo $latticeOutput->getHTML();
```

代码文档
----

[](#代码文档)

### 画布

[](#画布)

您在做任何操作之前起码得先创建一个画布。

```
use Lattice\LatticePck\Lattice;

$width = 296;
$height = 128;
$lattice = new Lattice();
$lattice->createBlankImage($width, $height);
```

```
    /**
     * 创建空白图片
     * @param int $width
     * @param int $height
     * @param int $color 填充颜色 0.白 1.黑
     */
```

### 字体

[](#字体)

#### 获取点阵字体

[](#获取点阵字体)

普通的字体因为加了锐角、美化是不能直接用来生成点阵图的，必须使用`点阵字体`。但是这方面的市场非常小，所以做的人很少，仅有的几个还会收取高昂费用。

这里提供一个工具用来生成点阵字体。

> 需要注意的是，如果源字体不可商用，您也无法将这个点阵字体商用，请尊重版权。本文会用免费的宋体来演示。

[点阵工具下载](https://pan.baidu.com/s/1pUImz37hdZMF4m1fzF6_6A?pwd=6666)

这里面主要是设置字号和字体宽高度，宽高要保持8的倍数，比如12号字体宽高填16。

[![image-20230112163019884](https://camo.githubusercontent.com/a05484362f9b33650070c40f11b00ac06e6300145d31356923d068ee57a7c8e4/68747470733a2f2f73322e6c6f6c692e6e65742f323032332f30312f31322f6c4752396f36557546496674574d4c2e706e67)](https://camo.githubusercontent.com/a05484362f9b33650070c40f11b00ac06e6300145d31356923d068ee57a7c8e4/68747470733a2f2f73322e6c6f6c692e6e65742f323032332f30312f31322f6c4752396f36557546496674574d4c2e706e67)

格式 bin 然后保存即可。

#### 代码里使用

[](#代码里使用)

```
use Lattice\LatticePck\Lattice;

$fontDraw = Lattice::getFontDraw();
$fontDraw->setFont('./font/st16_16', 16, 8);
```

这里第一个参数是字体路径，第二个是中文宽度，第三个是英文宽度，因为英文比中文短一半，所以设置为8。（设置英文宽度主要是去除多余空白，不然影响美观）

```
	/**
     * 设置字体大小
     * @param stirng $fontFileName 字体文件路径
     * @param int $font_v 中文宽度
     * @param int $font_e 英文宽度
     * @return void
     */
```

### 颜色

[](#颜色)

`LatticePHP`是用 1 表示黑色 0 表示白色 （后期可以考虑加入其他颜色）。本质上点阵图就是一串`01010101`.

#### 颜色反转

[](#颜色反转)

```
$lattice->colorReflection($textArr);
```

```
    /**
     * 颜色反转
     * @param array $textArr 点阵数组
     * @return void
     */
```

您可以传入`$lattice->image`，因为这个是整张画布，如这么做将会把整张画布颜色反转。

### 坐标偏移

[](#坐标偏移)

所有绘制的图像需要设置X、Y坐标与偏移指令，例如：

```
[$x, $y, 'top-center']
```

除了`top-center`，还有`top-right`、`top-left`、`bottom-center`、`bottom-left`、`bottom-right`、`center-right`、`center`、`center-left`

意思应该懂吧，`top-center`就是Y轴靠上，X轴居中，以此类推。

### 文本

[](#文本)

```
use Lattice\LatticePck\Lattice;

$lattice = new Lattice();
$lattice->text('LatticePHP-使用PHP生成点阵图', [$x, $y, 'top-center']);
```

```
	/**
     * 写入文字
     * @param string $text 文字
     * @param array $xy xy坐标与偏移指令
     * @param int $color 字体颜色 1.黑色 0.白色
     * @param int $spacing 字间距
     * @param int $font_bold 1 加粗 0 不加粗
     * @param int $heightSpacing 1 去除上下空白 0 不去除
     * @param int $getType 1 获取点阵数组
     * @return array
     */

```

参数`$getType`的意思是返回一个由01构成的数组。

例如：

[![image-20230112170601447](https://camo.githubusercontent.com/387d091f75474f8fd86f87b0f4aac7f93694b72d3930fcb2d216321d521e8bfb/68747470733a2f2f73322e6c6f6c692e6e65742f323032332f30312f31322f7032776f596c4c575441376b6258782e706e67)](https://camo.githubusercontent.com/387d091f75474f8fd86f87b0f4aac7f93694b72d3930fcb2d216321d521e8bfb/68747470733a2f2f73322e6c6f6c692e6e65742f323032332f30312f31322f7032776f596c4c575441376b6258782e706e67)

### 图形

[](#图形)

#### 点

[](#点)

```
use Lattice\LatticePck\LatticeImg;

LatticeImg::point($lattice, [0, 12]);
```

```
    /**
     * 创建点
     * @param Lattice $lattice Lattice对象
     * @param array $xy XY坐标偏移指令
     * @param int $color 点颜色 1.黑 0.白 默认为1
     * @return void
     */
```

#### 直线

[](#直线)

```
use Lattice\LatticePck\LatticeImg;

LatticeImg::Line($lattice, [[1, 1],  [8, 8]]);
```

```
    /**
     * 创建线
     * @param Lattice $lattice Lattice对象
     * @param array $xy XY坐标[[1, 1],  [8, 8]] 不支持偏移指令
     * @param int $color 线条颜色 1.黑 0.白 默认为1
     * @return void
     */
```

#### 矩形

[](#矩形)

```
use Lattice\LatticePck\LatticeImg;

LatticeImg::Rectangle($lattice, $width, $height, [$x, $y]);
```

```
	/**
     * 创建矩形
     * @param Lattice $lattice Lattice对象
     * @param int $width 宽度
     * @param int $height 高度
     * @param array $xy XY坐标偏移
     * @param int $fillColor 填充颜色 0.白 1.黑
     * @param int $borderSize 边框大小
     * @param int $borderColor 边框颜色
     * @return void
     */
```

#### 插入自定义图片

[](#插入自定义图片)

```
use Lattice\LatticePck\LatticeImg;

$lattice->insertImg($lattice, $filePath, [$x, $y]);
```

```
    /**
     * 插入图片（只能是黑白）
     * @param Lattice $lattice 点阵类
     * @param string $filePath 图片的文件路径
     * @param array $xy 坐标偏移
     * @param int $mode 模式 0.只读白色，其余为黑 1. 只读黑色，其余为白 2. 黑白都读，其余为灰（用2表示），默认模式为1
     * @return void
     * @throws Exception
     */
```

### 条码

[](#条码)

因为条码种类繁多，所以`LatticePHP`内置了`CodeItNow`来生成条码，先生成图片，然后再插入。

#### 一维码

[](#一维码)

```
use Lattice\LatticePck\LatticeImg;

$order = "112233";
LatticeImg::BarCode($lattice, $order, [$x, $y, 'top-center']);
```

```
    /**
     * Barcode生成条纹码
     * @param Lattice $lattice 点阵类
     * @param string $text 内容字符串
     * @param array $xy xy坐标
     * @param string $barcodeType 条码格式 默认 BarcodeType::Code128
     * @param int $thickness 高度厚度 默认18
     * @param int $thick 厚度比例 默认1
     * @param int $fillColor 条纹码颜色 默认1
     * @param bool $delete 是否自动将生成的图片删除 默认true
     * @param string $filePath 文件路径 默认 public/images/ (结尾记得带/)
     * @param string $fileName 文件名 默认时间戳+随机数
     * @return void
     */
```

#### 二维码

[](#二维码)

```
use Lattice\LatticePck\LatticeImg;

$order = "http://weixin.qq.com/r/BRy0rI7EoNPfrcrP90kX";
LatticeImg::QrCode($lattice, $order, [$x, $y, 'center']);
```

```
    /**
     * Barcode生成二维码
     * @param Lattice $lattice 点阵类
     * @param string $text 字符串
     * @param array $xy xy坐标
     * @param int $size 尺寸 默认100
     * @param bool $delete 是否自动将生成的图片删除 默认true
     * @param string $filePath 生成二维码的文件路径 默认 public/images/ (结尾记得带/)
     * @param string $fileName 生成二维码的文件名 默认时间戳+随机数
     * @return void
     * @throws Exception
     */
```

### 输出画布

[](#输出画布)

我们有时候需要预览画布，您可以通过`LatticeOutput`这个工具类进行输出

```
use Lattice\LatticePck\Lattice;
use Lattice\LatticePck\LatticeOutput;

$width = 296;
$height = 128;
$lattice = new Lattice();
$latticeOutput = (new LatticeOutput($lattice));
$lattice->createBlankImage($width, $height);

$order = "http://weixin.qq.com/r/BRy0rI7EoNPfrcrP90kX";
LatticeImg::QrCode($lattice, $order, [0, 12, 'center']);
```

#### 输出HTML

[](#输出html)

```
echo $latticeOutput->getHTML();
```

#### 输出数组

[](#输出数组)

通过``这个标签可以格式化输出

```
echo '';
print_r($latticeOutput->getImageArray());
```

#### 输出16进制

[](#输出16进制)

将图片的`01`当做二进制转换为16进制

```
echo $latticeOutput->toHexa();
```

```
    /**
     * 将二进制数据转换16进制数串的函数（并反转）
     * @param array $olbImage 点阵数组 默认整张画布
     * @param bool $strrev 是否反转图片（将图片倒置并且竖直）
     * @return string $_32hexa
     */
```

### 其他

[](#其他)

#### 截取

[](#截取)

有时候您插入了一个超过了整张画布的图片，它不会报错，但肯定会影响您的程序。您需要使用这个方法进行画布截取。

```
$lattice->interception();
```

这将会把超出画布的元素截取掉。

#### 字符串换行

[](#字符串换行)

默认情况下一个字符串超出时不会换行，您可以通过这个函数进行换行

```
$address = "我是一个很长的地址，要被换行哦噢噢噢噢噢噢噢噢哦哦哦哦哦哦哦哦哦哦哦哦";
$addressArray = LatticeFont::strWrap($address, 48, 2);
```

```
    /**
     * 字符串换行分割
     * @param string $str 要被换行分割的字符串
     * @param int $length 一行的长度, 英文长度 默认12
     * @param int $hans_length 一个汉字等于多少个英文的宽度（GBK编码是2，UTF-8编码是3） 默认2
     * @param string $append 尾部追加的字符串 默认为空
     * @return array 字符串分割后的数组
     */
```

#### ABABAB合并字符串

[](#ababab合并字符串)

```
use Lattice\Utils\CString;

CString::MergeBetween('AAAAA', 'BBBBB');
```

```
    /**
     * 以类似ABABAB模式的方式合并两个字符串结果。
     * @param string $str1 String A
     * @param string $str2 String B
     * @return string Merged string
     */
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~46 days

Total

4

Last Release

1212d ago

Major Versions

0.03 → 1.02023-01-13

### Community

Maintainers

![](https://www.gravatar.com/avatar/e54d7ab5b296568444cef0688842287cbca93f135e20d13c8bb30664d6acdb5f?d=identicon)[zmxy](/maintainers/zmxy)

---

Top Contributors

[![YunMengs](https://avatars.githubusercontent.com/u/44698638?v=4)](https://github.com/YunMengs "YunMengs (23 commits)")

### Embed Badge

![Health badge](/badges/zmxy-lattice/health.svg)

```
[![Health](https://phpackages.com/badges/zmxy-lattice/health.svg)](https://phpackages.com/packages/zmxy-lattice)
```

###  Alternatives

[razonyang/yii2-rate-limiter

Yii2 Rate Limiter

1025.0k1](/packages/razonyang-yii2-rate-limiter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
