PHP code example of tegic / poster
1. Go to this page and download the library: Download tegic/poster library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tegic / poster example snippets
use Kkokk\Poster\PosterManager;
use Kkokk\Poster\Exception\Exception;
$poster = PosterManager::Poster('poster/poster_user'); # 设置保存路径和文件名
$poster->buildIm($w,$h,$rgba,$alpha); # 创建画布
$poster->buildImDst($src,$w,$h,$rgba,$alpha); # 创建指定图片为画布
$poster->buildImage($src,$dst_x,$dst_y,$src_x,$src_y,$src_w,$src_h,$alpha,$type); # 合成图片
$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); # 批量合成图片
$poster->buildQr($text,$dst_x,$dst_y,$src_x,$src_y,$src_w,$src_h,$size,$margin); # 合成二维码
$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); # 批量合成二维码
$poster->buildText($content,$dst_x,$dst_y,$font,$rgba,$max_w,$font_family,$weight,$space); # 合成文字
$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
]
];
$poster->buildQrMany($texts); # 批量合成文字
$poster->getPoster(); # 获取合成后图片文件地址
$poster->setPoster(); # 处理图片,需要传原图片
$poster->stream(); # 输出图片流
$qr = PosterManager::Poster()->Qr('http://www.520yummy.com','poster/1.png'); # 生成二维码
use Kkokk\Poster\PosterManager;
use Kkokk\Poster\Exception\Exception;
# 合成图片
try {
$addImage = "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2854425629,4097927492&fm=26&gp=0.jpg";
$result = PosterManager::Poster('poster/poster_user') //生成海报,这里写保存路径和文件名,可以指定图片后缀。默认png
->buildIm(638,826,[255,255,255,127],false)
->buildImage('https://test.acyapi.51acy.com/wechat/poster/top_bg.png')
->buildImage('https://test.acyapi.51acy.com/wechat/poster/half_circle.png',254,321)
->buildImage($addImage,253,326,0,0,131,131,false,'circle')
->buildImage('https://test.acyapi.51acy.com/wechat/poster/fengexian.png',0,655)
->buildText('苏 轼','center',477,16,[51, 51, 51,1])
->buildText('明月几时有,把酒问青天。不知天上宫阙,今夕是何年。','center',515,14,[53, 53, 53, 1])
->buildText('我欲乘风归去,又恐琼楼玉宇,高处不胜寒。','center',535,14,[53, 153, 153, 1])
->buildText('起舞弄清影,何似在人间。转朱阁,低绮户,照无眠。','center',555,14,[53, 153, 153, 1])
->buildText('不应有恨,何事长向别时圆?','center',575,14,[53, 153, 153, 1])
->buildText('人有悲欢离合,月有阴晴圆缺,此事古难全。','center',595,14,[53, 153, 153, 1])
->buildText('但愿人长久,千里共婵娟。','center',615,14,[53, 153, 153, 1])
->buildText('长按识别',497,720,15,[53, 153, 153, 1])
->buildText('查看TA的更多作品',413,757,15,[53, 153, 153, 1])
->buildQr('http://www.520yummy.com',37,692,0,0,0,0,4,1)
->getPoster();
# 批量合成
$buildImageManyArr = [
[
'src' => 'https://test.acyapi.51acy.com/wechat/poster/top_bg.png'
],
[
'src' => 'https://test.acyapi.51acy.com/wechat/poster/half_circle.png',
'dst_x' => 254,
'dst_y' => 321
],
[
'src' => 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2854425629,4097927492&fm=26&gp=0.jpg',
'dst_x' => 253,
'dst_y' => 326,
'src_x' => 0,
'src_y' => 0,
'src_w' => 131,
'src_h' => 131,
'alpha' => false,
'type' => 'circle'
],
[
'src' => 'https://test.acyapi.51acy.com/wechat/poster/fengexian.png',
'dst_x' => 0,
'dst_y' => 655
]
];
$buildTextManyArr = [
[
'content'=> '苏轼',
'dst_x' => 'center',
'dst_y' => 477,
'font' => 16,
'rgba' => [51, 51, 51, 1],
'max_w'=> 0,
'font_family' => '',
'weight' => 1,
'space'=>20
],
[
'content'=> '明月几时有,把酒问青天。不知天上宫阙,今夕是何年。',
'dst_x' => 'center',
'dst_y' => 515,
'font' => 16,
'rgba' => [51, 51, 51, 1],
'max_w'=> 0,
'font_family' => '',
'weight' => 1,
'space'=>20
],
[
'content'=> '我欲乘风归去,又恐琼楼玉宇,高处不胜寒。',
'dst_x' => 'center',
'dst_y' => 535,
'font' => 16,
'rgba' => [51, 51, 51, 1],
'max_w'=> 0,
'font_family' => '',
'weight' => 1,
'space'=>20
],
[
'content'=> '起舞弄清影,何似在人间。转朱阁,低绮户,照无眠。',
'dst_x' => 'center',
'dst_y' => 555,
'font' => 16,
'rgba' => [51, 51, 51, 1],
'max_w'=> 0,
'font_family' => '',
'weight' => 1,
'space'=>20
],
[
'content'=> '不应有恨,何事长向别时圆?',
'dst_x' => 'center',
'dst_y' => 575,
'font' => 16,
'rgba' => [51, 51, 51, 1],
'max_w'=> 0,
'font_family' => '',
'weight' => 1,
'space'=>20
],
[
'content'=> '人有悲欢离合,月有阴晴圆缺,此事古难全。',
'dst_x' => 'center',
'dst_y' => 595,
'font' => 16,
'rgba' => [51, 51, 51, 1],
'max_w'=> 0,
'font_family' => '',
'weight' => 1,
'space'=>20
],
[
'content'=> '但愿人长久,千里共婵娟。',
'dst_x' => 'center',
'dst_y' => 615,
'font' => 16,
'rgba' => [51, 51, 51, 1],
'max_w'=> 0,
'font_family' => '',
'weight' => 1,
'space'=>20
],
[
'content'=> '长按识别',
'dst_x' => 'center',
'dst_y' => 720,
'font' => 16,
'rgba' => [51, 51, 51, 1],
'max_w'=> 0,
'font_family' => '',
'weight' => 1,
'space'=>20
],
[
'content'=> '查看TA的更多作品',
'dst_x' => 'center',
'dst_y' => 757,
'font' => 16,
'rgba' => [51, 51, 51, 1],
'max_w'=> 0,
'font_family' => '',
'weight' => 1,
'space'=>20
]
];
$buildQrManyArr = [
[
'text'=>'http://www.520yummy.com',
'dst_x'=>37,
'dst_y'=>692,
'src_x'=>0,
'src_y'=>0,
'src_w'=>0,
'src_h'=>0,
'size'=>4,
'margin'=>1
],
[
'text'=>'http://www.520yummy.com',
'dst_x'=>74,
'dst_y'=>692,
'src_x'=>0,
'src_y'=>0,
'src_w'=>0,
'src_h'=>0,
'size'=>4,
'margin'=>1
]
];
$result = PosterManager::Poster('poster/poster_user')
->buildIm(638,826,[255,255,255,127],false)
->buildImageMany($buildImageManyArr)
->buildTextMany($buildImageManyArr)
->buildQrMany($buildQrManyArr)
->getPoster();
# 给图片添加水印
$setImage = "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2854425629,4097927492&fm=26&gp=0.jpg";
$result = PosterManager::Poster() //给指定图片添加水印,这里为空就好
->buildImDst(__DIR__.'/test.jpeg')
->buildImage($setImage,'-20%','-20%',0,0,0,0,false)
->setPoster();
# 生成二维码
$result = PosterManager::Poster()->Qr('http://www.baidu.com','poster/1.png');
} catch (Exception $e){
echo $e->getMessage();
}
use Kkokk\Poster\PosterManager;
use Kkokk\Poster\Exception\Exception;
# 合成图片
try {
$addImage = "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2854425629,4097927492&fm=26&gp=0.jpg";
$PosterManager = new PosterManager('poster/poster_user'); //生成海报,这里写保存路径和文件名,可以指定图片后缀。默认png
$result = $PosterManager->buildIm(638,826,255,255,255,1]27,false)
->buildIm(638,826,[255,255,255,127],false)
->buildImage('https://test.acyapi.51acy.com/wechat/poster/top_bg.png')
->buildImage('https://test.acyapi.51acy.com/wechat/poster/half_circle.png',254,321)
->buildImage($addImage,253,326,0,0,131,131,false,'circle')
->buildImage('https://test.acyapi.51acy.com/wechat/poster/fengexian.png',0,655)
->buildText('苏 轼','center',477,16,[51, 51, 51,1])
->buildText('明月几时有,把酒问青天。不知天上宫阙,今夕是何年。','center',515,14,[53, 53, 53, 1])
->buildText('我欲乘风归去,又恐琼楼玉宇,高处不胜寒。','center',535,14,[53, 153, 153, 1])
->buildText('起舞弄清影,何似在人间。转朱阁,低绮户,照无眠。','center',555,14,[53, 153, 153, 1])
->buildText('不应有恨,何事长向别时圆?','center',575,14,[53, 153, 153, 1])
->buildText('人有悲欢离合,月有阴晴圆缺,此事古难全。','center',595,14,[53, 153, 153, 1])
->buildText('但愿人长久,千里共婵娟。','center',615,14,[53, 153, 153, 1])
->buildText('长按识别',497,720,15,[53, 153, 153, 1])
->buildText('查看TA的更多作品',413,757,15,[53, 153, 153, 1])
->buildQr('http://www.520yummy.com',37,692,0,0,0,0,4,1)
->getPoster();
# 给图片添加水印
$setImage = 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2854425629,4097927492&fm=26&gp=0.jpg';
$PosterManager = new PosterManager(); //给指定图片添加水印,这里为空就好
$result = $PosterManager->buildImDst(__DIR__.'/test.jpeg')
->buildImage($setImage,'center','-20%',0,0,0,0,true)
->setPoster();
# 生成二维码
$result = $PosterManager->Qr('http://www.baidu.com','poster/1.png');
} catch (Exception $e){
echo $e->getMessage();
}