PHP海报

简介

因工作需要制作各种邀请海报,分享海报,生成签到日历海报等等。
为了方便封装了一个 composer 包,主要是用来完成,制作海报,合成图片,图片加水印,合成二维码,生成二维码等工作。
2022-12
增加了旋转验证图片生成。
增加了滑块验证图片生成。
有兴趣的小伙伴可以使用这个,确实挺方便的!
点击链接加入群聊【海报图片验证交流群】

安装

composer require kkokk/poster

更新

composer update kkokk/poster

仓库

gitee
github

文档

使用文档

示例

旋转验证

    use Kkokk\Poster\Facades\Captcha;
    use Kkokk\Poster\Exception\PosterException;

    try {
        # 旋转图片自定义参数
        $params = [
            'src'           => '',  // 背景图片,尺寸 350 * 350 正方形都可
            'im_width'      => 350, // 画布宽度
            'im_height'     => 350, // 画布高度
        ];

        $type = 'rotate';

        /**
          * 获取验证参数
          * 内部使用了 laravel 的 cache 缓存,返回的是图片的 base64 、 缓存key
          * @param string $type   验证码类型
          * @param array  $params 验证码自定义参数
          * @return arary
          */
        $data = Captcha::type($type)->config($params)->get();

        /**
          * 验证
          * 前端根据相关滑块操作进行处理, 返回旋转角度,返回 true 则验证成功
          * @param string     $key     缓存key
          * @param string|int $value   前端传回来的旋转角度
          * @param int        $leeway  误差值
          * @return boolean
          */
        $res = Captcha::type($type)->check($key, $value, $leeway);

    } catch (PosterException $e) {
        print_r($e->getMessage())
    }

滑块验证

    use Kkokk\Poster\Facades\Captcha;
    use Kkokk\Poster\Exception\PosterException;

    try {
        # 滑块自定义参数
        $params = [
            'src'           => '',  // 背景图片,尺寸 340 * 191
            'im_width'      => 340, // 画布宽度
            'im_height'     => 251, // 画布高度
            'bg_width'      => 340, // 背景宽度
            'bg_height'     => 191, // 背景高度
            'slider_width'  => 50,  // 滑块宽度
            'slider_height' => 50,  // 滑块高度
            'slider_border' => 2,   // 滑块边框
        ];

        $type = 'slider';

        /**
          * 获取滑块验证参数
          * 内部使用了 laravel 的 cache 缓存,返回的是图片的 base64 、 缓存key 、滑块高度
          * @param string $type   验证码类型
          * @param array  $params 验证码自定义参数
          * @return arary
          */
        $data = Captcha::type($type)->config($params)->get();

        /** 
          * 验证滑块
          * 前端根据相关滑块操作进行处理, 返回x坐标,返回 true 则验证成功
          * @param string     $key     缓存key
          * @param string|int $value   前端传回来的x坐标
          * @param int        $leeway  误差值
          * @return boolean
          */
        $res =  Captcha::type($type)->check($key, $value, $leeway);

    } catch (PosterException $e) {
        print_r($e->getMessage())
    }

制作海报、图片


use Kkokk\Poster\Facades\Poster;
use Kkokk\Poster\Exception\Exception;

$addImage = "https://portrait.gitee.com/uploads/avatars/user/721/2164500_langlanglang_1601019617.png";
 $result = Poster::config([
     'path' => '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)
 ->buildBg(400,526,['color'=>[[0,0,162], [0,255,162], [255,255,162], [255, 0, 0], [0, 255, 0]], 'alpha'=>50, 'to'=>'bottom'],true, 'center', 'center', 0, 0 ,
    function($im){
        $im->buildText('明月几时有,把酒问青天。不知天上宫阙,今夕是何年。','center',100,20,[255, 255, 255, 1]);
    }
)->getPoster();

# 支持批量合成
$buildImageManyArr = [];  // 多个图片
$buildTextManyArr   = []; // 多个文字
$buildQrManyArr = [];     // 多个二维码
$result = Poster::config([
     'path' => 'poster/poster_user'
 ])
 ->buildIm(638,826,[255,255,255,127],false)
 ->buildImageMany($buildImageManyArr)
 ->buildTextMany($buildImageManyArr)
 ->buildQrMany($buildQrManyArr)
 ->getPoster();

# 图片加水印
# 给图片添加水印
 $setImage = "https://portrait.gitee.com/uploads/avatars/user/721/2164500_langlanglang_1601019617.png";
 $result = Poster::buildImDst(__DIR__.'/test.jpeg')
 ->buildImage($setImage,'-20%','-20%',0,0,0,0,false)
 ->setPoster();

调用结果示例

点击

旋转 ui仿百度统计
旋转
滑块
滑块
海报
测试结果示例
背景
背景、渐变透明

本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由 阿江 于 1年前 加精
讨论数量: 4

看起来效果不错啊

1年前 评论
kkokk (楼主) 1年前

我以前用GD库也生成过海报,觉得GD库生成圆形的图像锯齿好严重。没找到什么好的解决办法 :speak_no_evil:

1年前 评论
kkokk (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!