LMUploadFile.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2016/1/28
  6. * Time: 16:16
  7. */
  8. namespace common\library;
  9. use yii\base\Model;
  10. use Yii;/*ImageResize*/
  11. use common\library\ImageResize;
  12. class LMUploadFile extends Model
  13. {
  14. public $imageFile;
  15. public $filepath;
  16. public $fileabbrepath;
  17. public $uploaderror;
  18. public function rules()
  19. {
  20. return [
  21. [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png,jpg,gif,bmp,webp,jpeg,ai,psd,cdr,doc,txt,zip','checkExtensionByMimeType' => false],
  22. ];
  23. }
  24. public function upload($type,$flag=true,$x=160,$y=160)
  25. {
  26. $file_path=Yii::getAlias('@common/allupload')."/".$type."/".date("Ym")."/".date("d")."/";
  27. $picture_path=time().rand(0,10000000).'.' . $this->imageFile->extension;
  28. $this->filepath="/".$type."/".date("Ym")."/".date("d")."/".$picture_path;//原图地址
  29. $this->fileabbrepath ="/".$type."/".date("Ym")."/".date("d")."/".'s'.$picture_path; //缩略图地址
  30. // return $this->imageFile->extension;
  31. if($this->validate()){
  32. if(!is_dir($file_path)){
  33. mkdir($file_path,0777,true);
  34. }
  35. $this->imageFile->saveAs($file_path.$picture_path);
  36. if($flag){
  37. //生成缩略图
  38. $result = self::uploadresize($file_path.'/'.$picture_path,$file_path.'/s'.$picture_path,$x,$y);
  39. if(!$result){
  40. $this->uploaderror = '生成缩略图失败!';
  41. return false;
  42. }
  43. }
  44. return true;
  45. }else{
  46. $this->uploaderror = "图片上传失败!";
  47. return false;
  48. }
  49. }
  50. /**
  51. * @param $serverids
  52. * @param bool $flag
  53. * @param int $x
  54. * @param int $y
  55. * @return array|bool
  56. */
  57. public function uploads($serverids,$flag=true){
  58. $file_path=Yii::getAlias('@common/allupload')."/"."7474"."/".date("Ym")."/".date("d")."/";
  59. $picture_path=time().rand(0,10000000).'.' . $this->imageFile->extension;
  60. $this->filepath="/"."7474"."/".date("Ym")."/".date("d")."/".$picture_path;//原图地址
  61. // $this->fileabbrepath ="/".$type."/".date("Ym")."/".date("d")."/".'s'.$picture_path; //缩略图地址
  62. // return $this->imageFile->extension;
  63. if($this->validate()){
  64. if(!is_dir($file_path)){
  65. mkdir($file_path,0777,true);
  66. }
  67. $this->imageFile->saveAs($file_path.$picture_path);
  68. // if($flag){
  69. // //生成缩略图
  70. // $result = self::uploadresize($file_path.'/'.$picture_path,$file_path.'/s'.$picture_path,$x,$y);
  71. // if(!$result){
  72. // $this->uploaderror = '生成缩略图失败!';
  73. // return false;
  74. // }
  75. // }
  76. return true;
  77. }else{
  78. $this->uploaderror = "图片上传失败!";
  79. return false;
  80. }
  81. // return print_r($this->imageFile);
  82. // $imgs = [];
  83. // $i = 0;
  84. // if(is_array($this->imageFile)){
  85. //
  86. // foreach($this->imageFile as $serverid){
  87. // $i ++;
  88. // $res = self::data($serverid,$flag);
  89. // if(!$res){
  90. // return false;
  91. // }
  92. // $imgs[] = $res;
  93. // }
  94. // return $imgs;
  95. // }else{
  96. // return false;
  97. // }
  98. }
  99. /**
  100. * @param $serverids
  101. * @param bool $flag
  102. * @param int $x
  103. * @param int $y
  104. * @return array|bool
  105. */
  106. static function installImages($serverids,$flag=true,$x= 160,$y=160){
  107. $imgs = [];
  108. $i = 0;
  109. if(is_array($serverids)){
  110. foreach($serverids as $serverid){
  111. $i ++;
  112. $res = self::wechatUploaImage($serverid,'install',$flag,$x,$y);
  113. if(!$res){
  114. return false;
  115. }
  116. $imgs[] = $res;
  117. }
  118. return $imgs;
  119. }else{
  120. return false;
  121. }
  122. }
  123. /**
  124. * 微信图片上传
  125. * @param $serverid
  126. * @param $type
  127. * @param bool $flag
  128. * @param int $x
  129. * @param int $y
  130. * @return array|bool
  131. */
  132. public function wechatUploaImage($serverid,$type,$flag=true,$x=160,$y=160)
  133. {
  134. $content = self::get_media($serverid);
  135. if(!$content){
  136. return false;
  137. }
  138. $userid = \Yii::$app->user->id;
  139. $file_path = \Yii::getAlias('@common/allupload').'/'.$type.'/'.date('Ym').'/'.date('d').'/'.$userid."/";
  140. $picture_path=time().rand(0,100000).'.jpg';
  141. $filepath="/".$type."/".date("Ym")."/".date("d")."/".$userid."/".$picture_path;//原图地址
  142. $fileabbrepath ="/".$type."/".date("Ym")."/".date("d")."/".$userid."/".$picture_path; //缩略图地址
  143. if(!is_dir($file_path)){
  144. mkdir($file_path,0777,true);
  145. }
  146. if(!file_put_contents($file_path.$picture_path, $content)){
  147. return false;
  148. };
  149. if($flag){
  150. $result = self::uploadresize($file_path.$picture_path,$file_path.'s'.$picture_path,$x,$y);
  151. if(!$result){
  152. $fileabbrepath = '';
  153. }
  154. }
  155. return ['arche'=>$filepath,'remote'=>$fileabbrepath];
  156. }
  157. //从微信服务器上获取媒体文件
  158. static function get_media($media_id){
  159. $access_token = \Yii::$app->cache->get('access_token');
  160. //刷新access_token
  161. if(empty($access_token)){
  162. $wechat = new WeChat();
  163. $access_token = $wechat->gzAccess_token();
  164. \Yii::$app->cache->set('access_token',$access_token,3600);
  165. }
  166. $url = 'https://api.weixin.qq.com/cgi-bin/media/get?access_token='.$access_token.'&media_id='.$media_id;
  167. $content = file_get_contents($url);
  168. return $content;
  169. }
  170. /**
  171. * 生成缩略图
  172. * @param $localimg
  173. * @param $remoteimg
  174. * @param int $x
  175. * @param int $y
  176. * @return bool
  177. */
  178. static function uploadresize($localimg,$remoteimg,$x = 160,$y = 160){
  179. $resize = new ImageResize();
  180. $result = $resize->resize($localimg,$remoteimg,$x,$y);
  181. if(!$result){
  182. return false;
  183. }
  184. return true;
  185. }
  186. }