PHP 使用GD庫合成帶二維碼和圓形頭像的海報步驟以及源碼實現
之前記錄過一篇文章,不過那只是簡單將二維碼合成到海報中去,這次還要合成頭像,而且是圓形。所需要素材就不一一列舉,直接代碼吧
1、先獲取用戶頭像
有的用戶是自定義頭像(自定義頭像是其他站點),有的用戶是小程序頭像
1 if (!$user['logo_status'] && $user['logo']) { 2 $app_domain = config('app_url');//因為自己上傳的頭像都放在了小程序的后台上
3 $user['avatarurl'] = $app_domain.$user['logo']; 4 //$headurl = getcwd().'/upload/logo/30f792c66405eb51304086cec6025b48.png';
5 } 6 $img_file = curl_file_get_contents($user['avatarurl']); //小程序傳的頭像是網絡地址需要周轉一下 7 //$img_content_logo= base64_encode($img_file);
8 $file_tou_name = time().".png"; 9 $headurl = getcwd().'/upload/logo/'.$file_tou_name; 10 file_put_contents($headurl,$img_file);
2、將二維碼縮小到206*206,微信二維碼默認尺寸是430*430
1 //1、首先將二維碼縮小成206*206
2 $thumb = imagecreatetruecolor(206,206); //創建一個新的畫布(縮放后的),從左上角開始填充透明背景
3 $img_content = $this->get_resource_by_img(getcwd().$qrcode);//雖然保存二維碼文件的時候,以后綴.png命名,但是格式確實jpg格式的圖片
4 imagecopyresampled($thumb, $img_content, 0, 0, 0, 0, 206, 206, 430, 430); 5 imagedestroy($img_content);
3、將二維碼合成到海報中,前提海報尺寸為750*909,
1 //$arr = getimagesize(getcwd().$promote['value']); 2 //if($arr[2] != 2) com_out_fail('海報資源格式不正確,請聯系后台管理員更換'); 3 //$imgs = imagecreatefromjpeg(getcwd().$promote['value']);
4 $imgs = $this->get_resource_by_img(getcwd().$promote['value']);//暫時海報資源是png格式的
5 imagecopy($imgs, $thumb, 272, 529, 0, 0, 206, 206);
4、將頭像轉換成圓形,再裁剪為132*132,其實小程序頭像默認尺寸就是132*132的,這里主要是兼容自定義頭像
1 //將用戶頭像先轉換成圓形,再合成到海報中
2 list($imgg,$w) = $this->yuan_img($headurl);//yuan_img() 方法在文末會列出 3 //$file_name = "2_".time().".png"; 4 //imagepng($imgg,getcwd().'/upload/logo/'.$file_name); 5 //裁剪為132*132的
6 $imgg = $this->get_new_size($imgg,132,132,$w);//小程序頭像其實不用裁剪,小程序頭像本身就是132*132的,不過文檔好像沒更新 7 //$file_name = "4_".time().".png"; 8 //imagepng($imgg,getcwd().'/upload/logo/'.$file_name); 9 //imagedestroy($imgg); 10 //$logo = imagecreatefrompng(getcwd().'/upload/logo/'.$file_name);
11 imagecopy($imgs, $imgg, 309, 20, 0, 0, 132, 132);
5、最后保存生成的海報,避免重復生成,只需要在合適的機會重新生成就行
1 //最后、保存到服務器
2 $promote_img = '/upload/promote_img/'.$this->id."_promote_img.png"; 3 imagepng($imgs,getcwd().$promote_img); //保存
4 imagedestroy($imgs); 5 imagedestroy($thumb); 6 imagedestroy($imgg); 7 //刪除多余圖片文件
8 unlink($headurl); 9 //4、入庫
10 User::where('id',$this->id)->update([ 11 'promote_img'=>$promote_img,
12 'promote_img_status'=>1,
13 ]);
用到的方法如下
1 /*
2 * 通過curl獲取數據,而不是發送請求,比file_get_contents效率高 3 */
4 function curl_file_get_contents($durl) 5 { 6 $ch = curl_init(); 7 curl_setopt($ch, CURLOPT_URL, $durl); 8 curl_setopt($ch, CURLOPT_TIMEOUT, 5); 9 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 10 $r = curl_exec($ch); 11 curl_close($ch); 12 return $r; 13 }
1 /*
2 * 轉換為圓形 3 */
4 private function yuan_img($imgpath) 5 { 6 $wh = getimagesize($imgpath);//pathinfo()不准
7 $src_img = null; 8 switch ($wh[2]) { 9 case 1:
10 //gif
11 $src_img = imagecreatefromgif($imgpath); 12 break; 13 case 2:
14 //jpg
15 $src_img = imagecreatefromjpeg($imgpath); 16 break; 17 case 3:
18 //png
19 $src_img = imagecreatefrompng($imgpath); 20 break; 21 } 22 $w = $wh[0]; 23 $h = $wh[1]; 24 $w = min($w, $h); 25 $h = $w; 26 $img = imagecreatetruecolor($w, $h); 27 //這一句一定要有
28 imagesavealpha($img, true); 29 //拾取一個完全透明的顏色,最后一個參數127為全透明
30 $bg = imagecolorallocatealpha($img, 255, 255, 255, 127); 31 imagefill($img, 0, 0, $bg); 32 $r = $w / 2; //圓半徑
33 $y_x = $r; //圓心X坐標
34 $y_y = $r; //圓心Y坐標
35 for ($x = 0; $x < $w; $x++) { 36 for ($y = 0; $y < $h; $y++) { 37 $rgbColor = imagecolorat($src_img, $x, $y); 38 if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) { 39 imagesetpixel($img, $x, $y, $rgbColor); 40 } 41 } 42 } 43 return [$img,$w]; 44 } 45
46 /*
47 * 根據指定尺寸裁剪目標圖片,這里統一轉成132*132的 48 * 注意第一個參數,為了簡便,直接傳遞的是圖片資源,如果是絕對地址圖片路徑,可以加以改造 49 */
50 private function get_new_size($imgpath,$new_width,$new_height,$w) 51 { 52 $image_p = imagecreatetruecolor($new_width, $new_height);//新畫布
53 $bg = imagecolorallocatealpha($image_p, 255, 255, 255, 127); 54 imagefill($image_p, 0, 0, $bg); 55 imagecopyresampled($image_p, $imgpath, 0, 0, 0, 0, $new_width, $new_height, $w, $w); 56 return $image_p; 57 } 58
59 /*
60 * 根據絕對路徑的圖片地址獲取對應的圖片資源, 61 */
62 private function get_resource_by_img($img) 63 { 64 $wh = getimagesize($img);//比pathinfo要准
65 $src_img = null; 66 switch ($wh[2]) { 67 case 1:
68 //gif
69 $src_img = imagecreatefromgif($img); 70 break; 71 case 2:
72 //jpg
73 $src_img = imagecreatefromjpeg($img); 74 break; 75 case 3:
76 //png
77 $src_img = imagecreatefrompng($img); 78 break; 79 } 80 return $src_img; 81 }
最后來一張,合成的效果圖: