Skip to content

生成海报

基础配置

php
$params = [
    'path'        => $path,        // 设置路径
    'dpi'         => $dpi,         // int[]|int 设置 dpi 只针对 Imagick 有效
    'font_size'   => $fontSize,    // 统一设置文字大小
    'font_rgba'   => $rgba,        // 统一设置文字颜色
    'font_space'  => $angle,       // 统一设置文字间距
    'font_weight' => $angle,       // 统一设置文字粗细
    'font_family' => $fontFamily,  // 统一设置文字字体,字体绝对路径
    'font_angle'  => $angle,       // 统一设置文字旋转角度
    'font_max_w'  => $maxW,        // 统一设置文字最大换行宽度
];
$poster->config($params);

设置路径

php
$poster->path($path); # 设置路径

参数说明

变量类型必填注释
pathstring|array地址,例如:poster/poster_user

创建画布

php
$poster->buildIm($w,$h,$rgba,$alpha); # 创建画布

参数说明

变量类型必填注释
wnumber画布宽
hnumber画布高
rgbaarray颜色rbga,[255,255,255,1]
alphaboolean是否透明,是:true

创建指定图片为画布

php
$poster->buildImDst($src,$w,$h,$rgba,$alpha); # 创建指定图片为画布

参数说明

变量类型必填注释
srcsource图像资源
wnumber画布宽,默认原图宽
hnumber画布高,默认原图高
rgbaarray颜色rbga,[255,255,255,1]
alphaboolean是否透明,默认false

裁剪画布

php
$poster->crop($x = 0, $y = 0, $width = 0, $height = 0);

参数说明

变量类型必填注释
xnumber裁剪x起始点
ynumber裁剪y起始点
widthnumber裁剪宽度
heightnumber裁剪高度

创建背景、遮罩

注意:Imagick 方式,背景目前支持做圆角,渐变色可以支持多种,方向只支持上下

php
// 背景 rgba 参数解释
// color 颜色数组取值范围 0-255
// alpha 透明度范围 1-127
// to 颜色渐变方向取值范围 bottom、 top、 left、 right、 left top、 right top、 left bottom、 right bottom 默认 bottom
// 单色:['color'=>[[0-255,0-255,0-255]],'alpha'=>1-127]
// 多色渐变:['color'=>[[0-255,0-255,0-255], [0-255,0-255,0-255]],'alpha'=>1-127, 'to'=>'left']
// radius string|array|integer 圆角 默认0 ( '10 20', [10, 20, 30], 10)
// [20] 四个角
// [20,30] 第一个值 左上 右下 第二个值 右上 左下
// [20,30,20] 第一个值 左上 第二个值 右上 左下 第三个值 右下
// [20,30,20,10]  左上 右上 右下  左下
$poster->buildBg(400,526,[
    'color'=>[
        [0,0,162], 
        [0,255,162], 
        [255,255,162], 
        [255, 0, 0], 
        [0, 255, 0]
    ], 
    'alpha'=>50, 
    'to'=>'bottom', 
    'radius'=>'10'
], true, ['center', -10], ['center', 10], 0, 0 , function($im){         
    $im->buildText('明月几时有,把酒问青天。不知天上宫阙,今夕是何年。','center',100,20,[255, 255, 255, 50]);     
}); # 创建画布

参数说明

变量类型必填注释
wnumber画布宽
hnumber画布高
rgbaarray详见上方注释
alphaboolean是否透明,是:true
dst_xnumber|string|array画布位置x 特殊值 center 居中,居中并向左偏移 ['center',-5], 居中并向右偏移 ['center',5]; 支持百分比20% 支持自定义 支持正负
dst_ynumber|string|array画布位置y 特殊值 center 居中,居中并向上偏移 ['center',-5], 居中并向下偏移 ['center',5]; 支持百分比20% 支持自定义 支持正负
src_xnumber图片x轴,默认0
src_ynumber图片y轴,默认0
funcclosure匿名函数(闭包),可以已当前背景为基础合成相应的内容

合成图片

php
/**
 * 合成图片
 * @param string|array $src  图片地址,旋转角度
 */
$poster->buildImage($src,$dst_x,$dst_y,$src_x,$src_y,$src_w,$src_h,$alpha,$type); # 合成图片

注意:旋转角度是顺时针

参数说明

变量类型必填注释
srcstring|array['src'=> $src, 'angle'=> $angle]
$src: 路径,支持网络图片(带http或https)
$angle: 旋转角度,顺时针旋转
dst_xnumber|string画布位置x 特殊值 center 居中,居中并向左偏移 ['center',-5], 居中并向右偏移 ['center',5]; 支持百分比20% 支持自定义 支持正负
dst_ynumber|string画布位置y 特殊值 center 居中,居中并向上偏移 ['center',-5], 居中并向下偏移 ['center',5]; 支持百分比20% 支持自定义 支持正负
src_xnumber图片x轴,默认0
src_ynumber图片y轴,默认0
src_wnumber图片自定义宽,默认原宽
src_hnumber图片自定义高,默认原高
alphaboolean是否透明,true:是
typestring图片变形类型,正常形状:'normal',圆形:'circle' ,默认normal

批量合成图片

php
$images = [
    [
        'src'   => $src,
        'dst_x' => $dst_x,
        'dst_y' => $dst_y,
        'src_x' => $src_x,
        'src_y' => $src_y,
        'src_w' => $src_w,
        'src_h' => $src_h,
        'alpha' => $alpha,
        'type'  => $type
    ]
];
$poster->buildImageMany($images); # 批量合成图片

参数说明:与合成图片参数一致。

合成二维码

php
$poster->buildQr($text,$dst_x,$dst_y,$src_x,$src_y,$src_w,$src_h,$size,$margin); # 合成二维码

参数说明

变量类型必填注释
textstring内容,例如:http://www.520yummy.com
dst_xnumber|string画布位置x 特殊值 center 居中,居中并向左偏移 ['center',-5], 居中并向右偏移 ['center',5]; 支持百分比20% 支持自定义 支持正负
dst_ynumber|string画布位置y 特殊值 center 居中,居中并向上偏移 ['center',-5], 居中并向下偏移 ['center',5]; 支持百分比20% 支持自定义 支持正负
src_xnumber图片x轴,默认0
src_ynumber图片y轴,默认0
src_wnumber图片自定义宽,默认原宽
src_hnumber图片自定义高,默认原高
alphainteger大小,默认4
margininteger白边大小,默认1

批量合成二维码

php
$qrs = [
    [
        'text'   => $text,
        'dst_x'  => $dst_x,
        'dst_y'  => $dst_y,
        'src_x'  => $src_x,
        'src_y'  => $src_y,
        'src_w'  => $src_w,
        'src_h'  => $src_h,
        'size'   => $size,
        'margin' => $margin
    ]
];
$poster->buildQrMany($qrs); # 批量合成二维码

参数说明:与合成二维码参数一致。

合成线段

php
$poster->buildLine($x1, $y1, $x2, $y2, $rgba, $type, $weight);
变量类型可选值必填注释
x1int起始点x坐标
y1int起始点y坐标
x2int结束点x坐标
y2int结束点y坐标
rgbaarray默认透明
typestring默认line ( rectangle | filled_rectangle )默认线, rectangle 矩形, filled_rectangle 矩形并填充
weightint默认1粗细

合成圆弧

php
$poster->buildArc($cx, $cy, $w, $h, $s, $e, $rgba, $type, $style, $weight);
变量类型可选值必填注释
cxint原点x坐标
cyint原点y坐标
wint圆宽度
hint圆高度
sint起始点角度
eint结束点角度
rgbaarray否否默认透明
typestring默认圆弧 ( filled_arc )默认圆弧线, filled_arc 圆弧并填充
stylestring默认**IMG_ARC_PIEIMG_ARC_PIE**IMG_ARC_CHORDIMG_ARC_NOFILL
weightint默认1粗细

合成文字

php
// 可以用span标签的style color属性改变文字颜色,可用br标签换行
$content = [
    'type' => 'html',
    'content' => '我是<br><span style="color:#fF8716;">某某</span>,<br/>我在<span style="color:#fF8716;">贵阳</span><br>我为家乡助力<br>我用<span style="color:#fF8716;">poster</span>生成海报图片'
];

$poster->buildText($content,$dst_x,$dst_y,$font,$rgba,$max_w,$font_family,$weight,$space); # 合成文字

// 新增文字宽度定位
$dst_x = "center | left | right"; // 特殊值:center 居中 left 靠左 right 靠右
$dst_x = ['center', 10]; // 居中且向右偏移10 负数向左偏移
$dst_x = ['left', 10]; // 靠左且向右偏移10 负数向左偏移
$dst_x = ['right', 10]; // 靠左且向右偏移10 负数向左偏移
// 自定义宽度定位, center left right 
$dst_x = ['custom', 'center', 100, 200, 0]; // 在图像对象的x坐标100到200之间居中, 偏移0

// 新增文字高度定位
$dst_y = "center | top | bottom"; // 特殊值:center 居中 top 靠顶 bottom 靠底
$dst_y = ['center', 10]; // 居中且向下偏移10 负数向上偏移
$dst_y = ['top', 10]; // 靠顶且向下偏移10 负数向上偏移
$dst_y = ['bottom', 10]; // 靠底且向下偏移10 负数向上偏移
// 自定义高度定位, center top bottom 
$dst_y = ['custom', 'center', 100, 200, 0]; // 在图像对象的y坐标100到200之间居中, 偏移0

参数说明

变量类型必填注释
contentstring内容,例如:http://www.520yummy.com
dst_xnumber|string|array画布位置x ;特殊值 center 居中;居中并向左偏移 ['center',-5], 居中并向右偏移 ['center',5],上面注释
dst_ynumber画布位置y,默认0
fontnumber字体大小,默认16
rgbaarray颜色rbga,[255,255,255,1]
max_wnumber最大换行宽度,默认0不换行。达到换行宽度自动换行
font_familystring字体,可不填,有默认 (相对路径为项目根目录)
weightinteger字体粗细 默认字体大小
spaceinteger字体间距 默认无
angleinteger旋转角度

批量合成文字

php
$texts = [
    [
        'content'     => $content,
        'dst_x'       => $dst_x,
        'dst_y'       => $dst_y,
        'font'        => $font,
        'rgba'        => $rgba,
        'max_w'       => $max_w,
        'font_family' => $font_family,
        'weight'      => $weight,
        'space'       => $space,
        'angle'       => $angle,
    ]
];
$poster->buildTextMany($texts); # 批量合成文字

参数说明:与合成文字参数一致。

获取海报

php
$poster->getPoster($path = ''); # 获取合成后图片文件地址

返回说明:返回数组,返回文件地址。

处理海报、图片

php
$poster->setPoster(); # 处理图片,需要传原图片

返回说明:处理原图片资源,无返回。

输出图片流

php
$poster->stream(); # 输出图片流

返回说明:返回文件流,可输出到浏览器或img标签。

获取base64

php
$poster->baseData(); # 返回base64

返回说明:返回base64,不保留在服务器直接使用。

获取二进制流

php
$poster->blob(); # 返回二进制流

返回说明:返回二进制流。

获取画布

php
$poster->getIm();

返回说明:返回Im对象。

获取Im属性

php
$poster->getImInfo();

返回说明:返回Im属性。

临时文件

php
$poster->tmp();

返回说明:保存并返回临时文件。

生成二维码

php
$qr = Poster::Qr('http://www.520yummy.com','poster/1.png'); # 生成二维码

参数说明

变量类型必填注释
textstring二维码包含的内容,可以是链接、文字、json字符串等等,例如:http://www.520yummy.com
outfileboolean|string默认为false,不生成文件,只将二维码图片返回输出;否则需要给出存放生成二维码图片的文件名及路径
levelstring容错级别,默认为L, 可传递的值分别是L(QR_ECLEVEL_L,7%)、M(QR_ECLEVEL_M,15%)、Q(QR_ECLEVEL_Q,25%)、H(QR_ECLEVEL_H,30%)。这个参数控制二维码容错率,不同的参数表示二维码可被覆盖的区域百分比,也就是被覆盖的区域还能识别
sizeinteger控制生成图片的大小,默认为4
margininteger控制生成二维码的空白区域大小,默认4
saveandprintboolean保存二维码图片并显示出来,outfile 必须传递图片路径,默认false

返回说明:outfile 为空,输出二维码图片,不生成文件;否则返回图片路径。

微不足道,绵薄之力