123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2018/2/27/027
- * Time: 15:31
- */
- namespace backend\server;
- use yii\web\UploadedFile;
- use Yii;
- //use yii\base\Model;
- use yii\base\DynamicModel;
- class UploadFile
- {
- // public static function Radio($path,$model)
- // {
- // if(empty($path) || !is_object($model)) return false;
- // if(!is_dir($path)){
- // mkdir($path,0777,true);
- // chmod($path,0777);
- // }
- // $imgName = md5(uniqid()).'.'.$model->extension;
- // if($model->saveAs($path.$imgName) === true)
- // {
- // return $imgName;
- // }
- // return false;
- // }
- /*
- * 单张
- * */
- public static function InstanceImgName($name,$path)
- {
- if(empty($path)) return false;
- if(!is_dir($path)){
- mkdir($path,0777,true);
- chmod($path,0777);
- }
- $row = UploadedFile::getInstanceByName($name);
- if(is_object($row))
- {
- $validator = new \yii\validators\ImageValidator();
- $validator->extensions = ['png','jpg','jpeg'];
- $validator->maxSize = 1024*1024*2; //2M
- if(!$validator->validate($row,$error))return false;
- $name = md5(uniqid(microtime())).'.'.$row->extension;
- if($row->saveAs($path.$name) == true) return $name;
- }
- return false;
- }
- /*
- * 重写
- * */
- public static function InstanceImgName_v1($name,$path)
- {
- if(empty($path)) throw new \Exception('请添加图片路径');
- if(!is_dir($path)){
- mkdir($path,0777,true);
- chmod($path,0777);
- }
- $row = UploadedFile::getInstanceByName($name);
- if(is_object($row))
- {
- $validator = new \yii\validators\ImageValidator();
- switch ($path)
- {
- case Yii::$app->params['img_url']['push_lb'] :
- $validator->extensions = ['png','jpg','jpeg'];
- $validator->maxSize = 1024*1024*0.3; //2KB
- break;
- case Yii::$app->params['img_url']['push_lb']:
- $validator->extensions = ['png','jpg','jpeg'];
- $validator->maxSize = 1024*1024*0.3; //3KB
- break;
- default:
- $validator->extensions = ['png','jpg','jpeg'];
- $validator->maxSize = 1024*1024*2; //2M
- break;
- }
- if(!$validator->validate($row,$error))
- {
- throw new \Exception($error);
- }
- $name = md5(uniqid(microtime())).'.'.$row->extension;
- if($row->saveAs($path.$name) == true) return $name;
- }
- throw new \Exception('图片添加失败');
- }
- /*
- * 重写多张
- * */
- public static function InstancesImgName_v1($name,$path)
- {
- if(empty($path)) throw new \Exception('请添加图片路径');
- if(!is_dir($path)){
- mkdir($path,0777,true);
- chmod($path,0777);
- }
- $arr = [];
- $row = UploadedFile::getInstancesByName($name);
- switch ($path)
- {
- case Yii::$app->params['img_url']['news']: //资讯
- $validator = new \yii\validators\ImageValidator();
- $validator->extensions = ['png','jpg','jpeg'];
- $validator->maxSize = 1024*1024*0.3;
- $validator->maxFiles = 3;
- if(empty($row)) throw new \Exception('请上传图片');
- // $validator->notImage = '请上传图片';
- break;
- default:
- $validator = new \yii\validators\ImageValidator();
- $validator->extensions = ['png','jpg','jpeg'];
- $validator->maxSize = 1024*1024*0.5;
- break;
- }
- foreach ($row as $k=>$v)
- {
- if(!$validator->validate($row[$k],$error))
- {
- throw new \Exception($error);
- }
- $name = md5(uniqid(microtime())).'.'.$row[$k]->extension;
- if($row[$k]->saveAs($path.$name) == true)
- {
- $arr[$k] = $name;
- }
- }
- if(!empty($arr)) return $arr;
- throw new \Exception('图片添加失败');
- }
- public static function InstancesImgName($name,$path)
- {
- $arr = [];
- $row = UploadedFile::getInstancesByName($name);
- if(empty($row[0])){
- return '请上传图片';
- }
- $validator = new \yii\validators\ImageValidator();
- $validator->extensions = ['png','jpg','jpeg'];
- $validator->maxSize = 1024*1024*2; //2M
- foreach ($row as $k=>$v)
- {
- if(!is_object($row[$k])) return false;
- if(!$validator->validate($row[$k],$error))
- {
- return $error;
- }
- $name = md5(uniqid(microtime())).'.'.$row[$k]->extension;
- if($row[$k]->saveAs($path.$name) == true)
- {
- $arr[$k] = $name;
- }
- }
- if(!empty($arr)) return $arr;
- }
- /*
- * 删除临时目录里面的图片
- * */
- public static function delImg($path,$name)
- {
- @unlink($path.$name);
- }
- }
|