<!-- more -->
入门
这是什么
LatticePHP是一个点阵图生成软件包,用于PHP生成点阵图。因为点阵图生成个别是嵌入式语言所须要的,而PHP简直不应用,于是我开发了这一款冷门的软件包,心愿能帮忙你。
我的项目曾经开源:Github地址
环境
- PHP >= 7.1
装置
应用composer装置
composer require zmxy/lattice
或者返回GitHub仓库下载源文件,自行手动导入
应用
先创立一个画布
use Lattice\LatticePck\Lattice;$width = 296;$height = 128;$lattice = new Lattice();$lattice->createBlankImage($width, $height);
这样子咱们就创立了一个296X128的一个画布
接下来往上写字
$lattice->text('LatticePHP-应用PHP生成点阵图', [0, 0]);
成果如下
画一个矩形
use Lattice\LatticePck\LatticeImg;LatticeImg::Rectangle($lattice, 50, 50, [50, 50], 0, 1);
成果如下
画二维码
use Lattice\LatticePck\LatticeImg;$order = "http://weixin.qq.com/r/BRy0rI7EoNPfrcrP90kX";LatticeImg::QrCode($lattice, $order, [0, 12, 'center'], "public/", 0);
输入HTML来看看成果
use Lattice\LatticePck\LatticeOutput;$latticeOutput = (new LatticeOutput($lattice));echo $latticeOutput->getHTML();
残缺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.黑 */
字体
获取点阵字体
一般的字体因为加了锐角、丑化是不能间接用来生成点阵图的,必须应用点阵字体
。然而这方面的市场十分小,所以做的人很少,仅有的几个还会收取昂扬费用。
这里提供一个工具用来生成点阵字体。
须要留神的是,如果源字体不可商用,您也无奈将这个点阵字体商用,请尊重版权。本文会用收费的宋体来演示。
点阵工具下载
这外面次要是设置字号和字体宽高度(比如说一般来讲16号字体宽高都是16)。
格局 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形成的数组。
例如:
图形
点
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
echo $latticeOutput->getHTML();
输入数组
通过<pre>
这个标签能够格式化输入
echo '<pre>';print_r($latticeOutput->getImageArray());
输入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合并字符串
use Lattice\Utils\CString;CString::MergeBetween('AAAAA', 'BBBBB');
/** * 以相似ABABAB模式的形式合并两个字符串后果。 * @param string $str1 String A * @param string $str2 String B * @return string Merged string */