UtilService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/02
  6. */
  7. namespace crmeb\services;
  8. use crmeb\services\storage\COS;
  9. use crmeb\services\storage\OSS;
  10. use crmeb\services\storage\Qiniu;
  11. use think\facade\Config;
  12. use dh2y\qrcode\QRcode;
  13. class UtilService
  14. {
  15. public static function postMore($params,$request = null,$suffix = false)
  16. {
  17. if($request === null) $request = app('request');
  18. $p = [];
  19. $i = 0;
  20. foreach ($params as $param){
  21. if(!is_array($param)) {
  22. $p[$suffix == true ? $i++ : $param] = $request->param($param);
  23. }else{
  24. if(!isset($param[1])) $param[1] = null;
  25. if(!isset($param[2])) $param[2] = '';
  26. $name = is_array($param[1]) ? $param[0].'/a' : $param[0];
  27. $p[$suffix == true ? $i++ : (isset($param[3]) ? $param[3] : $param[0])] = $request->param($name,$param[1],$param[2]);
  28. }
  29. }
  30. return $p;
  31. }
  32. public static function getMore($params,$request=null,$suffix = false)
  33. {
  34. if($request === null) $request = app('request');
  35. $p = [];
  36. $i = 0;
  37. foreach ($params as $param){
  38. if(!is_array($param)) {
  39. $p[$suffix == true ? $i++ : $param] = $request->param($param);
  40. }else{
  41. if(!isset($param[1])) $param[1] = null;
  42. if(!isset($param[2])) $param[2] = '';
  43. $name = is_array($param[1]) ? $param[0].'/a' : $param[0];
  44. $p[$suffix == true ? $i++ : (isset($param[3]) ? $param[3] : $param[0])] = $request->param($name,$param[1],$param[2]);
  45. }
  46. }
  47. return $p;
  48. }
  49. public static function encrypt($string, $operation = false, $key = '', $expiry = 0) {
  50. // 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙
  51. $ckey_length = 6;
  52. // 密匙
  53. $key = md5($key);
  54. // 密匙a会参与加解密
  55. $keya = md5(substr($key, 0, 16));
  56. // 密匙b会用来做数据完整性验证
  57. $keyb = md5(substr($key, 16, 16));
  58. // 密匙c用于变化生成的密文
  59. $keyc = $ckey_length ? ($operation == false ? substr($string, 0, $ckey_length):
  60. substr(md5(microtime()), -$ckey_length)) : '';
  61. // 参与运算的密匙
  62. $cryptkey = $keya.md5($keya.$keyc);
  63. $key_length = strlen($cryptkey);
  64. // 明文,前10位用来保存时间戳,解密时验证数据有效性,10到26位用来保存$keyb(密匙b),
  65. //解密时会通过这个密匙验证数据完整性
  66. // 如果是解码的话,会从第$ckey_length位开始,因为密文前$ckey_length位保存 动态密匙,以保证解密正确
  67. $string = $operation == false ? base64_decode(substr($string, $ckey_length)) :
  68. sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  69. $string_length = strlen($string);
  70. $result = '';
  71. $box = range(0, 255);
  72. $rndkey = array();
  73. // 产生密匙簿
  74. for($i = 0; $i <= 255; $i++) {
  75. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  76. }
  77. // 用固定的算法,打乱密匙簿,增加随机性,好像很复杂,实际上对并不会增加密文的强度
  78. for($j = $i = 0; $i < 256; $i++) {
  79. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  80. $tmp = $box[$i];
  81. $box[$i] = $box[$j];
  82. $box[$j] = $tmp;
  83. }
  84. // 核心加解密部分
  85. for($a = $j = $i = 0; $i < $string_length; $i++) {
  86. $a = ($a + 1) % 256;
  87. $j = ($j + $box[$a]) % 256;
  88. $tmp = $box[$a];
  89. $box[$a] = $box[$j];
  90. $box[$j] = $tmp;
  91. // 从密匙簿得出密匙进行异或,再转成字符
  92. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  93. }
  94. if($operation == false) {
  95. // 验证数据有效性,请看未加密明文的格式
  96. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) &&
  97. substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  98. return substr($result, 26);
  99. } else {
  100. return '';
  101. }
  102. } else {
  103. // 把动态密匙保存在密文里,这也是为什么同样的明文,生产不同密文后能解密的原因
  104. // 因为加密后的密文可能是一些特殊字符,复制过程可能会丢失,所以用base64编码
  105. return $keyc.str_replace('=', '', base64_encode($result));
  106. }
  107. }
  108. /**
  109. * 路径转url路径
  110. * @param $path
  111. * @return string
  112. */
  113. public static function pathToUrl($path)
  114. {
  115. return trim(str_replace(DS, '/', $path),'.');
  116. }
  117. /**
  118. * url转换路径
  119. * @param $url
  120. * @return string
  121. */
  122. public static function urlToPath($url)
  123. {
  124. $path = trim(str_replace('/',DS,$url),DS);
  125. if(0 !== strripos($path, 'public'))
  126. $path = 'public' . DS . $path;
  127. return app()->getRootPath().$path;
  128. }
  129. public static function timeTran($time)
  130. {
  131. $t = time() - $time;
  132. $f = array(
  133. '31536000' => '年',
  134. '2592000' => '个月',
  135. '604800' => '星期',
  136. '86400' => '天',
  137. '3600' => '小时',
  138. '60' => '分钟',
  139. '1' => '秒'
  140. );
  141. foreach ($f as $k => $v) {
  142. if (0 != $c = floor($t / (int)$k)) {
  143. return $c . $v . '前';
  144. }
  145. }
  146. }
  147. /**
  148. * 分级排序
  149. * @param $data
  150. * @param int $pid
  151. * @param string $field
  152. * @param string $pk
  153. * @param string $html
  154. * @param int $level
  155. * @param bool $clear
  156. * @return array
  157. */
  158. public static function sortListTier($data, $pid = 0, $field = 'pid', $pk = 'id', $html = '|-----', $level = 1, $clear = true)
  159. {
  160. static $list = [];
  161. if ($clear) $list = [];
  162. foreach ($data as $k => $res) {
  163. if ($res[$field] == $pid) {
  164. $res['html'] = str_repeat($html, $level);
  165. $list[] = $res;
  166. unset($data[$k]);
  167. self::sortListTier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
  168. }
  169. }
  170. return $list;
  171. }
  172. /**
  173. * 分级返回多维数组
  174. * @param $data
  175. * @param int $pid
  176. * @param string $field
  177. * @param string $pk
  178. * @param int $level
  179. * @return array
  180. */
  181. public static function getChindNode($data, $pid = 0, $field = 'pid', $pk = 'id', $level = 1)
  182. {
  183. static $list = [];
  184. foreach ($data as $k => $res) {
  185. if ($res['pid'] == $pid) {
  186. $list[] = $res;
  187. unset($data[$k]);
  188. self::getChindNode($data, $res['id'], $field, $pk, $level + 1);
  189. }
  190. }
  191. return $list;
  192. }
  193. /**分级返回下级所有分类ID
  194. * @param $data
  195. * @param string $children
  196. * @param string $field
  197. * @param string $pk
  198. * @return string
  199. */
  200. public static function getChildrenPid($data,$pid, $field = 'pid', $pk = 'id')
  201. {
  202. static $pids = '';
  203. foreach ($data as $k => $res) {
  204. if ($res[$field] == $pid) {
  205. $pids .= ','.$res[$pk];
  206. self::getChildrenPid($data, $res[$pk], $field, $pk);
  207. }
  208. }
  209. return $pids;
  210. }
  211. /**
  212. * 删除公告资源
  213. * @param $url
  214. * @return \StdClass
  215. */
  216. public static function rmPublicResource($url,$isPath = false)
  217. {
  218. $path = $isPath ? $url : realpath(self::urlToPath($url));
  219. if(!$path) return JsonService::fail('删除文件不存在!');
  220. if(!file_exists($path)) return JsonService::fail('删除路径不合法!');
  221. // if(0 !== strpos($path,app()->getRootPath().'public/uploads/')) return JsonService::fail('删除路径不合法!');
  222. if(!unlink($path)) return JsonService::fail('删除文件失败!');
  223. return JsonService::successful();
  224. }
  225. /**
  226. * 是否为微信内部浏览器
  227. * @return bool
  228. */
  229. public static function isWechatBrowser()
  230. {
  231. return (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false);
  232. }
  233. /**
  234. * 匿名处理
  235. * @param $name
  236. * @return string
  237. */
  238. public static function anonymity($name)
  239. {
  240. $strLen = mb_strlen($name,'UTF-8');
  241. $min = 3;
  242. if($strLen <= 1)
  243. return '*';
  244. if($strLen<= $min)
  245. return mb_substr($name,0,1,'UTF-8').str_repeat('*',$min-1);
  246. else
  247. return mb_substr($name,0,1,'UTF-8').str_repeat('*',$strLen-1).mb_substr($name,-1,1,'UTF-8');
  248. }
  249. /**
  250. * 身份证验证
  251. * @param $card
  252. * @return bool
  253. */
  254. public static function setCard($card){
  255. $city = [11=>"北京",12=>"天津",13=>"河北",14=>"山西",15=>"内蒙古",21=>"辽宁",22=>"吉林",23=>"黑龙江 ",31=>"上海",32=>"江苏",33=>"浙江",34=>"安徽",35=>"福建",36=>"江西",37=>"山东",41=>"河南",42=>"湖北 ",43=>"湖南",44=>"广东",45=>"广西",46=>"海南",50=>"重庆",51=>"四川",52=>"贵州",53=>"云南",54=>"西藏 ",61=>"陕西",62=>"甘肃",63=>"青海",64=>"宁夏",65=>"新疆",71=>"台湾",81=>"香港",82=>"澳门",91=>"国外 "];
  256. $tip = "";
  257. $match = "/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/";
  258. $pass= true;
  259. if(!$card || !preg_match($match,$card)){
  260. //身份证格式错误
  261. $pass = false;
  262. }else if(!$city[substr($card,0,2)]){
  263. //地址错误
  264. $pass = false;
  265. }else{
  266. //18位身份证需要验证最后一位校验位
  267. if(strlen($card) == 18){
  268. $card = str_split($card);
  269. //∑(ai×Wi)(mod 11)
  270. //加权因子
  271. $factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
  272. //校验位
  273. $parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
  274. $sum = 0;
  275. $ai = 0;
  276. $wi = 0;
  277. for ($i = 0; $i < 17; $i++)
  278. {
  279. $ai = $card[$i];
  280. $wi = $factor[$i];
  281. $sum += $ai * $wi;
  282. }
  283. $last = $parity[$sum % 11];
  284. if($parity[$sum % 11] != $card[17]){
  285. // $tip = "校验位错误";
  286. $pass =false;
  287. }
  288. }else{
  289. $pass =false;
  290. }
  291. }
  292. if(!$pass) return false;/* 身份证格式错误*/
  293. return true;/* 身份证格式正确*/
  294. }
  295. /**
  296. * TODO 砍价 拼团 分享海报生成
  297. * @param array $data
  298. * @param $path
  299. * @return array|bool|string
  300. * @throws \Exception
  301. */
  302. public static function setShareMarketingPoster($data = array(), $path){
  303. $config = array(
  304. 'text'=>array(
  305. array(
  306. 'text'=>$data['price'],//TODO 价格
  307. 'left'=>116,
  308. 'top'=>200,
  309. 'fontPath'=>app()->getRootPath().'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  310. 'fontSize'=>50, //字号
  311. 'fontColor'=>'255,0,0', //字体颜色
  312. 'angle'=>0,
  313. ),
  314. array(
  315. 'text'=>$data['label'],//TODO 标签
  316. 'left'=>394,
  317. 'top'=>190,
  318. 'fontPath'=>app()->getRootPath().'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  319. 'fontSize'=>24, //字号
  320. 'fontColor'=>'255,255,255', //字体颜色
  321. 'angle'=>0,
  322. ),
  323. array(
  324. 'text'=>$data['msg'],//TODO 简述
  325. 'left'=>80,
  326. 'top'=>270,
  327. 'fontPath'=>app()->getRootPath().'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  328. 'fontSize'=>22, //字号
  329. 'fontColor'=>'40,40,40', //字体颜色
  330. 'angle'=>0,
  331. )
  332. ),
  333. 'image'=>array(
  334. array(
  335. 'url'=>$data['image'], //图片
  336. 'stream'=>0,
  337. 'left'=>120,
  338. 'top'=>340,
  339. 'right'=>0,
  340. 'bottom'=>0,
  341. 'width'=>450,
  342. 'height'=>450,
  343. 'opacity'=>100
  344. ),
  345. array(
  346. 'url'=>$data['url'], //二维码资源
  347. 'stream'=>0,
  348. 'left'=>260,
  349. 'top'=>890,
  350. 'right'=>0,
  351. 'bottom'=>0,
  352. 'width'=>160,
  353. 'height'=>160,
  354. 'opacity'=>100
  355. )
  356. ),
  357. 'background'=>app()->getRootPath().'/public/static/poster/poster.jpg'
  358. );
  359. if(!file_exists($config['background'])) exception('缺少系统预设背景图片');
  360. if(strlen($data['title']) < 36){
  361. $text = array(
  362. 'text'=>$data['title'],//TODO 标题
  363. 'left'=>76,
  364. 'top'=>100,
  365. 'fontPath'=>app()->getRootPath().'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  366. 'fontSize'=>32, //字号
  367. 'fontColor'=>'0,0,0', //字体颜色
  368. 'angle'=>0,
  369. );
  370. array_push($config['text'],$text);
  371. }else{
  372. $titleOne = array(
  373. 'text'=>mb_substr($data['title'], 0, 12),//TODO 标题
  374. 'left'=>76,
  375. 'top'=>70,
  376. 'fontPath'=>app()->getRootPath().'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  377. 'fontSize'=>32, //字号
  378. 'fontColor'=>'0,0,0', //字体颜色
  379. 'angle'=>0,
  380. );
  381. $titleTwo = array(
  382. 'text'=> mb_substr($data['title'], 12, 12),//TODO 标题
  383. 'left'=>76,
  384. 'top'=>120,
  385. 'fontPath'=>app()->getRootPath().'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  386. 'fontSize'=>32, //字号
  387. 'fontColor'=>'0,0,0', //字体颜色
  388. 'angle'=>0,
  389. );
  390. array_push($config['text'],$titleOne);
  391. array_push($config['text'],$titleTwo);
  392. }
  393. return self::setSharePoster($config, $path);
  394. }
  395. /**
  396. * TODO 生成分享二维码图片
  397. * @param array $config
  398. * @param $path
  399. * @return array|bool|string
  400. * @throws \Exception
  401. */
  402. public static function setSharePoster($config = array(), $path){
  403. $imageDefault = array(
  404. 'left'=>0,
  405. 'top'=>0,
  406. 'right'=>0,
  407. 'bottom'=>0,
  408. 'width'=>100,
  409. 'height'=>100,
  410. 'opacity'=>100
  411. );
  412. $textDefault = array(
  413. 'text'=>'',
  414. 'left'=>0,
  415. 'top'=>0,
  416. 'fontSize'=>32, //字号
  417. 'fontColor'=>'255,255,255', //字体颜色
  418. 'angle'=>0,
  419. );
  420. $background = $config['background'];//海报最底层得背景
  421. $backgroundInfo = getimagesize($background);
  422. $background = imagecreatefromstring(file_get_contents($background));
  423. $backgroundWidth = $backgroundInfo[0]; //背景宽度
  424. $backgroundHeight = $backgroundInfo[1]; //背景高度
  425. $imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
  426. $color = imagecolorallocate($imageRes, 0, 0, 0);
  427. imagefill($imageRes, 0, 0, $color);
  428. imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));
  429. if(!empty($config['image'])){
  430. foreach ($config['image'] as $key => $val) {
  431. $val = array_merge($imageDefault,$val);
  432. $info = getimagesize($val['url']);
  433. $function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
  434. if($val['stream']){
  435. $info = getimagesizefromstring($val['url']);
  436. $function = 'imagecreatefromstring';
  437. }
  438. $res = $function($val['url']);
  439. $resWidth = $info[0];
  440. $resHeight = $info[1];
  441. $canvas=imagecreatetruecolor($val['width'], $val['height']);
  442. imagefill($canvas, 0, 0, $color);
  443. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight);
  444. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
  445. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];
  446. imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左,上,右,下,宽度,高度,透明度
  447. }
  448. }
  449. if(isset($config['text']) && !empty($config['text'])){
  450. foreach ($config['text'] as $key => $val) {
  451. $val = array_merge($textDefault,$val);
  452. list($R,$G,$B) = explode(',', $val['fontColor']);
  453. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  454. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
  455. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
  456. imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
  457. }
  458. }
  459. ob_start();
  460. imagejpeg ($imageRes);
  461. imagedestroy($imageRes);
  462. $res = ob_get_contents();
  463. ob_end_clean();
  464. $key = substr(md5(rand(0, 9999)) , 0, 5). date('YmdHis') . rand(0, 999999) . '.jpg';
  465. return UploadService::imageStream($key,$res,$path);
  466. }
  467. /*
  468. * 获取当前控制器模型方法组合成的字符串
  469. * @paran object $request Request 实例化后的对象
  470. * @retun string
  471. * */
  472. public static function getCurrentController()
  473. {
  474. return strtolower(request()->app().'/'.request()->controller().'/'.request()->action());
  475. }
  476. /**
  477. * TODO 获取小程序二维码是否生成
  478. * @param $url
  479. * @return array
  480. */
  481. public static function remoteImage($url)
  482. {
  483. $curl = curl_init();
  484. curl_setopt($curl, CURLOPT_URL, $url);
  485. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  486. $result = curl_exec($curl);
  487. $result = json_decode($result,true);
  488. if(is_array($result)) return ['status'=>false,'msg'=>$result['errcode'].'---'.$result['errmsg']];
  489. return ['status'=>true];
  490. }
  491. /**
  492. * TODO 修改 https 和 http
  493. * @param $url $url 域名
  494. * @param int $type 0 返回https 1 返回 http
  495. * @return string
  496. */
  497. public static function setHttpType($url, $type = 0)
  498. {
  499. $domainTop = substr($url,0,5);
  500. if($type){ if($domainTop == 'https') $url = 'http'.substr($url,5,strlen($url)); }
  501. else{ if($domainTop != 'https') $url = 'https:'.substr($url,5,strlen($url)); }
  502. return $url;
  503. }
  504. public static function setSiteUrl($image, $siteUrl = '')
  505. {
  506. if(!strlen(trim($siteUrl))) $siteUrl = SystemConfigService::get('site_url');
  507. $domainTop = substr($image,0,4);
  508. if($domainTop == 'http') return $image;
  509. $image = str_replace('\\', '/', $image);
  510. return $siteUrl.$image;
  511. }
  512. /*
  513. * CURL 检测远程文件是否在
  514. *
  515. * */
  516. public static function CurlFileExist($url)
  517. {
  518. $ch = curl_init();
  519. try{
  520. curl_setopt ($ch, CURLOPT_URL, $url);
  521. curl_setopt($ch, CURLOPT_HEADER, 1);
  522. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  523. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);
  524. $contents = curl_exec($ch);
  525. if (preg_match("/404/", $contents)) return false;
  526. if (preg_match("/403/", $contents)) return false;
  527. return true;
  528. }catch (\Exception $e){
  529. return false;
  530. }
  531. }
  532. /**
  533. * 获取二维码
  534. * @param $url
  535. * @param $name
  536. * @return array|bool|string
  537. */
  538. public static function getQRCodePath($url, $name)
  539. {
  540. if(!strlen(trim($url)) || !strlen(trim($name))) return false;
  541. $uploadType = SystemConfigService::get('upload_type');
  542. //TODO 没有选择默认使用本地上传
  543. if (!$uploadType) $uploadType = 1;
  544. $siteUrl = SystemConfigService::get('site_url') ?: '.';
  545. $info = [];
  546. $outfile = Config::get('qrcode.cache_dir');
  547. $code = new QRcode();
  548. $wapCodePath = $code->png($url, $outfile.'/'.$name)->getPath(); //获取二维码生成的地址
  549. $content = file_get_contents('.'.$wapCodePath);
  550. switch ($uploadType) {
  551. case 1 :
  552. $info["code"] = 200;
  553. $info["name"] = $name;
  554. $info["dir"] = $wapCodePath;
  555. $info["time"] = time();
  556. $headerArray = get_headers(UtilService::setHttpType($siteUrl, 1) . $info['dir'],true);
  557. $info['size'] = $headerArray['Content-Length'];
  558. $info['type'] = $headerArray['Content-Type'];
  559. $info["image_type"] = 1;
  560. $info['thumb_path'] = $wapCodePath;
  561. break;
  562. case 2 :
  563. $keys = Qiniu::uploadImageStream($name, $content);
  564. if (is_array($keys)) {
  565. foreach ($keys as $key => &$item) {
  566. if (is_array($item)) {
  567. $info = Qiniu::imageUrl($item['key']);
  568. $info['dir'] = UtilService::setHttpType($info['dir']);
  569. $headerArray = get_headers(UtilService::setHttpType(str_replace('\\', '/', $info['dir']), 1), true);
  570. $info['size'] = $headerArray['Content-Length'];
  571. $info['type'] = $headerArray['Content-Type'];
  572. $info['image_type'] = 2;
  573. }
  574. }
  575. if (!count($info)) return '七牛云文件上传失败';
  576. } else return $keys;
  577. break;
  578. case 3 :
  579. $content = COS::resourceStream($content);
  580. $serverImageInfo = OSS::uploadImageStream($name, $content);
  581. if (!is_array($serverImageInfo)) return $serverImageInfo;
  582. $info['code'] = 200;
  583. $info['name'] = substr(strrchr($serverImageInfo['info']['url'], '/'), 1);
  584. $serverImageInfo['info']['url'] = UtilService::setHttpType($serverImageInfo['info']['url']);
  585. $info['dir'] = $serverImageInfo['info']['url'];
  586. $info['thumb_path'] = $serverImageInfo['info']['url'];
  587. $headerArray = get_headers(UtilService::setHttpType(str_replace('\\', '/', $serverImageInfo['info']['url']), 1), true);
  588. $info['size'] = $headerArray['Content-Length'];
  589. $info['type'] = $headerArray['Content-Type'];
  590. $info['time'] = time();
  591. $info['image_type'] = 3;
  592. break;
  593. case 4 :
  594. list($imageUrl,$serverImageInfo) = COS::uploadImageStream($name, $content);
  595. if (!is_array($serverImageInfo) && !is_object($serverImageInfo)) return $serverImageInfo;
  596. if (is_object($serverImageInfo)) $serverImageInfo = $serverImageInfo->toArray();
  597. $serverImageInfo['ObjectURL'] = $imageUrl;
  598. $info['code'] = 200;
  599. $info['name'] = substr(strrchr($serverImageInfo['ObjectURL'], '/'), 1);
  600. $info['dir'] = $serverImageInfo['ObjectURL'];
  601. $info['thumb_path'] = $serverImageInfo['ObjectURL'];
  602. $headerArray = get_headers(UtilService::setHttpType(str_replace('\\', '/', $serverImageInfo['ObjectURL']), 1), true);
  603. $info['size'] = $headerArray['Content-Length'];
  604. $info['type'] = $headerArray['Content-Type'];
  605. $info['time'] = time();
  606. $info['image_type'] = 4;
  607. break;
  608. default:
  609. return '上传类型错误,请先选择文件上传类型';
  610. }
  611. return $info;
  612. }
  613. /**
  614. * 获取图片转为base64
  615. * @param string $avatar
  616. * @return bool|string
  617. */
  618. public static function setImageBase64($avatar = '',$timeout=15){
  619. try{
  620. $header = array(
  621. 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
  622. 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
  623. 'Accept-Encoding: gzip, deflate',);
  624. $curl = curl_init();
  625. curl_setopt($curl, CURLOPT_URL, $avatar);
  626. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  627. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  628. curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
  629. curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
  630. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  631. $data = curl_exec($curl);
  632. $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  633. curl_close($curl);
  634. if ($code == 200) return "data:image/jpeg;base64," . base64_encode($data);
  635. else return false;
  636. }catch (\Exception $e){
  637. return false;
  638. }
  639. }
  640. }