先上代码 如果有不明白的可以加我微信zwj4_15
直接请求这个方法即可 id 为文章id path 是小程序的路径 如: /page/index/index
public function sharImg(Request $request){
(new IDMustBePositiveInt())->goCheck('id'); // 这个是我项目进行参数的校验 这里的id是传过来的文章id
$typeid = $request->post('id');//接收传过来的 文章id
$path = $request->post('path');//path 是 小程序给的路径 比如“/page/index/index”
$result = $this->curl_get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".config('setting.appid')."&secret=".config('setting.appsecret'));//小程序调用小程序码需要一个token ,token是需要appid 和appsecret
$token = json_decode($result,true);
$json_data=json_encode(array("path"=>"$path",'is_hyaline'=>true));//参数转为json格式 (生成小程序码参数格式必须为 json 格式)
$url ="https://api.weixin.qq.com/wxa/getwxacode?access_token=".$token['access_token'];
$Binary = $this->curl_post($url,$json_data);//发送请求
// $img = (new ProductsModel())->findImg($typeid);//查找发表这篇文章人的头像 如果是每篇文章都要换中间的logo ,就可以用数据库的存起来的图片,如果只是不想用自己的logo 可以采用我下面的 file_get_contents(固定图片的路径)方法
// $img_path = config('setting.img_path').$img['smallPic'];//把头像进行拼接成一个可以直接用浏览器打开的网址
//用户头像图片变圆形
// $avatar = file_get_contents("$img_path");//打开这张图片
$avatar = file_get_contents("https://wxapi.bjjlythy.com/uploads/wuliuyuan/5niu.jpg");
$logo = $this->yuanImg($avatar);//返回的是图片数据流
//二维码与头像结合
$sharePic = $this->qrcodeWithLogo($Binary,$logo);
//将生成的二维码保存到本地
$path = $this->binary_to_file($sharePic);//二进制流转换成图片并保存下来 ,返回图片的路径
$shar = (new ProductsModel())->sharimg($typeid,$path);//把图片路径保存在数据库对应的文章中
if($shar){
//成功则返回图片路径
return $this->errorCode(20000,config('setting.sharimg').substr($path,1));
}else{
//错误抛出异常 我的抛出异常是自己写的,按自己的方法处理即可
throw new SuccessMessage([
'msg' => '生成失败',
'errorCode' => 20004
]);
}
}
curl_get 方法
/**
* @param string $url get请求地址
* @param int $httpCode 返回状态码
* @return mixed
*/
function curl_get($url, &$httpCode = 0)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//不做证书校验,部署在linux环境下请改为true
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$file_contents = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $file_contents;
}
curl_post 请求 注意这个 参数是json 格式
//json post 请求
public function curl_post($url , $data=array()){
$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);
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json; charset=utf-8'));
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
function yuanImg($picture) {
$src_img = imagecreatefromstring($picture);
$w = imagesx($src_img);
$h = imagesy($src_img);
$w = min($w, $h);
$h = $w;
$img = imagecreatetruecolor($w, $h);
//这一句一定要有
imagesavealpha($img, true);
//拾取一个完全透明的颜色,最后一个参数127为全透明
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r = $w / 2; //圆半径
$y_x = $r; //圆心X坐标
$y_y = $r; //圆心Y坐标
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgbColor = imagecolorat($src_img, $x, $y);
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
/**
* 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
* 并且去掉缓存区函数
*/
//获取输出缓存,否则imagepng会把图片输出到浏览器
ob_start();
imagepng ( $img );
imagedestroy($img);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
public function qrcodeWithLogo($QR,$logo){
$QR = imagecreatefromstring ($QR);
$logo = imagecreatefromstring ($logo);
$QR_width = imagesx ( $QR );//二维码图片宽度
$QR_height = imagesy ( $QR );//二维码图片高度
$logo_width = imagesx ( $logo );//logo图片宽度
$logo_height = imagesy ( $logo );//logo图片高度
$logo_qr_width = $QR_width / 2.2;//组合之后logo的宽度(占二维码的1/2.2)
$scale = $logo_width / $logo_qr_width;//logo的宽度缩放比(本身宽度/组合后的宽度)
$logo_qr_height = $logo_height / $scale;//组合之后logo的高度
$from_width = ($QR_width - $logo_qr_width) / 2;//组合之后logo左上角所在坐标点
/**
* 重新组合图片并调整大小
* imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
*/
imagecopyresampled ( $QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height );
/**
* 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
* 并且去掉缓存区函数
*/
//获取输出缓存,否则imagepng会把图片输出到浏览器
ob_start();
imagepng ( $QR );
imagedestroy($QR);
imagedestroy($logo);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
二进制流转换为照片并 保存在本地项目中
/**
* $file 二进制流
*/
function binary_to_file($file){
$imgDir = './uploads/*****/';
//要生成的图片名字
$filename = md5(time().mt_rand(10, 99)).".png"; //新图片名称
$newFilePath = $imgDir.$filename;
$data = $file;
$newFile = fopen($newFilePath,"w"); //打开文件准备写入
fwrite($newFile,$data); //写入二进制流到文件
fclose($newFile); //关闭文件
return $newFilePath;
}