php 生成小程序二维码,生成海报画图。。

createImageFromFile($bacimg);
        imagecopyresized($im, $bacimg, 0, 0, 0, 0, 750, 1335, $b_w, $b_h);

        //字体文件
        $font_file = "static/PINGFANG.TTF";
        //设定字体的颜色
        $font_color_1 = ImageColorAllocate ($im, 18, 18, 18);//黑色
        $font_color_2 = ImageColorAllocate ($im, 153, 153, 153);//灰色
        $font_color_3 = ImageColorAllocate ($im, 239, 239, 239);//灰色
        //二维码
        $ress = self::getQrcodeImage($uid);
        if($ress)
        {
            $logoImg =  $ress;
        }else{
            $logoImg  = 'static/upload/user.jpg';
        }
        //Logo二维码
        list($l_w,$l_h) = getimagesize($logoImg);
        $logoImg = $this->createImageFromFile($logoImg);
        imagecopyresized($im, $logoImg, 50, 980, 0, 0, 170, 170, $l_w, $l_h);
        //个人头像
        $image =  $gData['cover'] ; // 原图

        list($g_w,$g_h) = getimagesize($image);
        $headimg = $this->createImageFromFile($image);
        imagecopyresized($im, $headimg, 80, 580, 0, 0, 79, 79, $g_w, $g_h);

            $nick_name = $gData['nick_name'];
        imagettftext($im, 28,0, 180, 600, $font_color_1 ,$font_file, $nick_name);
        
        imagettftext($im, $fontsize,0, 490 + (4-$count)*10, 890, $font_color_1 ,$font_file, $mins);
        imagettftext($im, $fontsize,0, 90, 890, $font_color_1 ,$font_file, $bai);
        imagettftext($im, $fontsize,0, 125, 890, $font_color_1 ,$font_file, $shi);
        imagettftext($im, $fontsize,0, 170, 890, $font_color_1 ,$font_file, $ge);
        $per = rand(90,99);
        imagettftext($im, $fontsize,0, 280, 890, $font_color_1 ,$font_file, $per);
        $path2 = 'static/upload/qrcode/'.rand(10000,99999).time().".png";

        imagepng ($im,$path2);
        $imgurl = "https://rwan.org.cn/".$path2;
        $data['promotion_img'] = $imgurl;
        $data['id'] = $uid;
        $this->success("生成成功",$imgurl);
        $res = UserService::save($data,$uid);
        if($res){
            $this->success("生成成功",$imgurl);
        }else{
            $this->error("生成失败");
        }
        exit();

        //释放空间
        imagedestroy($im);
        imagedestroy($goodImg);
        imagedestroy($codeImg);
    }

    /**
     * 从图片文件创建Image资源
     * @param $file 图片文件,支持url
     * @return bool|resource  成功返回图片image资源,失败返回false
     */
    function createImageFromFile($file){
        if(preg_match('/http(s)?:\/\//',$file)){
            $fileSuffix = $this->getNetworkImgType($file);
        }else{
            $fileSuffix = pathinfo($file, PATHINFO_EXTENSION);
        }

        if(!$fileSuffix) return false;

        switch ($fileSuffix){
            case 'jpeg':
                $theImage = @imagecreatefromjpeg($file);
                break;
            case 'jpg':
                $theImage = @imagecreatefromjpeg($file);
                break;
            case 'png':
                $theImage = @imagecreatefrompng($file);
                break;
            case 'gif':
                $theImage = @imagecreatefromgif($file);
                break;
            default:
                $theImage = @imagecreatefromstring(file_get_contents($file));
                break;
        }

        return $theImage;
    }

    /**
     * 获取网络图片类型
     * @param $url 网络图片url,支持不带后缀名url
     * @return bool
     */
    function getNetworkImgType($url){
        $ch = curl_init(); //初始化curl
        curl_setopt($ch, CURLOPT_URL, $url); //设置需要获取的URL
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持https
        curl_exec($ch);//执行curl会话
        $http_code = curl_getinfo($ch);//获取curl连接资源句柄信息
        curl_close($ch);//关闭资源连接

        if ($http_code['http_code'] == 200) {
            $theImgType = explode('/',$http_code['content_type']);

            if($theImgType[0] == 'image'){
                return $theImgType[1];
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * 分行连续截取字符串
     * @param $str 需要截取的字符串,UTF-8
     * @param int $row 截取的行数
     * @param int $number  每行截取的字数,中文长度
     * @param bool $suffix 最后行是否添加‘...'后缀
     * @return array  返回数组共$row个元素,下标1到$row
     */
    function cn_row_substr($str,$row = 1,$number = 10,$suffix = true){
        $result = array();
        for ($r=1;$r<=$row;$r++){
            $result[$r] = '';
        }

        $str = trim($str);
        if(!$str) return $result;

        $theStrlen = strlen($str);

        //每行实际字节长度
        $oneRowNum = $number * 3;
        for($r=1;$r<=$row;$r++){
            if($r == $row and $theStrlen > $r * $oneRowNum and $suffix){
                $result[$r] = $this->mg_cn_substr($str,$oneRowNum-6,($r-1)* $oneRowNum).'...';
            }else{
                $result[$r] = $this->mg_cn_substr($str,$oneRowNum,($r-1)* $oneRowNum);
            }
            if($theStrlen < $r * $oneRowNum) break;
        }

        return $result;
    }

    /**
     * 按字节截取utf-8字符串
     * 识别汉字全角符号,全角中文3个字节,半角英文1个字节
     * @param $str 需要切取的字符串
     * @param $len 截取长度[字节]
     * @param int $start  截取开始位置,默认0
     * @return string
     */
    function mg_cn_substr($str,$len,$start = 0)
    {
        $q_str = '';
        $q_strlen = ($start + $len) > strlen($str) ? strlen($str) : ($start + $len);

        //如果start不为起始位置,若起始位置为乱码就按照UTF-8编码获取新start
        if ($start and json_encode(substr($str, $start, 1)) === false) {
            for ($a = 0; $a < 3; $a++) {
                $new_start = $start + $a;
                $m_str = substr($str, $new_start, 3);
                if (json_encode($m_str) !== false) {
                    $start = $new_start;
                    break;
                }
            }
        }

        //切取内容
        for ($i = $start; $i < $q_strlen; $i++) {
            //ord()函数取得substr()的第一个字符的ASCII码,如果大于0xa0的话则是中文字符
            if (ord(substr($str, $i, 1)) > 0xa0) {
                $q_str .= substr($str, $i, 3);
                $i += 2;
            } else {
                $q_str .= substr($str, $i, 1);
            }
        }
        return $q_str;
    }

    public function getQrcodeImage( $user_id)
    {
        $access_token = WechatService::getAccessToken();
        $api = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$access_token}";
        header('content-type:image/gif');
        $data = array();
        $data['scene'] = $user_id;//自定义信息,可以填写诸如识别用户身份的字段,注意用中文时的情况
        $data['page'] = "pages/index/index";//扫描后对应的path
        $data['width'] = 170;//自定义的尺寸
        $data['auto_color'] = false;//是否自定义颜色
        $color = array(
            "r"=>"221",
            "g"=>"0",
            "b"=>"0",
        );
        $data['line_color'] = $color;//自定义的颜色值
        $data = json_encode($data);
        $da = $this->get_http_array($api,$data);
        $path2 = 'static/upload/qrcode/'.rand(10000,99999).time()."1.jpg";
        file_put_contents($path2, $da);
        return  $path2;

    }
    public function get_http_array($url,$data) {
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //解决数据包大不能提交
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回

        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);
        }
        curl_close($curl); // 关键CURL会话
        return $tmpInfo; // 返回数据

    }


}


 

你可能感兴趣的:(PHP)