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

<?php
namespace app\api\controller;

use app\api\service\RiverService;
use app\river\service\AccountService;
use controller\BasicApi;
use service\UserService;
use service\WechatService;
use think\Db;

class Qrcode extends BasicApi {


    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 分享图片生成
     * @param $gData 商品数据,array
     * @param $codeName 二维码图片
     * @param $fileName string 保存文件名,默认空则直接输入图片
     */
    public function getimg(){
        header("Content-type:image/png");
        $a = imagecreate(100,100);
        imagecolorallocate($a,255,0,255);
       imagepng($a);
        imagedestroy($a);
    }
    /**
     * 生成成就图
     */
    public function createsharepng(){
        ob_clean ();
        Header("Content-Type: image/png");

        //创建画布
        $im = imagecreatetruecolor(750, 1335);
        //填充画布背景色
        $color = imagecolorallocate($im, 255, 255, 255);
        imagefill($im, 0, 0, $color);
        //背景图
        $bacimg = 'static/bg.jpg';
        
        list($b_w,$b_h) = getimagesize($bacimg);
        $bacimg = $this->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; // 返回数据

    }


}


 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成小程序二维码可以通过微信官方提供的API实现,而PHP可以通过调用这个API来生成小程序二维码。具体步骤如下: 1. 获取access_token:调用微信官方提供的获取access_token的API,获取access_token。注意:access_token需要缓存,不要频繁调用该API获取。 2. 调用生成小程序二维码的API:调用微信官方提供的生成小程序二维码的API,传入必要的参数,比如path、width等。 3. 保存小程序二维码:把API返回的二进制流保存为图片即可。 下面是示例代码: ```php <?php $app_id = '你的小程序appid'; $app_secret = '你的小程序appsecret'; //获取access_token $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$app_id}&secret={$app_secret}"; $res = file_get_contents($url); $res = json_decode($res, true); $access_token = $res['access_token']; //调用生成小程序二维码的API $path = '/pages/index/index'; $width = 430; $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$access_token}"; $data = array( 'path' => $path, 'width' => $width ); $data = json_encode($data); $options = array( 'http' => array( 'header' => "Content-type:application/json", 'method' => 'POST', 'content' => $data, 'timeout' => 60 ) ); $context = stream_context_create($options); $res = file_get_contents($url, false, $context); //保存小程序二维码 file_put_contents('./qrcode.jpg', $res); ``` 需要注意的是,生成小程序二维码的API有一些限制,比如只能生成已发布的小程序二维码有效期为30天等。具体详情可以参考微信官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值