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'=>'海报生成成功']);

二维码生成成功

相关推荐
用户30745969820721 分钟前
PHP 扩展——从入门到理解
php
鹏仔先生18 小时前
拷贝漫画APP下载页PHP程序,后台带免费AI写作
php
云水一下1 天前
从零开始学 PHP 系列(一):PHP 的前世今生与开发环境搭建
开发语言·php
xingpanvip1 天前
星盘接口开发文档:本命盘接口指南
android·开发语言·css·php·lua
酉鬼女又兒1 天前
零基础入门计算机网络运输层:端到端通信核心作用、端口号分类规则、复用分用工作机制及UDP与TCP协议全方位对比详解
网络·网络协议·tcp/ip·计算机网络·考研·udp·php
dog2501 天前
不要再继续优化 TCP
网络协议·tcp/ip·php
Channing Lewis1 天前
PHP 解析 Excel 的那些坑:一次“行号错位”引发的数据丢失
开发语言·php·excel
Cheng小攸1 天前
渗透行为分析与检测
开发语言·php
云水一下1 天前
从零开始学 PHP 系列(六):MySQL 数据库与 PHP 交互——让数据真正“住”进服务器
数据库·mysql·php
qq_452396231 天前
第十四篇:《K8s 网络模型与 CNI 插件(Calico、Flannel、Cilium)》
网络·kubernetes·php