記錄一下PHP生成小程式海報(含轉圓角的微信頭像和小程式二維碼)
阿新 • • 發佈:2021-02-05
技術標籤:php
記錄一下PHP 生成帶小程式二維碼的海報
- 含文字 微信頭像(轉圓角)
- 生成小程式二維碼的
- 本文只是記錄了樣例,實際開發還需根據業務邏輯自行處理
<?php
/**
* @param $user (使用者資訊)
* 生成分享圖片
*/
public function img_sc($user){
$img =$this->yuan_img($user->avatar);//微信頭像轉圓形
//載入圓形的微信頭像
$imgg=imagecreatefrompng( $_SERVER['DOCUMENT_ROOT' ].$img);
list($l_w,$l_h) = getimagesize($user->avatar);
// 從現在檔案載入一幅影象 背景圖
$im = imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT'].'/2.jpg');
// 從現在檔案載入一幅影象 二維碼圖片
//獲取微信二維碼圖片
$wx_code=$this->wx_code($user->id,'/pages/index/login/login');
$codeIm = imagecreatefromjpeg ($_SERVER['DOCUMENT_ROOT'].$wx_code);
// 獲取圖片的寬、高
list($w, $h) = getimagesize($_SERVER['DOCUMENT_ROOT'].$wx_code);
//設定字
$boldFontFile = $_SERVER['DOCUMENT_ROOT'] . '/stsong.ttf';
// 為畫布分配一個顏色,主要用於寫文字
$font_color_1 = imagecolorallocate($im, 255, 148, 44);
$title = $user ->nick_name;
$dibu='長按識別二維碼 進入小程式學習課程';
// 向畫布中寫檔案
//暱稱
imagettftext($im, 26, 0, $x, 450, $font_color_1, $boldFontFile, $title);
//畫板
imagecopyresized($im, $codeIm, 135, 800, 0, 0, 300, 300, $w, $h);
imagecopyresized($im, $imgg, 235, 285, 0, 0, 120, 120, $l_w, $l_h);
imagettftext($im, 12, 0, 170, 1120, $font_color_1, $boldFontFile, $dibu);
// 輸出圖片
header('content-type: image/png');
$IM=imagepng ($im);
die;
}
/*
* @fun 圖片轉換成圓形png,傳入源路徑和轉換後的路徑,均用相對於當前程式檔案的路徑
* @memo 對於非正方形的圖片,以短邊作為圖片的直徑
* @param string $src 源路徑
* @param string $dst 轉換後的路徑
*/
public function yuan_img($src) {
//獲取原圖尺寸,並設定新圖片的寬度和高度
list($w, $h) = getimagesize($src);
if( $w > $h ){
$w = $h;
}else{
$h = $w;
}
$oimgSrc = imagecreatefromstring(file_get_contents($src));
$oimgDst = imagecreatetruecolor($w, $h);
imagealphablending($oimgDst,false);
$transparent = imagecolorallocatealpha($oimgDst, 0, 0, 0, 127);
$r=$w/2;
for($x=0;$x<$w;$x++){
for($y=0;$y<$h;$y++){
$c = imagecolorat($oimgSrc,$x,$y);
$_x = $x - $w/2;
$_y = $y - $h/2;
if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){
imagesetpixel($oimgDst,$x,$y,$c);
}else{
imagesetpixel($oimgDst,$x,$y,$transparent);
}
}
}
$str = uniqid(mt_rand(),1);
$file='/uploads/user/wx_tx/'.date('Ymd',time()).'/';
$file2=$_SERVER['DOCUMENT_ROOT'].$file;
if(!is_dir($file2))//檢測目錄是否存在
{
mkdir($file2,0777,true);
}
imagesavealpha($oimgDst, true);
imagepng($oimgDst, $file2.md5($str).'.png');
imagedestroy($oimgDst);
imagedestroy($oimgSrc);
return $file.md5($str).'.png';
}
//獲取微信生成二維碼
public function wx_code($user_id,$JumpUrl){
$appid ='此處填寫微信的appid';
$secret ='此處填寫微信的AppSecret';
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
//開啟session
// 儲存2小時
$lifeTime = 2 * 3600;
setcookie(session_name(), session_id(), time() + $lifeTime, "/");
if(empty($access_token)){
$access_token_data = $this->getJson($url);
$access_token = $access_token_data['access_token'];
$_SESSION['access_token'] = $access_token;
}
$access_token = $_SESSION['access_token'];
if(!empty($access_token)){
$url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token;
$data['path'] = $JumpUrl;
$data['scene'] =$user_id;//(string型別,必須是數字)
$data['width'] = 430;
$result = $this->curlPost($url,$data,'POST');
$str = uniqid(mt_rand(),1);
$file='/uploads/user/qr_code/'.date('Ymd',time()).'/';
$file2=$_SERVER['DOCUMENT_ROOT'].$file;
if(!is_dir($file2))//檢測目錄是否存在
{
mkdir($file2,0777,true);
}
file_put_contents($file2.$user_id.md5($str).'.png', $result, true);
return $file.$user_id.md5($str).'.png';
}
}
//封裝curl請求
private function curlPost($url,$data,$method){
$ch = curl_init(); //1.初始化
curl_setopt($ch, CURLOPT_URL, $url); //2.請求地址
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.請求方式
//4.引數如下
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模擬瀏覽器
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解壓內容
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
if($method=="POST"){//5.post方式的時候新增資料
$data = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);//6.執行
if (curl_errno($ch)) {//7.如果出錯
return curl_error($ch);
}
curl_close($ch);//8.關閉
return $tmpInfo;
}
?>