链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

166 lines
5.3 KiB

<?php
namespace App\Admin\Common;
use Dcat\Admin\Controllers\AdminController;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
class Images extends AdminController
{
/**
* 获取token
*/
private function getWxToken()
{
$appid= env('WECHAT_MINI_PROGRAM_APPID');
$secret=env('WECHAT_MINI_PROGRAM_SECRET');
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$data = curl_exec($ch);
curl_close($ch);
$data = json_decode($data,true);
return $data['access_token'];
}
/**
* 请求生成微信小程序码
*/
public function createQrCode($param,$path)
{
$access_token = $this->getWxToken();
$data=array(
'scene'=>$param,
"page"=>$path,
"width"=>100
);
$data = json_encode($data);
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* 请求生成微信二维码
* @param $param 参数
*/
public function createWeChatQrCode($param,$path)
{
$access_token = $this->getWxToken();
$data=array(
"path"=>$path.$param,
"width"=>280
);
$data = json_encode($data);
$url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$access_token."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* 上传文件到oss
* @param $base64 //文件流
* @param $fileName //文件名称
* @return bool
*/
public function uploadOss($base64,$fileName)
{
return Storage::put($fileName,$base64);
}
public function test()
{
$wx_head = 'http://www.marketmanage.com/uploads/20200728/d5a491cd3d8d071e3212c3478e8e35a1.jpg';
$avatar_file = file_get_contents($wx_head);
file_put_contents('./logo.jpg',$avatar_file);
$logo = $this->changeAvatar($avatar_file);
file_put_contents('./logo_new.jpg',$logo);
//$qr_code = $this->createQrCode('','');
}
public function changeAvatar($avatar)
{
//处理用户头像为圆形icon
$avatar = imagecreatefromstring($avatar);
$w = imagesx($avatar)-5;
$h = imagesy($avatar)-5;
$w = min($w, $h);
$h = $w;
$img = imagecreatetruecolor($w, $h);
imagesavealpha($img, true);
$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($avatar, $x, $y);
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
ob_start();
imagepng($img);
imagedestroy($img);
imagedestroy($avatar);
$contents = ob_get_contents(); //读取缓存区的内容
ob_end_clean(); //清空缓存区
return $contents;
}
public function makeOnePic($qr_code, $logo) //二维码与头像组合
{
$qr_code = imagecreatefromstring($qr_code); //生成的二维码底色为白色
//设置二维码为透明底
imagesavealpha($qr_code, true); //这个设置一定要加上
$bg = imagecolorallocatealpha($qr_code, 255, 255, 255, 127); //拾取一个完全透明的颜色,最后一个参数127为全透明
imagefill($qr_code, 0, 0, $bg);
$icon = imagecreatefromstring($logo); //生成中间圆形logo (微信头像获取到的logo的大小为132px 132px)
$qr_width = imagesx($qr_code); //二维码图片宽度
$lg_width = imagesx($icon); //logo图片宽度
$lg_height = imagesy($icon); //logo图片高度
$qr_lg_width = $qr_width / 2.2;
$scale = $lg_width / $qr_lg_width;
$qr_lg_height = $lg_height / $scale;
$start_width = ($qr_width - $lg_width) / 2 ; //(获取logo的左上方的位置:( 外部的正方形-logo的宽 ) / 2,我这边存在1px的偏差 我就给+2啦)
imagecopyresampled($qr_code, $icon, $start_width, $start_width, 0, 0, $qr_lg_width, $qr_lg_height, $lg_width, $lg_height);
imagejpeg($qr_code,'./qrcode.png'); //保存
imagedestroy($qr_code);
imagedestroy($icon);
}
}