'y', 2=>'n']; public function getImgResources($path,$url) { try{ $aext = explode('.', $url); $ext = end($aext); $name = md5(uniqid(microtime())) . '.' . $ext; $source = @file_get_contents($url); if($source){ if( file_put_contents($path.$name,$source,FILE_USE_INCLUDE_PATH)) { return $name; } } }catch (\Exception $e){ // p($e->getMessage()); } return ; $state = @file_get_contents($url,0,null,0,1);//获取网络资源的字符内容 if($state){ $dir=dirname(Yii::$app->BasePath).'/resource/'.Yii::$app->params['httpImg']['newsthumb']; $filename = $dir.date("dMYHis").'.jpg';//文件名称生成 ob_start();//打开输出 readfile($url);//输出图片文件 $img = ob_get_contents();//得到浏览器输出 ob_end_clean();//清除输出并关闭 $size = strlen($img);//得到图片大小 $fp2 = @fopen($filename, "a"); //file_put_contents ($filename,$img); fwrite($fp2, $img);//向当前目录写入图片文件,并重新命名 fclose($fp2); return date("dMYHis").'.jpg'; } else{ return 0; } } public function CreateNews() { //处理图片 $url = Yii::$app->params['img_url']['news']; if(!empty($_FILES)){ $row = \backend\server\UploadFile::InstancesImgName('img',$url); //[0] => 3dc12b64b0efc4bdafa543e65c8a9ec2.jpg }else{ $row[0]=$this->getImgResources(Yii::$app->request->post('img')[0]); } if(is_array($row)) { $model = new News(); $model->scenario = 'add'; $model->thumb = json_encode($row); $newModel = $model->Authenticator(Yii::$app->request->post()); if(is_object($newModel)) { //处理水印 if(Yii::$app->request->post('thumb_watermark') == 1 ) { $ImgSet = new SetUploadImg(); $ImgSet->SetImgWatermark($row,$url); } //压缩图片 $compressParams = []; $compressParams['data']['imgname'] = $row;; $compressParams['data']['url'] = $url; $compressParams['data']['source'] = '资讯@添加'; $this->onCompress($compressParams); if($newModel->insert() == true) { $UserNews = new \common\models\UserNews(); $UserNews->uid = Yii::$app->session['user_info']['uid']; $UserNews->nid = $newModel->attributes['id']; $UserNews->create_at = $_SERVER['REQUEST_TIME']; $UserNews->insert(); return true; } else { UploadFile::delImg($url,$row); } } return $newModel; } return $row; } public function NewsEdit() { $model = new News(); $validateData = $model->Authenticator(Yii::$app->request->post()); if(is_object($validateData)) { $row = $model->FindById(Yii::$app->request->post('id')); if($row != null) { $delIdentifying = false; $row['thumb'] = json_decode($row['thumb'],true); $delimg = Yii::$app->request->post('delimg'); //需要删除的数组 $ImgUrl = Yii::$app->params['img_url']['news']; $arrImg = []; if(!empty($delimg)) { $arrImg = array_diff($row['thumb'],$delimg); //需要保留的数组 如果删除全部图片 返回空数组 if(empty($arrImg)) { $delIdentifying = true; //标识是否删除所有图片 } foreach ($delimg as $val) { UploadFile::delImg($ImgUrl,$val); } } else { $arrImg = $row['thumb']; } //$imgArr = \backend\server\UploadFile::InstancesImgName('img',$ImgUrl); if(!empty($_FILES)){ $imgArr = \backend\server\UploadFile::InstancesImgName('img',$ImgUrl); //[0] => 3dc12b64b0efc4bdafa543e65c8a9ec2.jpg }else{ $imgArr[0]=$this->getImgResources(Yii::$app->request->post('img')[0]); if($imgArr[0]==0){ $imgArr = \backend\server\UploadFile::InstancesImgName('img',$ImgUrl); } } if(is_array($imgArr)) { $arrImg = array_merge($imgArr,$arrImg); if(Yii::$app->request->post('thumb_watermark') == 1 ) { $ImgSet = new SetUploadImg(); $ImgSet->SetImgWatermark($imgArr,$ImgUrl); } //压缩图片 $compressParams = []; $compressParams['data']['imgname'] = $imgArr;; $compressParams['data']['url'] = $ImgUrl; $compressParams['data']['source'] = '资讯@修改'; $this->onCompress($compressParams); } $setAttr = Help::SetAttr(Yii::$app->request->post(),$validateData,$row); if(!empty($arrImg) || $delIdentifying == true) { $arrImg=array_values($arrImg); $setAttr->thumb = json_encode($arrImg); } else { $setAttr->thumb = json_encode($setAttr->thumb); //没有修改图片的情况下 } if($setAttr->update() == true) return true; } } return $validateData; } public function NewsList() { $model = new News(); $model->id = Yii::$app->request->post('id'); // $model->hid = Yii::$app->request->post('hid'); $model->subject = Yii::$app->request->post('name'); $model->category = Yii::$app->request->post('news'); $model->city = Yii::$app->request->post('city'); $model->author = Yii::$app->request->post('author'); $rows['data'] = $model->getList(Yii::$app->request->post()); $rows['count'] = $model->Total(); if($rows != null) { foreach ($rows['data'] as &$val) { $val['create_at'] = date('Y-m-d',$val['create_at']); $val['state'] =Yii::$app->params['newsStatus'][$val['state']]; } return $rows; } return false; } public function delNews($swith = 1) { $id = Yii::$app->request->post('id'); switch ($swith){ case 1: $result = \common\models\News::updateAll(['del'=>2],['id'=>$id]); // $row->del = 2; // if(is_array($id)) // { // // } \common\models\HouseNews::updateAll(['del'=>2],['nid'=>$id]); break; case 2: $model = new News(); $row = $model->FindByIdSort($id); if($row == null) return; if($row->state == 2) { $row->state = 1; } else if($row->state == 1) { $row->state = 2; } $result = $row->update(); break; } if($result > 0 ) return true; } // public function SetNewsState() // { // $model = new News(); // $row = $model->FindById(Yii::$app->request->post('id')); // $row->state = 2; // } //压缩图片 private function onCompress($data) { $model = new \backend\event\TinifyEvent(); $model->CompressImg($data); } }