Laravel + ThinkPhP 海报生成

相关资料:

小程序 | EasyWeChat

二维码生成

php 复制代码
public function test(){
  $config = [
    'app_id' => 'appid',
    'secret' => 'secret',
  ];

  $app = Factory::miniProgram($config);
  
  $response = $app->app_code->get('pages/index/index', [
    //二维码大小
    'width' => 150,
     //二维码颜色
    'line_color' => [
      'r' => 255,
      'g' => 255,
      'b' => 0,
    ],
  ]);

  if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
    //保存二维码
    $filename = $response->saveAs('qrcode', '123.png');
    //海报生成等逻辑写这个位置
    //...
    return $filename;
  }
}

海报生成

二维码生成好了之后,把二维码贴在海报的上面

php 复制代码
//首先我们读取二维码和海报背景图片
            
$posterImage = imagecreatefromjpeg('qrcode/bg.jpg'); //海报
$qrcodeImage = imagecreatefromjpeg('qrcode/123.jpg'); //小程序二维码
//字体
$fontFile = 'qrcode/ziti.ttf';
//文本颜色
$textColor = imagecolorallocate($posterImage, 126, 57, 26);
//字体大小
$textSize = 36;

$data = [
  //第一行文本 和 位置
  ['text' => '你好', 'x' => 110, 'y' => 600]
  //text 文本
  //x 左侧距离
  //y 头部距离
];
//海报上面写字
foreach ($data as $index => $textData) {
  $text = $textData['text'];
  $textX = $textData['x'];
  $textY = $textData['y'];
  imagettftext($posterImage, $textSize, 0, $textX, $textY, $textColor, $fontFile, $text);
}

//获取小程序二维码和海报图片的大小
            
$qrcodeWidth = imagesx($qrcodeImage);
$qrcodeHeight = imagesy($qrcodeImage);
            
//二维码大小
$qrcodeSize = 260; 
//二维码左侧距离
//手动设置用这个代码
// $qrcodeX = 90;
// 自动剧中用这个代码
//获取海报图片的宽度
$posterWidth = imagesx($posterImage);
//自动剧中
$qrcodeX = ($posterWidth - $qrcodeWidth) / 2;
//二维码头部距离
$qrcodeY = 1240;
//复制海报
imagecopyresampled($posterImage, $qrcodeImage, $qrcodeX, $qrcodeY, 0, 0, $qrcodeSize, $qrcodeSize, $qrcodeWidth, $qrcodeHeight);
//  如果还有图片要合并
$image3 = imagecreatefromjpeg('https://www.example.cn/zb1227/zx/201909/W020190909513625276746.jpg');
//  宽度
$newWidth = 900;
//  高度
$newHeight = 600;
// 调整图片尺寸
$resizedImage = imagescale($image3, $newWidth, $newHeight);
// 将调整后的图片复制到海报上
imagecopy($posterImage, $resizedImage, 90, 250, 0, 0, $newWidth, $newHeight);

// 海报保存
$outputPath = 'qrcode/poster.jpg';
imagejpeg($posterImage, $outputPath);

// 工作完毕
imagedestroy($posterImage);
imagedestroy($qrcodeImage);

return response()->json(['status'=>'success','msg'=>'海报生成成功']);

二维码生成成功

相关推荐
leo__5207 小时前
在Ubuntu 22.04系统中无需重启设置静态IP地址
tcp/ip·ubuntu·php
爱隐身的官人12 小时前
ctfshow - web - 命令执行漏洞总结(二)
php·命令执行
悠悠~飘1 天前
php简介(第一天打卡)
php
久绊A1 天前
Hydra-SSH 破解安全防范
开发语言·php
黑白世界46481 天前
开源分享: php-tools php gui的一次尝试
后端·php
2503_928411561 天前
9.8 ajax+php基础语法
ajax·okhttp·php
普通网友2 天前
支持二次开发的代练App源码:订单管理、代练监控、安全护航功能齐全,一站式解决代练护航平台源码(PHP+ Uni-app)
安全·uni-app·php
yenggd2 天前
centos系统apache支持php配置
centos·php·apache
ytttr8732 天前
PHP中各种超全局变量使用的过程
开发语言·php
名誉寒冰2 天前
TCP, 三次握手, 四次挥手, 滑动窗口, 快速重传, 拥塞控制, 半连接队列, RST, SYN, ACK
网络·tcp/ip·php