PHP 图片+文字+二维码生成小程序分享海报详解编程语言

本文章主要介绍了PHP 图片+文字+二维码生成小程序分享海报,具有不错的的参考价值,希望对您有所帮助,如解说有误或未考虑完全的地方,请您留言指出,谢谢!

思路:

1、请求微信接口获取一定尺寸微信二维码

2、准备海报主图,处理尺寸按比例缩放

3、准备分享语录,计算段落高度

4、生成海报:创建画布,分写别入按顺序和位置写入二维码、图片、文字等

5、保存海报

具体如下:

1、请求微信接口获取一定尺寸微信二维码

$access_token = 'jkagfgjkdahfgadhsfkdsj'; 
$url = sprintf("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=%s",$access_token); 
$postdata = '{"path": "/pages/subActivity/pages/activityDetail/activityDetail?id='.$id.'", "width": 210}'; 
 
//微信的二维码 
$image = http_post($url,$postdata);
//准备微信二维码以备生成海报使用
$wxim = imagecreatefromstring($image);
$wxim_size = 270;

function http_post($url = '', $param = '') { 
    if (empty($url) || empty($param)) { 
        return false; 
    } 
 
    $postUrl = $url; 
    $curlPost = $param; 
    $ch = curl_init();//初始化curl 
    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页 
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($ch);//运行curl curl_close($ch); return $data; }

2、准备海报主图,处理尺寸按比例缩放

$share_img = '图片路径'
$share_img_size
= getimagesize($share_img); //图片尺寸
$shareim = imagecreatefromstring(file_get_contents($share_img));
//计算原图长宽比例
$size_rate = $share_img_size[1]/$share_img_size[0];

//新图尺寸
$share_img_width = 450; $share_img_height = intval($share_img_width*$size_rate);

//复制生成新图并保存
$new_share_img = imagecreatetruecolor($share_img_width, $share_img_height);
imagecopyresampled(
$new_share_img, $shareim, 0, 0, 0, 0,$share_img_width,$share_img_height,$share_img_size[0], $share_img_size[1]); $new_share_img_filename = $dir.'/'.$id.'_new_share.jpg'; imagejpeg($new_share_img, $new_share_img_filename); imagedestroy($new_share_img);
//准备可写入新分享主图 以备生成海报使用
$new_shareim = imagecreatefromstring(file_get_contents($dir.'/'.$id.'_new_share.jpg')); unlink($new_share_img_filename); //删除临时新的分享图

3、准备分享语录文字,计算高度

 //准备分享语录 
 $content = ""; 
 // 将字符串拆分成一个个单字 保存到数组 letter 中 
 for ($i=0;$i<mb_strlen($share_title);$i++) { 
     $letter[] = mb_substr($share_title, $i, 1); 
 } 
 
 foreach ($letter as $l) { 
     $teststr = $content." ".$l; 
     $share_title_size = imagettfbbox(16, 0, $font, $teststr); 
     // 判断拼接后的字符串是否超过预设的宽度 
     if (($share_title_size[2] > $share_img_width) && ($content !== "")) { 
           $content .= "/n"; 
     } 
     $content .= $l; 
 }
 //文字高度
$share_title_height = $share_title_size[3];

4、生成海报:创建画布,分写别入按顺序和位置写入二维码、图片、文字等

//生成海报 
                
$font = FCPATH.'/styles/font/wryh.ttf';   //保证字体文件目录正确
$filepath = $dir.'/'.$id.'_p.jpg'; 
$posters_width = 510; 
$posters_height = 130 + $share_img_height+$share_title_height + 280; 
 
$newimg = imagecreatetruecolor($posters_width, $posters_height); 
$bg = imagecolorallocate($newimg, 255, 255, 255); 
imagefill($newimg,0,0,$bg); 
$black = imagecolorallocate($newimg, 0, 0, 0); 
 
//写宣传言 
imagettftext($newimg,18,0,30,47,$black,$font,'美好生活'); 
imagettftext($newimg,15,0,30,76,$black,$font,'关注美好生活了解社区好生活'); 
 
//写分享图片 
imagecopy($newimg,$new_shareim,30,95,0,0,$share_img_width,$share_img_height); 
 
//写分享标题 
imagettftext ($newimg, 15, 0, 30, $share_img_height+122, $black, $font, $content ); 
 
//写二维码 
$wxim_start_hight = 130+$share_title_height+$share_img_height; 
imagecopy($newimg,$wxim,20,$wxim_start_hight,0,0,$wxim_size,$wxim_size); 
 
 
$gray = imagecolorallocate($newimg, 136, 136, 136); 
 
//写提示语 
$tips = '长按保存海报至手机相册'; 
$tips_hight = $wxim_start_hight+200; 
imagettftext($newimg,13,0,290,$tips_hight,$gray,$font,$tips); 
 
//海报生成保存 
imagejpeg($newimg, $filepath); 
ImageDestroy($newimg); 
ImageDestroy($wxim);

整体代码

         $access_token = '请求微信接口的aceess_token'; 
if($access_token){ 
$url = sprintf("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=%s",$access_token); 
$postdata = '{"path": "/pages/subActivity/pages/activityDetail/activityDetail?id='.$id.'", "width": 210}'; 
//微信的二维码 
$image = http_post($url,$postdata); 
if(strlen($image) < 200){ //出错了 
log_message('error',"getTopicQRCodeProcessed, error : " . $image); 
//刷新token 
ls('wx_service'); 
$this->wx_service->refreshToken(); 
return false; 
} 
$dir = FCPATH.'upload/qrcode/'.date('Ym'); 
MkFolder($dir); 
$path = $dir.'/'.$id.'.jpg'; 
file_put_contents($path,$image); 
$qrcode = pathATOR($path); 
$share_title = element('share_title',$event); 
$share_img = element('share_img_moment',$event) ? : element('cover',$event); 
$share_img = get_oss_file_url($share_img); 
$font = FCPATH.'/styles/font/wryh.ttf'; 
//准备分享图片  先处理图片尺寸 
$share_img_size = getimagesize($share_img); 
$shareim = imagecreatefromstring(file_get_contents($share_img)); 
$size_rate = $share_img_size[1]/$share_img_size[0]; 
$share_img_width = 450; 
$share_img_height = intval($share_img_width*$size_rate); 
$new_share_img = imagecreatetruecolor($share_img_width, $share_img_height); 
imagecopyresampled($new_share_img, $shareim, 0, 0, 0, 0,$share_img_width,$share_img_height,$share_img_size[0], $share_img_size[1]); 
$new_share_img_filename = $dir.'/'.$id.'_new_share.jpg'; 
imagejpeg($new_share_img, $new_share_img_filename); 
imagedestroy($new_share_img); 
$new_shareim = imagecreatefromstring(file_get_contents($dir.'/'.$id.'_new_share.jpg')); 
unlink($new_share_img_filename); 
//准备分享标题 
$content = ""; 
// 将字符串拆分成一个个单字 保存到数组 letter 中 
for ($i=0;$i<mb_strlen($share_title);$i++) { 
$letter[] = mb_substr($share_title, $i, 1); 
} 
foreach ($letter as $l) { 
$teststr = $content." ".$l; 
$share_title_size = imagettfbbox(16, 0, $font, $teststr); 
// 判断拼接后的字符串是否超过预设的宽度 
if (($share_title_size[2] > $share_img_width) && ($content !== "")) { 
$content .= "/n"; 
} 
$content .= $l; 
} 
$share_title_height = $share_title_size[3]; 
//准备微信二维码 
$wxim = imagecreatefromstring($image); 
$wxim_size = 270; 
//生成海报 
$filepath = $dir.'/'.$id.'_p.jpg'; 
$posters_width = 510; 
$posters_height = 130 + $share_img_height+$share_title_height + 280; 
$newimg = imagecreatetruecolor($posters_width, $posters_height); 
$bg = imagecolorallocate($newimg, 255, 255, 255); 
imagefill($newimg,0,0,$bg); 
$black = imagecolorallocate($newimg, 0, 0, 0); 
//写宣传言 
imagettftext($newimg,18,0,30,47,$black,$font,'昌品生活'); 
imagettftext($newimg,15,0,30,76,$black,$font,'关注昌品生活了解社区好生活'); 
//写分享图片 
imagecopy($newimg,$new_shareim,30,95,0,0,$share_img_width,$share_img_height); 
//写分享标题 
imagettftext ($newimg, 15, 0, 30, $share_img_height+122, $black, $font, $content ); 
//写二维码 
$wxim_start_hight = 130+$share_title_height+$share_img_height; 
imagecopy($newimg,$wxim,20,$wxim_start_hight,0,0,$wxim_size,$wxim_size); 
$gray = imagecolorallocate($newimg, 136, 136, 136); 
//写提示语 
$tips = '长按保存海报至手机相册'; 
$tips_hight = $wxim_start_hight+200; 
imagettftext($newimg,13,0,290,$tips_hight,$gray,$font,$tips); 
//海报生成保存 
imagejpeg($newimg, $filepath); 
ImageDestroy($newimg); 
ImageDestroy($wxim); 
$posters = pathATOR($filepath);

原创文章,作者:bd101bd101,如若转载,请注明出处:https://blog.ytso.com/228233.html

(0)
上一篇 2022年1月11日
下一篇 2022年1月11日

相关推荐

发表回复

登录后才能评论