主要代碼如下,可直接復制調試參數:
//查詢當前用戶是否有分銷海報public function user_poster(){$this->checkAuth();//查詢會員信息$user = $this->getUserInfoById($this->user_id);if(!empty($user['distribution_img'])){$result['data'] = $user['distribution_img'];}else{$result = $this->getDistributorImg();}$this->success("請求成功!", $result);}//獲取小程序分享二維碼--分銷海報public function getDistributorImg(){try{//查詢會員信息$userinfo = $this->getUserInfoById($this->user_id);$url = $this->request->get('url');$save_dir = 'upload/qr_code/user_share/';$save_key = 'uid'.$userinfo['id'];$img_src = md5($save_key) . '.png';$poster_config = $this->userShareConfig();$poster_url = root_path().'public/'.$save_dir . $img_src;//生成小程序二維碼$res = $this->makeUserMnpQrcode($userinfo,$save_key,$img_src);if(true !== $res){return ['status' => 0, 'msg' => '微信配置錯誤:'.$res, 'data' => ''];}$background_img = root_path().'public/images/share_user_bg.png';$user_avatar = root_path().'public/images/default_logo.jpg';$qr_code_logic = new MemberController();//獲取背景圖$share_background_img = imagecreatefromstring(file_get_contents($background_img));//合成頭像$qr_code_logic->writeImg($share_background_img, $user_avatar, $poster_config['head_pic'],true);//合成昵稱$nickname = self::filterEmoji($userinfo['nickname']);$qr_code_logic->writeText($share_background_img, $nickname, $poster_config['nickname']);//長按識別$notice = '長按識別二維碼 >>';$qr_code_logic->writeText($share_background_img, $notice, $poster_config['notice']);//合成商品標題$title = self::auto_adapt($poster_config['title']['font_size'], 0, $poster_config['title']['font_face'], '邀請你一起來賺大錢', $poster_config['title']['w'],$poster_config['title']['y'],getimagesize($background_img));$qr_code_logic->writeText($share_background_img, $title, $poster_config['title']);//邀請碼$qr_code_logic->writeText($share_background_img, '邀請碼 '.$userinfo['distribution_code'], $poster_config['code_text']);//合成二維碼$qr_code_logic->writeImg($share_background_img, $poster_url, $poster_config['qr'],false);imagepng($share_background_img, $poster_url);$file_name = 'public/'.$save_dir . $img_src;$result['data'] = $file_name;$dis_oss_img = $this->local_upload($result['data']);//更新會員分銷海報信息$dis_oss_img = cmf_get_asset_url($dis_oss_img);$this->up_member(['distribution_img' => $dis_oss_img]);//上傳oss$result['data'] = $dis_oss_img;return $result;}catch(Exception $e){return ['status' => 0, 'msg' => $e->getMessage(), 'data' => ''];}}public function up_member($params){$this->checkAuth();$this->MemberModel = new MemberModel();$params['update_time'] = time();$member = $this->getUserInfoByOpenid($this->openid);if (empty($member)) $this->error("該會員不存在!");$result = $this->MemberModel->where('id', $member['id'])->strict(false)->update($params);if ($result) {$result = $this->getUserInfoById($this->user_id);return true;} else {return false;}}
需要引用的方法
/** 生成海報自動適應標題*/
public function auto_adapt($size, $angle = 0, $fontfile, $string, $width, $height, $bg_height)
{$content = "";// 將字符串拆分成一個個單字 保存到數組 letter 中for ($i = 0; $i < mb_strlen($string); $i++) {$letters[] = mb_substr($string, $i, 1);}foreach ($letters as $letter) {$str = $content . " " . $letter;$box = imagettfbbox($size, $angle, $fontfile, $str);$total_height = $box[1] + $height;if ($bg_height[1] - $total_height < $size) {break;}//右下角X位置,判斷拼接后的字符串是否超過預設的寬度if (($box[2] > $width) && ($content !== "")) {if ($bg_height[1] - $total_height < $size * 2) {break;}$content .= "\n";}$content .= $letter;}return $content;
}//寫入文字public function writeText($poster, $text, $config){$font_uri = $config['font_face'];$font_size = $config['font_size'];$color = substr($config['color'],1);//顏色轉換$color= str_split($color, 2);$color = array_map('hexdec', $color);if (empty($color[3]) || $color[3] > 127) {$color[3] = 0;}//寫入文字$font_col = imagecolorallocatealpha($poster, $color[0], $color[1], $color[2], $color[3]);imagettftext($poster, $font_size,0, $config['x'], $config['y'], $font_col, $font_uri, $text);return $poster;}
/*** Notes:去掉名稱中的表情* @param $str* @return string|string[]|null* @author: cjhao 2021/3/29 15:56*/
public function filterEmoji($str)
{$str = preg_replace_callback('/./u',function (array $match) {return strlen($match[0]) >= 4 ? '' : $match[0];},$str);return $str;
}public function makeUserMnpQrcode($userinfo,$save_key,$img_src) {try {$config = cmf_get_option('weipay');$wechatC['app_id'] = $config['wx_mini_app_id'];$wechatC['secret'] = $config['wx_mini_app_secret'];$app = Factory::miniProgram($wechatC);$code = $userinfo['distribution_code']; //邀請碼$response = $app->app_code->get('pages/index/index'.'?invite_code='.$code, ['width' => 170,]);if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {$response->saveAs('upload/qr_code/user_share/', $img_src);return true;}if(isset($response['errcode']) && 41030 === $response['errcode']){return '商城小程序碼,先提交審核并通過';}return $response['errmsg'];} catch (\EasyWeChat\Kernel\Exceptions\Exception $e){return $e->getMessage();}}//寫入圖片public function writeImg($poster, $img_uri, $config, $is_rounded = false){$pic_img = imagecreatefromstring(file_get_contents($img_uri));$is_rounded?$pic_img = self::rounded_corner($pic_img):'';//切成圓角返回頭像資源$pic_w = imagesx($pic_img);$pic_h = imagesy($pic_img);//圓形頭像大圖合并到海報imagecopyresampled($poster, $pic_img,$config['x'],$config['y'],0, 0,$config['w'],$config['h'],$pic_w,$pic_h);return $poster;}/*** 將圖片切成圓角*/
public function rounded_corner($src_img)
{$w = imagesx($src_img);//微信頭像寬度 正方形的$h = imagesy($src_img);//微信頭像寬度 正方形的$w = min($w, $h);$h = $w;$img = imagecreatetruecolor($w, $h);//這一句一定要有imagesavealpha($img, true);//拾取一個完全透明的顏色,最后一個參數127為全透明$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);imagefill($img, 0, 0, $bg);$r = $w / 2; //圓半徑
// $y_x = $r; //圓心X坐標
// $y_y = $r; //圓心Y坐標for ($x = 0; $x < $w; $x++) {for ($y = 0; $y < $h; $y++) {$rgbColor = imagecolorat($src_img, $x, $y);if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {imagesetpixel($img, $x, $y, $rgbColor);}}}unset($src_img);return $img;
}//用戶圖片配置public function userShareConfig(){return [//會員頭像'head_pic' => ['w' => 80, 'h' => 80, 'x' => 30, 'y' => 680,],//會員昵稱'nickname' => ['color' => '#333333', 'font_face' => root_path().'public/font/SourceHanSansCN-Regular.otf', 'font_size' => 20, 'x' => 120, 'y' => 730,],//標題'title' => ['color' => '#333333', 'font_face' => root_path().'public/font/SourceHanSansCN-Regular.otf', 'font_size' => 20, 'w' => 360, 'x' => 30, 'y' => 810,],//提醒長按掃碼'notice' => ['color' => '#333333', 'font_face' => root_path().'public/font/SourceHanSansCN-Regular.otf', 'font_size' => 20, 'x' => 30, 'y' => 880,],//邀請碼文本'code_text' => ['color' => '#FF2C3C', 'font_face' => root_path().'public/font/SourceHanSansCN-Regular.otf', 'font_size' => 20, 'x' => 355, 'y' => 930,],//二維碼'qr' => ['w' => 170,'h' => 170, 'x' => 370, 'y' => 730,],];}
最終效果: