Canvas
创建一个画布
选择扩展
gd
php
use Kkokk\Poster\Image\Gd\Canvas;
imagick
php
use Kkokk\Poster\Image\Imagick\Canvas;
创建画布
php
$background = [255, 255, 255];
$canvas = new Canvas(400, 400, $background);
// 或者
$canvas = new Canvas();
$canvas->newImage(400, 400, $background);
基础配置
php
$configs = [
'path' => '图片保存路径',
'type' => '图片类型',
'quality' => '图片质量:jpg、webp',
'dpi' => 'imagick可设置',
];
$canvas->config($configs);
读取图片
php
$source = '图片地址';
$canvas = new Canvas();
$bestFit = false;
$canvas->readImage($source, 400, 400, $bestFit);
参数说明
变量 | 类型 | 必填 | 注释 |
---|---|---|---|
source | string | 是 | 图片地址 |
width | int | 否 | 自定义宽度,例如:400 |
height | int | 否 | 自定义高度,例如:400 |
bestFit | bool | 否 | 是否等比缩放, 默认 false |
设置背景
php
$background = [255, 255, 255];
$canvas->background($background);
参数说明
变量 | 类型 | 必填 | 注释 |
---|---|---|---|
background | mixed | 是 | 背景色,例如:#ffffff、rgb(0, 0, 0)、 rgba(0, 0, 0, 0.5)、[255, 255, 255, 127] |
设置渐变背景
php
$rgba = [
[255, 255, 255],
[0, 0, 0]
];
$to = 'bottom';
$canvas->linearGradient($rgba, $to);
参数说明
变量 | 类型 | 必填 | 注释 |
---|---|---|---|
rgba | array | 是 | 渐变色 |
to | string | 是 | 方向,例如:bottom\top\left\right、gd 额外支持 top left\top right\bottom left\bottom right等 |
添加图片
添加图片前需用使用 Image
类创建图片对象
php
$image = new Image()
$canvas->addImage($image, 0, 0, 0, 0);
参数说明
变量 | 类型 | 必填 | 注释 |
---|---|---|---|
image | ImageGraphicsEngineInterface | 是 | 图片 |
dst_x | number|string|array | 否 | 画布位置x 特殊值 center 居中,居中并向左偏移 ['center',-5], 居中并向右偏移 ['center',5]; 支持百分比20% 支持自定义 支持正负 |
dst_y | number|string|array | 否 | 画布位置y 特殊值 center 居中,居中并向上偏移 ['center',-5], 居中并向下偏移 ['center',5]; 支持百分比20% 支持自定义 支持正负 |
src_x | number | 否 | 图片x轴,默认0 |
src_y | number | 否 | 图片y轴,默认0 |
添加文字
添加文字前需用使用 Text
类创建文字对象
php
$text = new Text();
$text->setText('故人西辞黄鹤楼,烟花三月下扬州。');
$canvas->addText($text, 0, 0);
参数说明
变量 | 类型 | 必填 | 注释 |
---|---|---|---|
image | Text | 是 | 文字 |
dst_x | number|string|array | 否 | 画布位置x 特殊值 center 居中,居中并向左偏移 ['center',-5], 居中并向右偏移 ['center',5]; 支持百分比20% 支持自定义 支持正负 |
dst_y | number|string|array | 否 | 画布位置y 特殊值 center 居中,居中并向上偏移 ['center',-5], 居中并向下偏移 ['center',5]; 支持百分比20% 支持自定义 支持正负 |
添加图片文字
添加图片文字前需用使用 ImageText
类创建图片文字对象
php
$imageText = new ImageText();
$text = new Text();
$text->setText('孤帆远影碧空尽,唯见长江天际流。');
$image = new Image();
$imageText->addText($text);
$imageText->addImage($image);
$canvas->addImageText($imageText, 0, 0);
参数说明
变量 | 类型 | 必填 | 注释 |
---|---|---|---|
imageText | ImageText | 是 | 图片文字 |
dst_x | number|string|array | 否 | 画布位置x 特殊值 center 居中,居中并向左偏移 ['center',-5], 居中并向右偏移 ['center',5]; 支持百分比20% 支持自定义 支持正负 |
dst_y | number|string|array | 否 | 画布位置y 特殊值 center 居中,居中并向上偏移 ['center',-5], 居中并向下偏移 ['center',5]; 支持百分比20% 支持自定义 支持正负 |
设置类型
设置图片类型(当前面已设置类型时,如果要重置类型需要传入 $force = true)
php
$force = true;
$canvas->setType('jpg', $force = false);
设置质量
jpeg、jpg、webp 可设置图片质量
php
$canvas->setQuality(100);
获取图片资源
php
$canvas->getImage();
获取宽度
php
$canvas->getWidth();
获取高度
php
$canvas->getHeight();
获取类型
php
$canvas->getType();
获取图片路径
获取路径和文件名
php
$canvas->getPath();
获取保存目录名
php
$canvas->getPathName();
获取保存文件名
php
$canvas->getFileName();
获取质量
php
$canvas->getQuality();
保存图片
php
$path = '图片保存路径';
$canvas->getData($path);
获取图片流
php
// 设置图片类型
$type = 'jpg';
$canvas->getStream($type);
获取 base64
php
$canvas->getBaseData();
获取二进制流
php
$canvas->blob();
保存到临时文件
php
$canvas->tmp();
设置图片
用于修改原图
php
$canvas->setData();
缩放(封面)
php
$newWidth = 400; // 新宽度
$newHeight = 400; // 新高度
$bestFit = false; // 是否等比缩放, 默认 false
$canvas->thumb($newWidth, $newHeight, $bestFit);
缩放(高质量)
php
$newWidth = 400; // 新宽度
$newHeight = 400; // 新高度
$bestFit = false; // 是否等比缩放, 默认 false
$canvas->scale($newWidth, $newHeight, $bestFit);
剪裁圆形
php
$canvas->circle();
剪裁矩形
php
$x = 0; // 剪裁位置x
$y = 0; // 剪裁位置y
$width = 0; // 剪裁宽度
$height = 0; // 剪裁高度
$canvas->crop($x, $y, $width, $height);
透明度
php
$transparent = 50; // 0-127
$canvas->transparent(transparent);
圆角
php
// [20] 四个角
// [20,30] 第一个值 左上 右下 第二个值 右上 左下
// [20,30,20] 第一个值 左上 第二个值 右上 左下 第三个值 右下
// [20,30,20,10] 左上 右上 右下 左下
$canvas->borderRadius(10);
应用蒙版(灰度图)
php
$mask = '灰度图蒙版路径';
$canvas->applyMask($mask);
抠图
php
$x1 = 0; // 抠图位置x
$y1 = 0; // 抠图位置y
$width = 0; // 抠图宽度
$height = 0; // 抠图高度
$crossCondition = null; // 抠图判断条件
// 返回抠图 Canvas
$croppedImage = $canvas->cutout($x1, $y1, $width, $height, $crossCondition);
画多边形
php
$points = []; // 点坐标
$color = [0, 0, 0, 1]; // 颜色
$thickness = 1; // 宽度
$canvas->drawImagePolygon($points, $color, $thickness);
填充多边形
php
$points = []; // 点坐标
$color = [0, 0, 0, 1]; // 颜色
$canvas->drawImageFilledPolygon($points, $color);