123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2016/1/28
- * Time: 16:16
- */
- namespace common\library;
- use yii\base\Model;
- use Yii;/*ImageResize*/
- use common\library\ImageResize;
- class LMUploadFile extends Model
- {
- public $imageFile;
- public $filepath;
- public $fileabbrepath;
- public $uploaderror;
- public function rules()
- {
- return [
- [['imageFile'], 'file', 'skipOnEmpty' => 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;
- }
- }
|