作者 王晓刚
1 个管道 的构建 通过 耗费 1 秒

海报

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/6/8
* Time: 18:59
*/
namespace app\portal\controller;
use cmf\controller\WeChatBaseController;
use think\Db;
class ShareController extends WeChatBaseController
{
public function index(){
//判断用户是否填写手机号
$user_id = cmf_get_current_user_id();
$my_user = Db::name('my_user')->where('uid',$user_id)->find();
$user = Db::name('user')->where('id',$user_id)->find();
if(empty($my_user)){
$this->error('查询为空','','','');
}
if(empty($my_user['phone'])){
$this->error('跳转补全信息页面','','','');
}
//判断有没有生成过
if(file_exists('./upload/poster/poster_'.$user_id.".png")){
$url = $this->get_url($user_id,$my_user['status']);
$webPath = 'qrcode/share_'.$user_id.'.png';
$savePath = './upload/'.$webPath;
//生成带参二维码
if(!file_exists($savePath)){
$prcode_url = $this->get_code($user_id,$url);
}
//保存头像
$avatar=$this->getImage($user['avatar'],'avatar_'.$user['id']);
if($avatar['code']==1){
$this->error($avatar['msg']);
}
$savePath = './upload/poster';
if(!file_exists($savePath)){
mkdir ($savePath,0777,true);
}
$image = \think\Image::open(ROOT_PATH."public/upload/avatar/avatar_".$user_id.".jpeg");
$image->thumb(173,173,\think\Image::THUMB_FIXED)->save('./upload/avatar/avatar_'.$user_id.".png");
$image = \think\Image::open(ROOT_PATH."public/white.png");
$image->water(ROOT_PATH."public/upload/qrcode/share_".$user_id.".png",[195,725],100)
->water(ROOT_PATH."public/upload/avatar/avatar_".$user_id.".png",[289,478],100)
->water(ROOT_PATH."public/bg.png",\think\Image::WATER_CENTER,100)
->save('./upload/poster/poster_'.$user_id.".png");
}
$share_image = cmf_get_image_url('poster/poster_'.$user_id.".png");
$this->assign('share_image',$share_image);
return $this->fetch();
}
/**
* 分享链接
* @param $user_id
* @param $status
* @return string
*/
public function get_url($user_id,$status){
//http://xkeasy.w.bronet.cn/portal/index/index/my_user_id/用户id/status_id/身份id
$url = url('portal/index/index',['my_user_id'=>$user_id,'status_id'=>$status]);
return $url;
}
/**
* 生成二维码
* @param $user_id
* @param $qrData
* @return bool|string
*/
public function get_code($user_id,$qrData){
/*存储本地*/
$webPath = 'qrcode/';
$savePath = './upload/'.$webPath;
if(!file_exists($savePath)){
mkdir ($savePath,0777,true);
}
$saveName = "share_".$user_id;
$qrLevel = 'H';
$qrSize = '8';
$savePrefix = "";
if($filename = createQRcode($savePath, $qrData, $qrLevel, $qrSize, $savePrefix,$saveName)){
$pic = $webPath .$savePrefix. $saveName . '.png';
return $pic;
}else{
return false;
}
}
/**
* 下载微信头像保存到本地
* @param $url
* @param $filename
* @return array
*/
function getImage($url,$filename){
$save_dir='./upload/avatar/';
if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){
return ['code'=>1,'msg'=>'图片路径错误'];
}
$header = array(
'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding: gzip, deflate',);
$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$data = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($code == 200) {//把URL格式的图片转成base64_encode格式的!
$imgBase64Code = "data:image/jpeg;base64," . base64_encode($data);
}else{
return ['code'=>1,'msg'=>'图片转换失败'];
}
$img_content=$imgBase64Code;//图片内容
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img_content, $result)) {
$type = $result[2];
$new_file = $save_dir.$filename.'.'.$type;
$save_path= 'avatar/'.$filename.'.'.$type;
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img_content)))) {
return ['code'=>0,'file_path'=>$new_file,'save_path'=>$save_path];
}else{
return ['code'=>1,'msg'=>'图片保存失败'];
}
}else{
return ['code'=>1,'msg'=>'图片格式错误'];
}
}
}
\ No newline at end of file
... ...
... ... @@ -30,7 +30,9 @@
"ezyang/htmlpurifier": "^4.9",
"weiwei/api-doc": "^1.6",
"hooklife/thinkphp5-wechat": "^1.1",
"yly-openapi/yly-openapi-sdk": "v1.0.1"
"yly-openapi/yly-openapi-sdk": "v1.0.1",
"dh2y/think-qrcode": "^1.0",
"aferrandini/phpqrcode": "^1.0"
},
"extra": {
},
... ...
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport" />
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>学考无忧-我的推广</title>
<link rel="stylesheet" href="__TMPL__/public/assets/css/reset.css">
<link rel="stylesheet" href="__TMPL__/public/assets/css/base.css">
<style>
html,
body {
width: 100%;
height: 100%;
display: flex;
display: -webkit-flex;
justify-content: flex-start;
align-items: flex-start;
flex-direction: column;
}
.code_Box {
background: #FF5C19 url("{:cmf_get_image_url($share_image)}") no-repeat top left;
background-size: 100%;
-webkit-background-size: 100%;
}
</style>
</head>
<body>
<!-- 顶部 -->
<div class="order_top" style="background-color: #FF5C19">
<img src="__TMPL__/public/assets/images/left2.png" alt="">
<p style="color:white">我的推广</p>
</div>
<div class="code_Box">
</div>
<script src="__TMPL__/public/assets/js/base.js"></script>
</body>
</html>
\ No newline at end of file
... ...
... ... @@ -1822,4 +1822,49 @@ function getAccessToken(){
$jssdk=new \JSSDK(config('wx_app_id'),config('wx_app_secret'));
return $jssdk->getAccessToken();
}
/**
* 功能:生成二维码
* @param string $savePath 图片存储路径
* @param string $qrData 手机扫描后要跳转的网址
* @param string $qrLevel 默认纠错比例 分为L、M、Q、H四个等级,H代表最高纠错能力
* @param int $qrSize 二维码图大小,1-10可选,数字越大图片尺寸越大
* @param string $savePrefix 图片名称前缀
* @param array $saveName 图片名称前缀
* @return bool|string
*/
function createQRcode($savePath, $qrData = 'PHP QR Code :)', $qrLevel = 'L', $qrSize = 4, $savePrefix = '',$saveName)
{
if (!isset($savePath)) return '';
//设置生成png图片的路径
$PNG_TEMP_DIR = $savePath;
//检测并创建生成文件夹
if (!file_exists($PNG_TEMP_DIR)) {
mkdir($PNG_TEMP_DIR,777,true);
}
$filename = $PNG_TEMP_DIR . 'test.png';
$errorCorrectionLevel = 'L';
if (isset($qrLevel) && in_array($qrLevel, ['L', 'M', 'Q', 'H'])) {
$errorCorrectionLevel = $qrLevel;
}
$matrixPointSize = 4;
if (isset($qrSize)) {
$matrixPointSize = min(max((int)$qrSize, 1), 10);
}
if (isset($qrData)) {
if (trim($qrData) == '') {
die('data cannot be empty!');
}
//生成文件名 文件路径+图片名字前缀+md5(名称)+.png
$filename = $PNG_TEMP_DIR . $savePrefix .$saveName . '.png';
//开始生成
\PHPQRCode\QRcode::png($qrData, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
} else {
//默认生成
\PHPQRCode\QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);
}
if (file_exists($PNG_TEMP_DIR . basename($filename)))
return basename($filename);
else
return FALSE;
}
... ...
... ... @@ -9,6 +9,7 @@ return array(
'Svg\\' => array($vendorDir . '/phenx/php-svg-lib/src'),
'Sabberworm\\CSS' => array($vendorDir . '/sabberworm/php-css-parser/lib'),
'Pimple' => array($vendorDir . '/pimple/pimple/src'),
'PHPQRCode' => array($vendorDir . '/aferrandini/phpqrcode/lib'),
'PHPExcel' => array($vendorDir . '/phpoffice/phpexcel/Classes'),
'HTMLPurifier' => array($vendorDir . '/ezyang/htmlpurifier/library'),
'Doctrine\\Common\\Cache\\' => array($vendorDir . '/doctrine/cache/lib'),
... ...
... ... @@ -11,6 +11,7 @@ return array(
'think\\captcha\\' => array($vendorDir . '/topthink/think-captcha/src'),
'think\\' => array($vendorDir . '/topthink/think-image/src'),
'mindplay\\annotations\\' => array($vendorDir . '/mindplay/annotations/src/annotations'),
'dh2y\\qrcode\\' => array($vendorDir . '/dh2y/think-qrcode/src'),
'Symfony\\Polyfill\\Php70\\' => array($vendorDir . '/symfony/polyfill-php70'),
'Symfony\\Polyfill\\Mbstring\\' => array($vendorDir . '/symfony/polyfill-mbstring'),
'Symfony\\Component\\HttpFoundation\\' => array($vendorDir . '/symfony/http-foundation'),
... ...
... ... @@ -34,6 +34,10 @@ class ComposerStaticInit929fb365b9981d29459d58f676689cf7
array (
'mindplay\\annotations\\' => 21,
),
'd' =>
array (
'dh2y\\qrcode\\' => 12,
),
'S' =>
array (
'Symfony\\Polyfill\\Php70\\' => 23,
... ... @@ -109,6 +113,10 @@ class ComposerStaticInit929fb365b9981d29459d58f676689cf7
array (
0 => __DIR__ . '/..' . '/mindplay/annotations/src/annotations',
),
'dh2y\\qrcode\\' =>
array (
0 => __DIR__ . '/..' . '/dh2y/think-qrcode/src',
),
'Symfony\\Polyfill\\Php70\\' =>
array (
0 => __DIR__ . '/..' . '/symfony/polyfill-php70',
... ... @@ -205,6 +213,10 @@ class ComposerStaticInit929fb365b9981d29459d58f676689cf7
array (
0 => __DIR__ . '/..' . '/pimple/pimple/src',
),
'PHPQRCode' =>
array (
0 => __DIR__ . '/..' . '/aferrandini/phpqrcode/lib',
),
'PHPExcel' =>
array (
0 => __DIR__ . '/..' . '/phpoffice/phpexcel/Classes',
... ...
[
{
"name": "aferrandini/phpqrcode",
"version": "1.0.1",
"version_normalized": "1.0.1.0",
"source": {
"type": "git",
"url": "https://github.com/aferrandini/PHPQRCode.git",
"reference": "3c1c0454d43710ab5bbe19a51ad4cb41c22e3d46"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/aferrandini/PHPQRCode/zipball/3c1c0454d43710ab5bbe19a51ad4cb41c22e3d46",
"reference": "3c1c0454d43710ab5bbe19a51ad4cb41c22e3d46",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"time": "2013-07-08T09:39:08+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
"psr-0": {
"PHPQRCode": "lib/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Ariel Ferrandini",
"email": "arielferrandini@gmail.com",
"homepage": "http://www.ferrandini.com/",
"role": "Developer"
}
],
"description": "PHPQRCode porting and changed for PHP 5.3 compatibility",
"homepage": "https://github.com/aferrandini/PHPQRCode",
"keywords": [
"barcode",
"php",
"qrcode"
],
"abandoned": "endroid/qr-code"
},
{
"name": "dh2y/think-qrcode",
"version": "1.0",
"version_normalized": "1.0.0.0",
"source": {
"type": "git",
"url": "https://github.com/cinaofdai/think-qrcode.git",
"reference": "e4eb6f588ffb6b99ef4fb4610d44e338c9b4c506"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/cinaofdai/think-qrcode/zipball/e4eb6f588ffb6b99ef4fb4610d44e338c9b4c506",
"reference": "e4eb6f588ffb6b99ef4fb4610d44e338c9b4c506",
"shasum": ""
},
"require": {
"php": ">=5.4.0"
},
"time": "2018-10-16T04:31:01+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
"psr-4": {
"dh2y\\qrcode\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "dh2y",
"email": "xiaodai54_long@163.com"
}
],
"description": "qrcode for thinkphp5",
"homepage": "https://github.com/cinaofdai/think-qrcode"
},
{
"name": "doctrine/cache",
"version": "v1.4.4",
"version_normalized": "1.4.4.0",
... ...
... ... @@ -607,4 +607,31 @@ class Image
empty($this->im) || imagedestroy($this->im);
}
function round() {
$w = $this->info['width'];
$h = $this->info['height'];
$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($this->im, $x, $y);
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
$this->im= $img;
return $this;
}
}
... ...