## 生成用户分享分销二维码 #### 需要 ```bash $ composer require endroid/qrcode ``` #### 代码片 ```php /** * @api {GET} user/qrcode 获取二维码 * @apiGroup 用户 * @apiParam {integer} uid 用户id * @apiParam {bool} marge_background=0 是否合成背景 (1=带背景, 0=纯二维码) * @apiDescription 返回图片数据 (字节集) */ public function qrcode($uid, $marge_background = false) { $url = url('index/index/index', false, false, true) . "?pid={$uid}"; $qrCode = new QrCode($url); header('Content-Type: ' . $qrCode->getContentType()); if ($marge_background) { echo $qrCode->writeString(); exit; } //背景图片 $dst = env('ROOT_PATH') . "public/uploads/image/qrcode.jpg"; //得到原始图片信息 $dst_im = imagecreatefromjpeg($dst); $src_im = imagecreatefromstring($qrCode->writeString()); //水印透明度 $alpha = 90; //3.使用固定的公式计算新的宽高 $sx = 300; $sy = 300; //4.生成目标图像资源 $small = imagecreatetruecolor($sx, $sy); imagecopyresampled($small, $src_im, 0, 0, 0, 0, $sx, $sy, imagesx($src_im), imagesy($src_im)); //合并水印图片 imagecopymerge($dst_im, $small, 230, 647, 0, 0, $sx, $sy, $alpha); //输出合并后水印图片 imagejpeg($dst_im); imagedestroy($dst_im); imagedestroy($src_im); exit; } ```