PHP合成圖圓角

℡純斯文~發表於2020-10-28
 //分享生成二維碼合成圖普通
    public function share()
    {
        if ($this->request->isPost()) {
            $shop_id = hsc($this->request->post('shop_id'));
            $uid = hsc($this->request->post('uid'));
            if (!($shop_id && $uid)) {
                return __error('缺少引數');
            }
            $shop_info = db::name('shop_info')->alias('i')
                ->join('shop_info_diff d', 'd.shop_info_id = i.id')
                ->where('i.id', $shop_id)
                ->where(['d.is_deleted' => 1, 'd.status' => 1])
                ->field('i.name,d.price,i.image')
                ->find();
            if (empty($shop_info)) {
                return __error('商品錯誤');
            } else {
                if ($shop_info['image']) {
                    $shop_info_image = CDNDomain() . explode(',', $shop_info['image'])[0];
                } else {
                    $shop_info_image = get_url() . 'static/image/default.jpg';
                }
            }
            $member_info = db::name('member')->where(['is_deleted' => 1, 'status' => 1, 'id' => $uid])->field('nickname,head_img')->find();
            if (empty($member_info)) {
                return __error('使用者資訊錯誤');
            } else {
                if ($member_info['head_img']) {
                    $member_info_image = $member_info['head_img'];
                } else {
                    $member_info_image = get_url() . 'static/image/img.png';
                }
            }
            // Cache::set('token', 'null');
            // if (Cache::get('token')) {
            //     $access_token = Cache::get('token');
            // } else {
            $access_token = $this->getAccessToken();
            // }
            // print_r($access_token);die;
            $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
            $data = [
                'scene' => "?id=$shop_id&father_id=$uid",
                'page' => "pages/shop/detail",
                'width' => 430,
                'auto_color' => false,
            ];
            $data = json_encode($data);
            //拿到二維碼
            $result = $this->curl_post_https($url, $data);
            //把二維碼存到伺服器端
            $res = chunk_split(base64_encode($result));
            $ishas = DB::name('code')->where(['uid' => $uid, 'shop_id' => $shop_id])->field('id,code')->find();
            if (!$ishas) {
                $ishas['code'] = 'data:image/PNG;base64,' . $res;
                $arr['code'] = 'data:image/PNG;base64,' . $res;
                $arr['shop_id'] = $shop_id;
                $arr['uid'] = $uid;
                $newcode = db::name('code')->insertGetId($arr);
            }
            $has = db::name('code_photo')->where(['uid' => $uid, 'shop_id' => $shop_id])->value('code');
            if ($ishas || @$newcode) {
                $rand = time() . rand(10000, 99999);
                $pathUlr = '/static/uploads/posters/' . $uid . '/' . $rand . '.png';
                if (!is_dir(ROOT_PATH . 'public/static/uploads/posters/' . $uid)) {
                    mkdir(ROOT_PATH . 'public/static/uploads/posters/' . $uid, 0777, true);
                }
                $path = ROOT_PATH . 'public' . $pathUlr;
                if ($has) {
                    return json(['code' => '0', 'path' => $has]);
                } else {
                    //原始背景
                    $back = get_url() . '/static/image/back.png';
                    $resource = imagecreatefromstring(http_request($back));
                    //合成二維碼
                    $sub_image_c = imagecreatefromstring(base64_decode(str_replace('data:image/PNG;base64,', '', $ishas['code'])));
                    $bool = imagecopyresampled($resource, $sub_image_c, 30, 625, 0, 0, 150, 150, imagesx($sub_image_c), imagesy($sub_image_c));
                    //合使用者頭像圖
                    // print_r($member_info_image);die;
                    $sub_image_c = imagecreatefromstring(http_request($member_info_image));
                    $bool = imagecopyresampled($resource, $sub_image_c, 30, 30, 0, 0, 80, 80, imagesx($sub_image_c), imagesy($sub_image_c));
                    //合商品圖
                    $sub_image_c = imagecreatefromstring(http_request($shop_info_image));
                    $bool = imagecopyresampled($resource, $sub_image_c, 30, 140, 0, 0, 500, 280, imagesx($sub_image_c), imagesy($sub_image_c));
                    //儲存到$path
                    imagejpeg($resource, $path, 100);
                    imagedestroy($resource);
                    $this->create_text(ROOT_PATH . 'public/' . $pathUlr, $member_info['nickname'], 40, [0, 0, 0], 150, 85, ROOT_PATH . 'public/' . $pathUlr, '/font/hei.ttf', $uid, $rand);
                    $this->create_text(ROOT_PATH . 'public/' . $pathUlr, '¥' . $shop_info['price'], 30, [0, 0, 0], 30, 480, ROOT_PATH . 'public/' . $pathUlr, '/font/hei.ttf', $uid, $rand);
                    $this->create_text(ROOT_PATH . 'public/' . $pathUlr, '¥' . $shop_info['name'], 30, [0, 0, 0], 30, 540, ROOT_PATH . 'public/' . $pathUlr, '/font/hei.ttf', $uid, $rand);
                    $codeArr['uid'] = $uid;
                    $codeArr['shop_id'] = $shop_id;
                    $codeArr['code'] = $pathUlr;
                    db::name('code_photo')->insertGetId($codeArr);
                    return json(['code' => '0', 'path' => $pathUlr]);
                }
            } else {
                return __error('二維碼生成錯誤');
            }
        } else {
            return __error('錯誤的請求');
        }
    }
    public function create_text($path, $text, $size, $color, $x, $y, $out_image, $font = '/font/hei.ttf', $uid = 0, $rand)
    {
        //建立圖片的例項
        $dst = imagecreatefromstring(file_get_contents($path));
        //打上文字
        $black = imagecolorallocate($dst, $color[0], $color[1], $color[2]); //字型顏色
        imagefttext($dst, $size, 0, $x, $y, $black, $font, $text);
        imagejpeg($dst, $_SERVER['DOCUMENT_ROOT'] . '/static/uploads/posters/' . $uid . '/' . $rand . '.png', 100);
        imagedestroy($dst);
    }
    public function getAccessToken()
    {
        $appid = config('wx.appid');
        $secret = config('wx.secret');
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        $tokeninfo = json_decode($output, true);
        cache('token', $tokeninfo['access_token'], 7200);
        // session('token', $tokeninfo['access_token'], 7200);
        return $tokeninfo['access_token'];
    }

    // 模擬post進行url請求
    public function curl_post_https($url, $data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept-Charset: utf-8']);
        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_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)) {
            return false;
        } else {
            return $tmpInfo;
        }
    }
    //圓形
    $sub_image_c = imagecreatefromstring(http_request($member_info_image));
                    $bool = imagecopyresampled($resource, $sub_image_c, 30, 30, 0, 0, 80, 80, imagesx($sub_image_c), imagesy($sub_image_c));
 // 圓角處理
                $radius = 50;
                // lt(左上角)
                $lt_corner = get_lt_rounder_corner($radius);
                imagecopymerge($resource, $lt_corner, 30, 30, 0, 0, $radius, $radius, 100);
                // lb(左下角)
                $lb_corner = imagerotate($lt_corner, 90, 0);
                imagecopymerge($resource, $lb_corner, 80, 1874 - $radius, 0, 0, $radius, $radius, 100);
                // rb(右下角)
                $rb_corner = imagerotate($lt_corner, 180, 0);
                imagecopymerge($resource, $rb_corner, 270 - $radius, 1874 - $radius, 0, 0, $radius, $radius, 100);
                // rt(右上角)
                $rt_corner = imagerotate($lt_corner, 270, 0);
                imagecopymerge($resource, $rt_corner, 270 - $radius, 1684, 0, 0, $radius, $radius, 100);
                imagejpeg($resource, $path, 100);
 function get_lt_rounder_corner($radius) {
    $img     = imagecreatetruecolor($radius, $radius);  // 建立一個正方形的影像
    $bgcolor    = imagecolorallocate($img, 255, 255, 255);   // 影像的背景
    $fgcolor    = imagecolorallocate($img, 0, 0, 0);
    imagefill($img, 0, 0, $bgcolor);
    // $radius,$radius:以影像的右下角開始畫弧
    // $radius*2, $radius*2:已寬度、高度畫弧
    // 180, 270:指定了角度的起始和結束點
    // fgcolor:指定顏色
    imagefilledarc($img, $radius, $radius, $radius*2, $radius*2, 180, 270, $fgcolor, IMG_ARC_PIE);
    // 將弧角圖片的顏色設定為透明
    imagecolortransparent($img, $fgcolor);
    // 變換角度
    // $img = imagerotate($img, 90, 0);
    // $img = imagerotate($img, 180, 0);
    // $img = imagerotate($img, 270, 0);
    // header('Content-Type: image/png');
    // imagepng($img);
    return $img;
}

相關文章