123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- <?php
- /**
- * Niushop商城系统 - 团队十年电商经验汇集巨献!
- * =========================================================
- * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
- * ----------------------------------------------
- * 官方网址: https://www.niushop.com.cn
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
- * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
- * =========================================================
- */
- namespace app\model\upload;
- use extend\Upload as UploadExtend;
- use Intervention\Image\ImageManagerStatic as Image;
- use app\model\BaseModel;
- class Upload extends BaseModel
- {
- public $upload_path = __UPLOAD__;//公共上传文件
- public $config = []; //上传配置
- public $site_id;
- public $rule_type;//允许上传 mime类型
- public $rule_ext;// 允许上传 文件后缀
- public $path;//上传路径
- public function __construct($site_id = 0)
- {
- $this->site_id = $site_id;
- $config_model = new Config();
- $config_result = $config_model->getUploadConfig();
- $this->config = $config_result["data"]["value"];//上传配置
- }
- /************************************************************上传开始*********************************************/
- /**
- * 单图上传
- * @param number $site_id
- * @param string $thumb_type 生成缩略图类型
- */
- public function image($param)
- {
- $check_res = $this->checkImg();
- if($check_res["code"] >= 0){
- $file = request()->file($param["name"]);
- if(empty($file))
- return $this->error();
- $tmp_name = $file->getPathname();//获取上传缓存文件
- $original_name = $file->getOriginalName();//文件原名
- // $file_path = $this->upload_path."/".$this->site_id . "/images/".date("Ymd"). '/';
- $file_path = $this->path;
- // 检测目录
- $checkpath_result = $this->checkPath($file_path);//验证写入文件的权限
- if($checkpath_result["code"] < 0)
- return $checkpath_result;
- $file_name = $file_path.$this->createNewFileName();
- $extend_name = $file->getOriginalExtension();
- $thumb_type = $param["thumb_type"];
- //原图保存
- $new_file = $file_name . "." . $extend_name;
- $image = Image::make($tmp_name);
- $width = $image->width();//图片宽
- $height = $image->height();//图片高
- $image = $this->imageWater($image);
- $result = $this->fileCloud($image, $new_file);//原图云上传(文档流上传)
- if($result["code"] < 0)
- return $result;
- $thumb_res = $this->thumbBatch($tmp_name, $file_name, $extend_name, $thumb_type);//生成缩略图
- if($thumb_res["code"] < 0)
- return $result;
- $data = array(
- "pic_path" => $result["data"],//图片云存储
- "pic_name" => $original_name,
- "file_ext" => $extend_name,
- "pic_spec" => $width . "*" . $height,
- "update_time" =>time(),
- "site_id" => $this->site_id
- );
- return $this->success($data, "SUCCESS");
- }else{
- //返回错误信息
- return $check_res;
- }
- }
- /**
- * 相册图片上传
- * @param number $site_id
- * @param number $category_id
- * @param string $thumb_type
- */
- public function imageToAlbum($param)
- {
- $check_res = $this->checkImg();
- if($check_res["code"] >= 0){
- $file = request()->file($param["name"]);
- if(empty($file))
- return $this->error();
- $tmp_name = $file->getPathname();//获取上传缓存文件
- $original_name = $file->getOriginalName();//文件原名
- // $file_path = $this->upload_path."/".$this->site_id . "/images/".date("Ymd"). '/';
- $file_path = $this->path;
- // 检测目录
- $checkpath_result = $this->checkPath($file_path);//验证写入文件的权限
- if($checkpath_result["code"] < 0)
- return $checkpath_result;
- $file_name = $file_path.$this->createNewFileName();
- $extend_name = $file->getOriginalExtension();
- $thumb_type = $param["thumb_type"];//所留
- $album_id = $param["album_id"];
- //原图保存
- $new_file = $file_name . "." . $extend_name;
- $image = Image::make($tmp_name);
- $width = $image->width();//图片宽
- $height = $image->height();//图片高
- // $image = $this->imageWater($image);
- $result = $this->fileCloud($image, $new_file);//原图云上传(文档流上传)
- if($result["code"] < 0)
- return $result;
- $thumb_res = $this->thumbBatch($tmp_name, $file_name, $extend_name, $thumb_type);//生成缩略图
- if($thumb_res["code"] < 0)
- return $result;
- $pic_name_first = substr(strrchr($original_name, '.'), 1);
- $pic_name = basename($original_name,".".$pic_name_first);
- $data = array(
- "pic_path" => $result["data"],//图片云存储
- "pic_name" => $pic_name,
- "pic_spec" => $width . "*" . $height,
- "update_time" =>time(),
- "site_id" => $this->site_id,
- "album_id" => $album_id
- );
- $album_model = new Album();
- $res = $album_model->addAlbumPic($data);
- if ($res['code'] >= 0) {
- $data["id"] = $res["data"];
- return $this->success($data, "UPLOAD_SUCCESS");
- } else {
- return $this->error($res);
- }
- }else{
- //返回错误信息
- return $check_res;
- }
- }
- /**
- * 视频上传
- * @param $param
- */
- public function video($param){
- $check_res = $this->checkFile();
- if($check_res["code"] >= 0){
- // 获取表单上传文件
- $file = request()->file($param["name"]);
- try {
- $extend_name = $file->getOriginalExtension();
- $new_name = $this->createNewFileName().".".$extend_name;
- $file_path = $this->path;
- \think\facade\Filesystem::disk('public')->putFileAs( $file_path, $file, $new_name);
- $file_name = $file_path.$new_name;
- return $this->success(["path" => $file_name], "UPLOAD_SUCCESS");
- } catch (\think\exception\ValidateException $e) {
- return $this->error('', $e->getMessage());
- }
- }else{
- return $check_res;
- }
- }
- /**
- * 上传文件
- * @param $param
- */
- public function file($param){
- $check_res = $this->checkFile();
- if($check_res["code"] >= 0){
- // 获取表单上传文件
- $file = request()->file($param["name"]);
- try {
- $extend_name = $file->getOriginalExtension();
- $new_name = $this->createNewFileName().".".$extend_name;
- $file_path = $this->path;
- \think\facade\Filesystem::disk('public')->putFileAs( $file_path, $file, $new_name);
- $file_name = $file_path.$new_name;
- return $this->success(["path" => $file_name], "UPLOAD_SUCCESS");
- } catch (\think\exception\ValidateException $e) {
- return $this->error('', $e->getMessage());
- }
- }else{
- return $check_res;
- }
- }
- /************************************************************上传结束*********************************************/
- /************************************************************上传功能组件******************************************/
- /**
- * 缩略图生成
- * @param unknown $file_name
- * @param unknown $extend_name
- * @param unknown $thumb_type
- * @return Ambigous <string, multitype:multitype:string >
- */
- private function thumbBatch($file_path, $file_name, $extend_name, $thumb_type = [])
- {
- $thumb_type_array = array(
- "big" => array(
- "size" => "big",
- "width" => $this->config["thumb"]["thumb_big_width"],
- "height" => $this->config["thumb"]["thumb_big_height"],
- "thumb_name" => ""
- ),
- "mid" => array(
- "size" => "mid",
- "width" => $this->config["thumb"]["thumb_mid_width"],
- "height" => $this->config["thumb"]["thumb_mid_height"],
- "thumb_name" => ""
- ),
- "small" => array(
- "size" => "small",
- "width" => $this->config["thumb"]["thumb_small_width"],
- "height" => $this->config["thumb"]["thumb_small_height"],
- "thumb_name" => ""
- )
- );
- foreach ($thumb_type_array as $k => $v) {
- if (!empty($thumb_type) && in_array($k, $thumb_type)) {
- $new_path_name = $file_name . "_" . $v["size"] . "." . $extend_name;
- $result = $this->imageThumb($file_path, $new_path_name, $v["width"], $v["height"]);
- //返回生成的缩略图路径
- if ($result["code"] >= 0) {
- $thumb_type_array[ $k ]["thumb_name"] = $new_path_name;
- } else {
- return $result;
- }
- }
- }
- return $this->success($thumb_type_array);
- }
- /**
- * 缩略图
- * @param unknown $file_name
- * @param unknown $new_path
- * @param unknown $width
- * @param unknown $height
- * @return multitype:boolean unknown |multitype:boolean
- */
- public function imageThumb($file, $thumb_name, $width, $height)
- {
- $image = Image::make($file)->fit($width, $height,function ($constraint) {
- // $constraint->upsize();//防止图片放大 不需要
- }, $this->config["thumb"]["thumb_position"]);
- $image = $this->imageWater($image);
- $result = $this->fileCloud($image, $thumb_name);
- return $result;
- }
- /**
- * 添加水印
- */
- public function imageWater($image)
- {
- //判断是否有水印(具体走配置)
- if($this->config["water"]["is_watermark"]){
- switch ($this->config["water"]["watermark_type"]) {
- case "1"://图片水印
- if(!empty($this->config["water"]["watermark_source"]) && is_file($this->config["water"]["watermark_source"])){
- $watermark = Image::make($this->config["water"]["watermark_source"]);
- $image->insert($watermark, $this->config["water"]["watermark_position"], $this->config["water"]["watermark_x"], $this->config["water"]["watermark_y"]);
- }
- break;
- case "2"://文字水印
- if(!empty($this->config["water"]["watermark_text"])){
- $image->text($this->config["water"]["watermark_text"], $this->config["water"]["watermark_x"], $this->config["water"]["watermark_y"], function($font) {
- // $font->file($this->config["water"]["watermark_text_file"]);//设置字体文件位置
- $font->size($this->config["water"]["watermark_text_size"]);//设置字号大小
- $font->color($this->config["water"]["watermark_text_color"]);//设置字号颜色
- $font->align($this->config["water"]["watermark_text_align"]);//设置字号水平位置
- $font->valign($this->config["water"]["watermark_text_valign"]);//设置字号 垂直位置
- $font->angle($this->config["water"]["watermark_text_angle"]);//设置字号倾斜角度
- });
- }
- break;
- }
- }
- return $image;
- }
- /**
- * 删除文件
- * @param $file_name
- */
- private function deleteFile($file_name)
- {
- $res = @unlink($file_name);
- if ($res) {
- return $this->success();
- } else {
- return $this->error();
- }
- }
- /**
- * 云上传
- */
- public function fileCloud($image, $file){
- try {
- //走 云上传
- //...
- $put_result = event("Put",["file_path" => $file, "key" => $file], true);
- if(!empty($put_result)){
- $this->deleteFile($file);
- if($put_result["code"] >= 0){
- $file = $put_result["data"]["path"];
- }else{
- return $put_result;
- }
- }else{
- $image->save($file);
- }
- //云上传没有成功 保存到本地
- return $this->success($file, "UPLOAD_SUCCESS");
- } catch (\Exception $e) {
- return $this->error('', $e->getMessage());
- }
- }
- /**
- * 检测图片上传 类型 大小
- * @param $file_info
- * @return \multitype
- */
- public function checkImage1($file_info){
- $upload_extend = new UploadExtend('');//实例化上传类
- $upload_extend->setFilename($file_info["tmp_name"]);
- $rule_type = $this->config["upload"]["image_allow_mime"];//规则mine类型
- $rule_ext = $this->config["upload"]["image_allow_ext"];//规则 允许上传后缀
- // $rule = [ "type" => "image/png,image/jpeg,image/gif,image/bmp", "ext" => "gif,jpg,jpeg,bmp,png" ];//上传文件验证规则
- $rule = [ "type" => $rule_type, "ext" => $rule_ext ];//上传文件验证规则
- $old_name = $upload_extend->getFileName($file_info["name"]);//文件原名
- $file_name = $this->site_id . "/images/".date("Ymd"). "/". $upload_extend->createNewFileName();
- $extend_name = $upload_extend->getFileExt($file_info["name"]);
- $size_data = $upload_extend->getImageInfo($file_info["tmp_name"]);//获取图片信息
- $check = $upload_extend->setValidate($rule)->setUploadInfo($file_info)->checkAll($this->upload_path, $file_name.".".$extend_name);
- if(!$check)
- return $this->error("", $upload_extend->getError());
- $data = array(
- "file_name" => $file_name,
- "old_name" => $old_name,
- "extend_name" => $extend_name,
- "size_data" => $size_data,
- );
- return $this->success($data);
- }
- /**
- * 图片验证
- * @param $file
- * @return \multitype
- */
- public function checkImg(){
- try {
- $file = request()->file();
- $rule_array = [];
- $size_rule = $this->config["upload"]["max_filesize"];
- $ext_rule = $this->config["upload"]["image_allow_ext"];
- $mime_rule = $this->config["upload"]["image_allow_mime"];
- // $size_rule = 10240;
- // $ext_rule = "jpg,jpeg,png,gif,pem";
- // $mime_rule = "image/jpeg,image/gif,image/png,text/plain";
- if(!empty($size_rule)){
- $rule_array[] = "fileSize:{$size_rule}";
- }
- if(!empty($ext_rule)){
- $rule_array[] = "fileExt:{$ext_rule}";
- }
- if(!empty($mime_rule)){
- $rule_array[] = "fileMime:{$mime_rule}";
- }
- if(!empty($rule_array)){
- // 'image'=>'filesize:10240|fileExt:jpg,jpeg,png,gif,pem|fileMime:image/jpeg,image/gif,image/png,text/plain'
- $rule = implode("|", $rule_array);
- validate(['file'=>$rule])->check($file);
- }
- return $this->success();
- } catch (\think\exception\ValidateException $e) {
- return $this->error('', $e->getMessage());
- }
- }
- /**
- * 图片验证
- * @param $file
- * @return \multitype
- */
- public function checkFile(){
- try {
- $file = request()->file();
- $rule_array = [];
- $size_rule = $this->config["upload"]["max_filesize"];
- // $ext_rule = $this->config["upload"]["image_allow_ext"];
- // $mime_rule = $this->config["upload"]["image_allow_mime"];
- // $size_rule = 10240*100;
- // $ext_rule = "jpg,jpeg,png,gif,pem";
- // $mime_rule = "image/jpeg,image/gif,image/png,text/plain";
- // if(!empty($size_rule)){
- // $rule_array[] = "fileSize:{$size_rule}";
- // }
- // if(!empty($ext_rule)){
- // $rule_array[] = "fileExt:{$ext_rule}";
- // }
- // if(!empty($mime_rule)){
- // $rule_array[] = "fileMime:{$mime_rule}";
- // }
- // $rule = implode("|", $rule_array);
- // 'image'=>'filesize:10240|fileExt:jpg,jpeg,png,gif,pem|fileMime:image/jpeg,image/gif,image/png,text/plain'
- // $res = validate(['file'=>$rule])->check($file);
- // if($res){
- // return $this->success();
- // }else{
- // return $this->error();
- // }
- return $this->success();
- } catch (\think\exception\ValidateException $e) {
- echo $e->getMessage();
- }
- }
- /************************************************************上传功能组件******************************************/
- /**
- *获取一个新文件名
- */
- public function createNewFileName()
- {
- $name = date('Ymdhis', time())
- . sprintf('%03d', microtime(true) * 1000)
- . sprintf('%02d', mt_rand(10, 99));
- return $name;
- }
- /**
- * 验证目录是否可写
- * @param unknown $path
- * @return boolean
- */
- public function checkPath($path)
- {
- if (is_dir($path) || mkdir($path, 0755, true)) {
- return $this->success();
- }
- return $this->error('', "directory {$path} creation failed");
- }
- /**
- * 设置上传目录
- * @param $path
- */
- public function setPath($path){
- if($this->site_id > 0){
- $this->path = $this->site_id."/".$path;
- }else{
- $this->path = $path;
- }
- $this->path = $this->upload_path."/".$this->path;
- return $this;
- }
- /**
- * 远程拉取图片
- * @param $path
- */
- public function remotePull($path){
- $file_path = $this->path;
- // 检测目录
- $checkpath_result = $this->checkPath($file_path);//验证写入文件的权限
- if($checkpath_result["code"] < 0)
- return $checkpath_result;
- $file_name = $file_path.$this->createNewFileName();
- $new_file = $file_name.".png";
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $path);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
- $file = curl_exec($ch);
- curl_close($ch);
- // $resource = fopen($new_file, 'a');
- // fwrite($resource, $file);
- // fclose($resource);
- //
- // $image = Image::make($new_file);
- $image = Image::make($file);
- $image = $this->imageWater($image);
- $result = $this->fileCloud($image, $new_file);//原图云上传(文档流上传)
- if($result["code"] < 0)
- return $result;
- return $this->success(["pic_path" => $result["data"]]);
- }
- }
|