ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PHP微信小程序海报二维码

2022-01-07 10:59:15  阅读:188  来源: 互联网

标签:setopt 微信 width1 二维码 logo curl PHP data CURLOPT


/**
 *微信接口调用凭证
 */
public function checkAuth($appid, $appsecret)
{
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
    $result = $this->httpRequest($url);
    if ($result) {
        $json = json_decode($result, true);
        if (!$json || isset($json['errcode'])) {
            return false;
        }
        return $json['access_token'];
    }
    return false;

}
/**
 * 小程序二维码
 */
public function Wxcode($openid)
{
    $appsecret = preg_replace("/\s/", "",*******);
    $appid = *******;
    $token = $this->checkAuth($appid, $appsecret);
    $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $token;
    $post_data = json_encode([
        'page' => 'pages/index/index',
        'width' => 1280,
        'scene' => $openid,
    ]);
    $data = $this->httpRequest($url, $post_data, 'POST');
    $poster = './upload/poster/posters.jpg';   //海报
    $result = $this->poster($data, $poster, $openid);
    return json(['code' => 1, 'msg' => 'succeed', 'data' => "http://" . $_SERVER['HTTP_HOST'] . $result]);
}

/**
 * 合成海报二维码
 */
function poster($code, $poster, $openid)
{
    $timg = imagecreatefromstring(file_get_contents($poster));        //目标图象连接资源。
    $originalUrl = imagecreatefromstring($code);    //源图象连接资源。
    $QR_width1 = imagesx($timg);            //背景海报图片宽度
    $QR_height1 = imagesy($timg);            //背景海报图片高度
    $logo_width1 = imagesx($originalUrl);        //logo图片宽度
    $logo_height1 = imagesy($originalUrl);        //logo图片高度
    $logo_qr_width1 = $QR_width1 / 4;       //组合之后logo的宽度(占二维码的1/5)
    $scale1 = $logo_width1 / $logo_qr_width1;       //logo的宽度缩放比(本身宽度/组合后的宽度)
    $logo_qr_height1 = $logo_height1 / $scale1;  //组合之后logo的高度
    $from_width1 = ($QR_width1 - $logo_qr_width1) / 2;   //组合之后logo左上角所在坐标点
    //重新组合图片并调整大小
    //imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
    imagecopyresampled($timg,
        $originalUrl,
        $from_width1,
        $from_width1 * 1.8,
        0,
        0,
        $logo_qr_width1,
        $logo_qr_height1,
        $logo_width1,
        $logo_height1
    );
    $filename = './upload/code/' . $openid . '.jpg';
    //输出图片
    imagepng($timg, $filename);

    return $filename;
}

/**
 * @param $url
 * @param string $data
 * @param string $method
 * @return bool|string
 */
function httpRequest($url, $data = '', $method = 'GET')
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
    if ($method == 'POST') {
        curl_setopt($curl, CURLOPT_POST, 1);
        if ($data != '') {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
    }

    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}

标签:setopt,微信,width1,二维码,logo,curl,PHP,data,CURLOPT
来源: https://blog.csdn.net/qq_46688194/article/details/122359755

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有