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); } }