false, 'extensions' => 'png,jpg,gif,bmp,webp,jpeg,ai,psd,cdr,doc,txt,zip','checkExtensionByMimeType' => false], ]; } public function upload($type,$flag=true,$x=160,$y=160) { $file_path=Yii::getAlias('@common/allupload')."/".$type."/".date("Ym")."/".date("d")."/"; $picture_path=time().rand(0,10000000).'.' . $this->imageFile->extension; $this->filepath="/".$type."/".date("Ym")."/".date("d")."/".$picture_path;//原图地址 $this->fileabbrepath ="/".$type."/".date("Ym")."/".date("d")."/".'s'.$picture_path; //缩略图地址 // return $this->imageFile->extension; if($this->validate()){ if(!is_dir($file_path)){ mkdir($file_path,0777,true); } $this->imageFile->saveAs($file_path.$picture_path); if($flag){ //生成缩略图 $result = self::uploadresize($file_path.'/'.$picture_path,$file_path.'/s'.$picture_path,$x,$y); if(!$result){ $this->uploaderror = '生成缩略图失败!'; return false; } } return true; }else{ $this->uploaderror = "图片上传失败!"; return false; } } /** * @param $serverids * @param bool $flag * @param int $x * @param int $y * @return array|bool */ public function uploads($serverids,$flag=true){ $file_path=Yii::getAlias('@common/allupload')."/"."7474"."/".date("Ym")."/".date("d")."/"; $picture_path=time().rand(0,10000000).'.' . $this->imageFile->extension; $this->filepath="/"."7474"."/".date("Ym")."/".date("d")."/".$picture_path;//原图地址 // $this->fileabbrepath ="/".$type."/".date("Ym")."/".date("d")."/".'s'.$picture_path; //缩略图地址 // return $this->imageFile->extension; if($this->validate()){ if(!is_dir($file_path)){ mkdir($file_path,0777,true); } $this->imageFile->saveAs($file_path.$picture_path); // if($flag){ // //生成缩略图 // $result = self::uploadresize($file_path.'/'.$picture_path,$file_path.'/s'.$picture_path,$x,$y); // if(!$result){ // $this->uploaderror = '生成缩略图失败!'; // return false; // } // } return true; }else{ $this->uploaderror = "图片上传失败!"; return false; } // return print_r($this->imageFile); // $imgs = []; // $i = 0; // if(is_array($this->imageFile)){ // // foreach($this->imageFile as $serverid){ // $i ++; // $res = self::data($serverid,$flag); // if(!$res){ // return false; // } // $imgs[] = $res; // } // return $imgs; // }else{ // return false; // } } /** * @param $serverids * @param bool $flag * @param int $x * @param int $y * @return array|bool */ static function installImages($serverids,$flag=true,$x= 160,$y=160){ $imgs = []; $i = 0; if(is_array($serverids)){ foreach($serverids as $serverid){ $i ++; $res = self::wechatUploaImage($serverid,'install',$flag,$x,$y); if(!$res){ return false; } $imgs[] = $res; } return $imgs; }else{ return false; } } /** * 微信图片上传 * @param $serverid * @param $type * @param bool $flag * @param int $x * @param int $y * @return array|bool */ public function wechatUploaImage($serverid,$type,$flag=true,$x=160,$y=160) { $content = self::get_media($serverid); if(!$content){ return false; } $userid = \Yii::$app->user->id; $file_path = \Yii::getAlias('@common/allupload').'/'.$type.'/'.date('Ym').'/'.date('d').'/'.$userid."/"; $picture_path=time().rand(0,100000).'.jpg'; $filepath="/".$type."/".date("Ym")."/".date("d")."/".$userid."/".$picture_path;//原图地址 $fileabbrepath ="/".$type."/".date("Ym")."/".date("d")."/".$userid."/".$picture_path; //缩略图地址 if(!is_dir($file_path)){ mkdir($file_path,0777,true); } if(!file_put_contents($file_path.$picture_path, $content)){ return false; }; if($flag){ $result = self::uploadresize($file_path.$picture_path,$file_path.'s'.$picture_path,$x,$y); if(!$result){ $fileabbrepath = ''; } } return ['arche'=>$filepath,'remote'=>$fileabbrepath]; } //从微信服务器上获取媒体文件 static function get_media($media_id){ $access_token = \Yii::$app->cache->get('access_token'); //刷新access_token if(empty($access_token)){ $wechat = new WeChat(); $access_token = $wechat->gzAccess_token(); \Yii::$app->cache->set('access_token',$access_token,3600); } $url = 'https://api.weixin.qq.com/cgi-bin/media/get?access_token='.$access_token.'&media_id='.$media_id; $content = file_get_contents($url); return $content; } /** * 生成缩略图 * @param $localimg * @param $remoteimg * @param int $x * @param int $y * @return bool */ static function uploadresize($localimg,$remoteimg,$x = 160,$y = 160){ $resize = new ImageResize(); $result = $resize->resize($localimg,$remoteimg,$x,$y); if(!$result){ return false; } return true; } }