PHP微信小程序海报二维码
作者:互联网
/**
*微信接口调用凭证
*/
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