<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------namespace plugins\QiNiu;use app\common\controller\Plugin;
use app\admin\model\Attachment as AttachmentModel;
use think\Db;
use think\Image;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\facade\Env;require Env::get('root_path') . 'plugins/QiNiu/SDK/autoload.php';/*** 七牛上传插件* @package plugins\QiNiu* @author 蔡伟明 <314013107@qq.com>*/
class QiNiu extends Plugin
{/*** @var array 插件信息*/public $info = [// 插件名[必填]'name' => 'QiNiu',// 插件标题[必填]'title' => '七牛上传插件',// 插件唯一标识[必填],格式:插件名.开发者标识.plugin'identifier' => 'qi_niu.ming.plugin',// 插件图标[选填]'icon' => 'fa fa-fw fa-upload',// 插件描述[选填]'description' => '仅支持DolphinPHP1.0.6以上版本,安装后,需将【<a href="/admin.php/admin/system/index/group/upload.html">上传驱动</a>】将其设置为“七牛云”。在附件管理中删除文件,并不会同时删除七牛云上的文件。',// 插件作者[必填]'author' => '蔡伟明',// 作者主页[选填]'author_url' => 'http://www.caiweiming.com',// 插件版本[必填],格式采用三段式:主版本号.次版本号.修订版本号'version' => '1.1.0',// 是否有后台管理功能[选填]'admin' => '0',];/*** @var array 插件钩子*/public $hooks = ['upload_attachment'];/*** 上传附件* @param array $params 参数* @return mixed* @author 蔡伟明 <314013107@qq.com>*/public function uploadAttachment($params = []){$file = $params['file'];// 缩略图参数$thumb = request()->post('thumb', '');// 水印参数$watermark = request()->post('watermark', '');$config = $this->getConfigValue();$error_msg = '';if ($config['ak'] == '') {$error_msg = '未填写七牛【AccessKey】';} elseif ($config['sk'] == '') {$error_msg = '未填写七牛【SecretKey】';} elseif ($config['bucket'] == '') {$error_msg = '未填写七牛【Bucket】';} elseif ($config['domain'] == '') {$error_msg = '未填写七牛【Domain】';}if ($error_msg != '') {switch ($params['from']) {case 'wangeditor':return "error|{$error_msg}";break;case 'ueditor':return json(['state' => $error_msg]);break;case 'editormd':return json(["success" => 0, "message" => $error_msg]);break;case 'ckeditor':return ck_js(request()->get('CKEditorFuncNum'), '', $error_msg);break;default:return json(['code' => 0,'class' => 'danger','info' => $error_msg]);}}$config['domain'] = rtrim($config['domain'], '/') . '/';// 移动到框架应用根目录/uploads/ 目录下$info = $file->move(config('upload_path') . DIRECTORY_SEPARATOR . 'temp', '');// 文件信息$file_info = $file->getInfo();// 要上传文件的本地路径$filePath = $info->getPathname();// 上传到七牛后保存的文件名$file_name = explode('.', $file_info['name']);$ext = end($file_name);$key = $info->hash('md5') . '.' . $ext;// 如果是图片,则获取宽度和高度,并判断是否添加水印$ext_limit = config('upload_image_ext');$ext_limit = $ext_limit == '' ? [] : explode(',', $ext_limit);// 缩略图路径$thumb_path_name = '';if (preg_grep("/$ext/i", $ext_limit)) {$img = Image::open($info);$img_width = $img->width();$img_height = $img->height();// 水印功能if ($watermark == '') {if (config('upload_thumb_water') == 1 && config('upload_thumb_water_pic') > 0) {$this->create_water($info->getRealPath(), config('upload_thumb_water_pic'));}} else {if (strtolower($watermark) != 'close') {list($watermark_img, $watermark_pos, $watermark_alpha) = explode('|', $watermark);$this->create_water($info->getRealPath(), $watermark_img, $watermark_pos, $watermark_alpha);}}// 生成缩略图if ($thumb == '') {if (config('upload_image_thumb') != '') {list($thumb_max_width, $thumb_max_height) = explode(',', config('upload_image_thumb'));$thumb_path_name = $config['domain'] . $key . '?imageMogr2/thumbnail/' . $thumb_max_width . 'x' . $thumb_max_height;}} else {if (strtolower($thumb) != 'close') {list($thumb_size, $thumb_type) = explode('|', $thumb);list($thumb_max_width, $thumb_max_height) = explode(',', $thumb_size);$thumb_path_name = $config['domain'] . $key . '?imageMogr2/thumbnail/' . $thumb_max_width . 'x' . $thumb_max_height;}}} else {$img_width = '';$img_height = '';}// 需要填写你的 Access Key 和 Secret Key$accessKey = $config['ak'];$secretKey = $config['sk'];// 构建鉴权对象$auth = new Auth($accessKey, $secretKey);// 要上传的空间$bucket = $config['bucket'];// 生成上传 Token$token = $auth->uploadToken($bucket, $key);// 初始化 UploadManager 对象并进行文件的上传$uploadMgr = new UploadManager();// 调用 UploadManager 的 putFile 方法进行文件的上传list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);if ($err !== null) {return json(['code' => 0, 'class' => 'danger', 'info' => '上传失败:' . $err]);} else {// 获取附件信息$data = ['uid' => session('user_auth.uid'),'name' => $file_info['name'],'mime' => $file_info['type'],'path' => $config['domain'] . $key . '?v=' . rand(111111, 999999),'ext' => $info->getExtension(),'size' => $info->getSize(),'md5' => $info->hash('md5'),'sha1' => $info->hash('sha1'),'thumb' => $thumb_path_name,'module' => $params['module'],'driver' => 'qiniu','width' => $img_width,'height' => $img_height,];if ($file_add = AttachmentModel::create($data)) {unset($info);// 删除本地临时文件@unlink(config('upload_path') . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . $file_info['name']);// 返回结果switch ($params['from']) {case 'wangeditor':return $data['path'];break;case 'ueditor':return json(["state" => "SUCCESS",          // 上传状态,上传成功时必须返回"SUCCESS""url" => $data['path'], // 返回的地址"title" => $file_info['name'], // 附件名]);break;case 'editormd':return json(["success" => 1,"message" => '上传成功',"url" => $data['path'],]);break;case 'ckeditor':return ck_js(request()->get('CKEditorFuncNum'), $data['path']);break;default:return json(['code' => 1,'info' => '上传成功','class' => 'success','id' => $file_add['id'],'path' => $data['path']]);}} else {switch ($params['from']) {case 'wangeditor':return "error|上传失败";break;case 'ueditor':return json(['state' => '上传失败']);break;case 'editormd':return json(["success" => 0, "message" => '上传失败']);break;case 'ckeditor':return ck_js(request()->get('CKEditorFuncNum'), '', '上传失败');break;default:return json(['code' => 0, 'class' => 'danger', 'info' => '上传失败']);}}}}/*** 添加水印* @param string $file 要添加水印的文件路径* @param string $watermark_img 水印图片id* @param string $watermark_pos 水印位置* @param string $watermark_alpha 水印透明度* @author 蔡伟明 <314013107@qq.com>*/private function create_water($file = '', $watermark_img = '', $watermark_pos = '', $watermark_alpha = ''){$img = Db::name('admin_attachment')->where('id', $watermark_img)->find();$path = $img['path'];$tmp = false;if (strtolower(substr($path, 0, 4)) == 'http') {$file_watermark = file_get_contents($path);$thumb_water_pic = config('upload_path') . DIRECTORY_SEPARATOR . 'temp/' . $img['md5'] . '.' . $img['ext'];if (false === file_put_contents($thumb_water_pic, $file_watermark)) {return;}$tmp = true;} else {$thumb_water_pic = realpath(Env::get('root_path') . 'public/' . $path);}if (is_file($thumb_water_pic)) {// 读取图片$image = Image::open($file);// 添加水印$watermark_pos = $watermark_pos == '' ? config('upload_thumb_water_position') : $watermark_pos;$watermark_alpha = $watermark_alpha == '' ? config('upload_thumb_water_alpha') : $watermark_alpha;$image->water($thumb_water_pic, $watermark_pos, $watermark_alpha);// 保存水印图片,覆盖原图$image->save($file);// 删除临时文件if ($tmp) {unlink($thumb_water_pic);}}}/*** 安装方法* @return bool* @author 蔡伟明 <314013107@qq.com>*/public function install(){if (!version_compare(config('dolphin.product_version'), '1.0.6', '>=')) {$this->error = '本插件仅支持DolphinPHP1.0.6或以上版本';return false;}$upload_driver = Db::name('admin_config')->where(['name' => 'upload_driver', 'group' => 'upload'])->find();if (!$upload_driver) {$this->error = '未找到【上传驱动】配置,请确认DolphinPHP版本是否为1.0.6以上';return false;}$options = parse_attr($upload_driver['options']);if (isset($options['qiniu'])) {$this->error = '已存在名为【qiniu】的上传驱动';return false;}$upload_driver['options'] .= PHP_EOL . 'qiniu:七牛云';$result = Db::name('admin_config')->where(['name' => 'upload_driver', 'group' => 'upload'])->setField('options', $upload_driver['options']);if (false === $result) {$this->error = '上传驱动设置失败';return false;}return true;}/*** 卸载方法* @return bool* @author 蔡伟明 <314013107@qq.com>*/public function uninstall(){$upload_driver = Db::name('admin_config')->where(['name' => 'upload_driver', 'group' => 'upload'])->find();if ($upload_driver) {$options = parse_attr($upload_driver['options']);if (isset($options['qiniu'])) {unset($options['qiniu']);}$options = implode_attr($options);$result = Db::name('admin_config')->where(['name' => 'upload_driver', 'group' => 'upload'])->update(['options' => $options, 'value' => 'local']);if (false === $result) {$this->error = '上传驱动设置失败';return false;}}return true;}/*** 前台 api 上传到七牛云* $params 数组 file 本地图片地址 file_name 图片名称,uid,操作人* @return array*/public function qiuNiu($params){$config = $this->getConfigValue();// 要上传文件的本地路径$filePath = $params['file'];// 上传到七牛后保存的文件名$file_name = explode('.', $params['file_name']);$ext = end($file_name);$key = md5($filePath) . '.' . $ext;// 如果是图片,则获取宽度和高度,并判断是否添加水印$ext_limit = config('upload_image_ext');$ext_limit = $ext_limit == '' ? [] : explode(',', $ext_limit);// 需要填写你的 Access Key 和 Secret Key$accessKey = $config['ak'];$secretKey = $config['sk'];// 构建鉴权对象$auth = new Auth($accessKey, $secretKey);// 要上传的空间$bucket = $config['bucket'];// 生成上传 Token$token = $auth->uploadToken($bucket, $key);// 初始化 UploadManager 对象并进行文件的上传$uploadMgr = new UploadManager();// 调用 UploadManager 的 putFile 方法进行文件的上传list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);if ($err !== null) {return json(['code' => 0, 'class' => 'danger', 'info' => '上传失败:' . $err]);} else {// 获取附件信息$data = ['uid' =>$params['uid'],'name' => $file_name,'mime' => 'image/jpeg','path' => $config['domain'] . $key . '?v=' . rand(111111, 999999),'ext' => 'jpeg','module' => 'api','driver' => 'qiniu',];if ($file_add = AttachmentModel::create($data)) {unset($info);// 删除本地临时文件@unlink($filePath);// 返回结果$arr= array('code' => 1,'info' => '上传成功','class' => 'success','id' => $file_add['id'],'path' => $data['path']);return $arr;} else {$arr= array('code' => 0,'info' => '上传失败','class' => 'errcode',);return $arr;}}}
}

//处理图片,及合成

// 获取旅游详情分享图合成
public function getTourismShareImage($id)
{$tourismModel = new TravelGoods();$tourism = $tourismModel::getTourism($id);// 线路二维码图片$tourism['share_qrcode'] = $this->getTourismShareQrcode($id);// 分享图片$tourism['share_img'] = ImageResource::getImageUrl($tourism['share_img']);if (!$tourism['share_img']) {throw new TourismCollectionException(['msg' => '没有找到指定的旅游线路分享图片','errorCode' => 40100]);}$a = $this->gdPicSynthesis($tourism['share_qrcode'],$tourism['share_img'],$id);return $a;
}//图片合成
private function gdPicSynthesis($share_qrcode,$share_img,$id){ob_start();//两张图片合成方法$bgimg = $share_img;$bg_info = getimagesize($bgimg);//获取图片的后缀$bg_type = image_type_to_extension($bg_info[2], false);//拼接图片资源句柄函数$func = 'imagecreatefrom' . $bg_type;//创建图片资源句柄$bg_image = $func($bgimg);$imageZ =imagecreatetruecolor(298, 447);//copy部分图像并调整imagecopyresized($imageZ, $bg_image,0, 0,0, 0,298, 447, $bg_info[0], $bg_info[1]);//图2处理  二维码$tu2 = $share_qrcode;list($width, $height)=getimagesize($tu2);$new=imagecreatetruecolor(150, 150);$img=imagecreatefromjpeg($tu2);//copy部分图像并调整imagecopyresized($new, $img,0, 0,0, 0,150, 150, $width, $height);//图像输出新图片、另存为imagejpeg($new, "pic1.jpg");imagedestroy($new);imagedestroy($img);$tu2 = './pic1.jpg';$tu2_mark = getimagesize($tu2);$tu2_type = image_type_to_extension($tu2_mark[2], false);//创建图片资源句柄$tu2_func = 'imagecreatefrom' . $tu2_type;$tu2_image = $tu2_func($tu2);//合并背景图+二维码imagecopy($imageZ, $tu2_image, 145, 295, 0, 0, 150, 150);//图片输出函数拼接$outFunc = 'image' . $bg_type;$file_name=time();//保存图片$img_name = config('upload_path') .'/'. $file_name . '.jpg';$outFunc($imageZ, $img_name);$params['file']=$img_name;$params['uid']='0';$params['file_name']=$file_name . '.jpg';$qiNiu=new QiNiu();//上传到七牛云$list=$qiNiu->qiuNiu($params);return $list;
}
    // 获取线路二维码图片urlprivate function getTourismShareQrcode($tourismId){// 获取会员编码
//        $memberCode = TokenService::getCurrentMemberCode();$memberCode='f047cb42';// 二维码参数$scene = 'member_code=' . $memberCode . '&id=' . $tourismId;// 生成邀请二维码$response = $this->miniApp->app_code->getUnlimit($scene, ['page' => 'pages/protail/protail','width' => 600,]);if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {// 生成二维码图片,后面建议走OSS,暂时简单实现$fileName = $response->save('./public/static/tourismShare/img');}// 拼接可以访问的二维码url地址return Config::get('base.qrcode_prefix') . 'public/static/tourismShare/img/' . $fileName;}

基于海豚PHP框架 七牛云(插件),合成小程序分享图 并上传到七牛云相关推荐

  1. 七牛云图床php,PHP实现Markdown文章上传到七牛图床的实例内容

    在使用 Markdown 编写文章之后,经常需要发布到不同的平台,这里会遇到一个问题,文章的图片需要手动的进行上传,管理起来非常不方便,因此,强烈建议将图片统一上传到图床中,这样的话一篇文章就可以轻松 ...

  2. node.js学习笔记 - 文件上传(并用七牛云托管)

    文章目录 环境搭建 准备工作 安装相关依赖 代码实现 执行 环境搭建 准备工作 提示:本文采用ts来构建环境,要是以js构建则取掉类型定义即可. 初始化项目 创建目录fileUpload-demo- ...

  3. 一个基于cocos2d-x 3.0和Box2d的demo小程序

    一个基于cocos2d-x 3.0和Box2d的demo小程序 p图demo小应用.想怎么p就怎么p 本文參考于http://blog.csdn.net/xiaominghimi/article/de ...

  4. 调用图片文件夹中的任意图片随机显示_hexo框架下,实现图片一键式上传到七牛云

    在使用hexo框架驱动个人博客时,图片的插入是一个比较费心的事情 以我为例,我使用hexo+Github搭建个人独立博客 但是Github仓库提供的存储空间是有限的,如果把图片保存到本地(对应的是Gi ...

  5. java 二维码与海报合成图片并上传至七牛云

    简单的二维码与图片合成 效果如下: 准备步骤: 第一步毋庸置疑准备图片 第二步整合二维码项目依赖(这里我们使用的是google.zxing,当然市面上开源的二维码项目很多,你可自行选择) GitHub ...

  6. html七牛云图片文件上传,使用Js将文件图片视频上传到七牛云详细教程-Fun言

    前言 因为项目需要,需要将一些较大文件上传到七牛云,以前都是通过数据流上传到服务端,然后在服务端进行上传操作,因为以前文件都很小所以无所谓,但是要是几百M的文件就不行了,所以就采用前端js直接上传到七 ...

  7. 将base64编码图片上传到七牛云

    将base64编码图片上传到七牛云 接口说明 FAQ >对象存储 >上传下载> 如何上传base64编码图片到七牛云 在七牛云FAQ上有一篇文章,讲解的接口的使用 https://d ...

  8. 项目基于vue-uniapp,使用colorUi与uview框架,完美支持微信小程序

    介绍: 项目说明 项目基于Vue-uniApp,使用colorUi与uview框架,部分Demo参考uniApp插件市场等,感谢各位作者- 项目包含功能 (部分): 自定义视频插件 自定义TabBar ...

  9. Spring Boot使用Simditor富文本编辑器,并将图片上传到七牛云。

    开始之前,需要强调的是Simditor富文本编辑器是基于Jquery的.所以必须引入Jquery.Simditor下载地址 下载解压后会发现是整个simditor项目,很多东西项目根本不需要,只需要将 ...

最新文章

  1. 压缩感知及应用 源代码_【DMD应用】基于压缩感知超分辨鬼成像
  2. 常见被病毒利用的漏洞补丁
  3. php jquery ajax输出数组吗,jquery – 从PHP返回数组时的Ajax Parse错误
  4. 腾讯的张小龙是一个什么样的人
  5. 红米ac2100有ipv6吗_#年末#白里透红,跑得相当快,红米AC2100体验
  6. 5个强大的Java分布式缓存框架推荐
  7. Logical Volume Manager(逻辑卷管理)
  8. gsoap写一个c++ webservice
  9. ML之sklearn:sklearn库中的ShuffleSplit()函数和StratifiedShuffleSplit()函数的讲解
  10. vb.net 机器学习-候选消除法
  11. 深入浅出Paxos分布式一致性算法
  12. 基本select语句的生命周期
  13. 7大最重要的管理方法
  14. Python高级特性: 12步轻松搞定Python装饰器
  15. 编写properties文件的Eclipse插件
  16. 自己动手写事件总线(EventBus)
  17. CentOS7 安装MongoDB 3.0服务
  18. Win32 Application 、Win32 Console Application、MFC工程项目
  19. 绘制微信小程序画布时钟
  20. CentOS mysql重置密码

热门文章

  1. Excel VBA 锁定特定单元格
  2. 基于微信小程序的教室管理系统_北邮信通院大二下程序设计综合实验
  3. ElasticSearch中的中文分词详解
  4. 一个资深设计师应该知道的7个网站-第一弹
  5. 更改awr快照保存时间_如何更改OS X中屏幕快照的保存位置
  6. 二年级的女儿用计算机算算术,一年级的孩子一分钟口算多少题才算能达标?
  7. c语言游戏服务器源码,2018大师级C++游戏服务器编程实战(视频+源码)
  8. android仿iphone的时间轮的工具demo,利用swiper仿iphone时间设置滚轮控件(示例代码)...
  9. 双屏怎么快速切换鼠标_设定双屏,怎样使鼠标不会跑到第二个屏
  10. chopper云音乐开发笔记