生成带头像二维码
<?php
//文档
//https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443433542
header("Content-type: image/jpeg");
//echo "<pre>";print_r($_W);echo "<pre>";
$appid = "appid";
$appsecret = "appsecret";
//获取access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
$res_access_token_json = httpsGet($url);
$access_token_arr = json_decode($res_access_token_json, true);
$access_token = $access_token_arr['access_token'];
//创建二维码ticket
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={$access_token}";
$post_arr = array(
"action_name" => "QR_LIMIT_STR_SCENE",
"action_info" => array(
"scene" => array("scene_str"=>"test")
)
);
$post_json = json_encode($post_arr, true);
$res_json = httpsRequest($url,$post_json);
$res_arr = json_decode($res_json, true);
$ticket = $res_arr['ticket'];
//通过ticket换取二维码
$url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
$img = httpsGet($url);
$file ="./".$type.$scene.'.jpg'; //保存图像地址
file_put_contents($file,$img);
//$str = "<img src=".$file.">";
//用头像和生成的二维码合拼成带头像二维码
$QR=$file;
$logo='http://7xk56y.com1.z0.glb.clouddn.com/FgkCSrkrr6VjMcK8v26DAWG11pav?imageView2/1/w/100/h/100';
$im = @imagecreatetruecolor(430, 430);
if ($logo !== FALSE) {
// $im=imagecreatefromstring(file_get_contents($im));
$QR = imagecreatefromstring(file_get_contents($QR));
$logo = imagecreatefromstring(file_get_contents($logo));
$QR_width = imagesx($QR);//二维码图片宽度
$QR_height = imagesy($QR);//二维码图片高度
$logo_width = imagesx($logo);//logo图片宽度
$logo_height = imagesy($logo);//logo图片高度
$logo_qr_width = $QR_width / 5;
$scale = $logo_width/$logo_qr_width;
$logo_qr_height = $logo_height/$scale;
$from_width = ($QR_width - $logo_qr_width) / 2;
//重新组合图片并调整大小
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
}
//输出图片
imagepng($QR, 'helloweba.png');
echo '<img src="helloweba.png">';
//post数据
function httpsRequest($url,$data = null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
//get数据
function httpsGet($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}
?>
来源出于 站帮 www.zhanbang100.cn