UtilService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/02
  6. */
  7. namespace crmeb\services;
  8. use think\facade\Config;
  9. use dh2y\qrcode\QRcode;
  10. use crmeb\services\upload\Upload;
  11. class UtilService
  12. {
  13. /**
  14. * 获取POST请求的数据
  15. * @param $params
  16. * @param null $request
  17. * @param bool $suffix
  18. * @return array
  19. */
  20. public static function postMore($params, $request = null, $suffix = false)
  21. {
  22. if ($request === null) $request = app('request');
  23. $p = [];
  24. $i = 0;
  25. foreach ($params as $param) {
  26. if (!is_array($param)) {
  27. $p[$suffix == true ? $i++ : $param] = $request->param($param);
  28. } else {
  29. if (!isset($param[1])) $param[1] = null;
  30. if (!isset($param[2])) $param[2] = '';
  31. if (is_array($param[0])) {
  32. $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
  33. $keyName = $param[0][0];
  34. } else {
  35. $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
  36. $keyName = $param[0];
  37. }
  38. $p[$suffix == true ? $i++ : (isset($param[3]) ? $param[3] : $keyName)] = $request->param($name, $param[1], $param[2]);
  39. }
  40. }
  41. return $p;
  42. }
  43. /**
  44. * 获取请求的数据
  45. * @param $params
  46. * @param null $request
  47. * @param bool $suffix
  48. * @return array
  49. */
  50. public static function getMore($params, $request = null, $suffix = false)
  51. {
  52. if ($request === null) $request = app('request');
  53. $p = [];
  54. $i = 0;
  55. foreach ($params as $param) {
  56. if (!is_array($param)) {
  57. $p[$suffix == true ? $i++ : $param] = $request->param($param);
  58. } else {
  59. if (!isset($param[1])) $param[1] = null;
  60. if (!isset($param[2])) $param[2] = '';
  61. if (is_array($param[0])) {
  62. $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
  63. $keyName = $param[0][0];
  64. } else {
  65. $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
  66. $keyName = $param[0];
  67. }
  68. $p[$suffix == true ? $i++ : (isset($param[3]) ? $param[3] : $keyName)] = $request->param($name, $param[1], $param[2]);
  69. }
  70. }
  71. return $p;
  72. }
  73. /**
  74. * TODO 砍价 拼团 分享海报生成
  75. * @param array $data
  76. * @param $path
  77. * @return array|bool|string
  78. * @throws \Exception
  79. */
  80. public static function setShareMarketingPoster($data = array(), $path)
  81. {
  82. $config = array(
  83. 'text' => array(
  84. array(
  85. 'text' => $data['price'],//TODO 价格
  86. 'left' => 116,
  87. 'top' => 200,
  88. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  89. 'fontSize' => 50, //字号
  90. 'fontColor' => '255,0,0', //字体颜色
  91. 'angle' => 0,
  92. ),
  93. array(
  94. 'text' => $data['label'],//TODO 标签
  95. 'left' => 450,
  96. 'top' => 188,
  97. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  98. 'fontSize' => 24, //字号
  99. 'fontColor' => '255,255,255', //字体颜色
  100. 'angle' => 0,
  101. ),
  102. array(
  103. 'text' => $data['msg'],//TODO 简述
  104. 'left' => 80,
  105. 'top' => 270,
  106. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  107. 'fontSize' => 22, //字号
  108. 'fontColor' => '40,40,40', //字体颜色
  109. 'angle' => 0,
  110. )
  111. ),
  112. 'image' => array(
  113. array(
  114. 'url' => $data['image'], //图片
  115. 'stream' => 0,
  116. 'left' => 120,
  117. 'top' => 340,
  118. 'right' => 0,
  119. 'bottom' => 0,
  120. 'width' => 450,
  121. 'height' => 450,
  122. 'opacity' => 100
  123. ),
  124. array(
  125. 'url' => $data['url'], //二维码资源
  126. 'stream' => 0,
  127. 'left' => 260,
  128. 'top' => 890,
  129. 'right' => 0,
  130. 'bottom' => 0,
  131. 'width' => 160,
  132. 'height' => 160,
  133. 'opacity' => 100
  134. )
  135. ),
  136. 'background' => 'static/poster/poster.jpg'
  137. );
  138. if (!file_exists($config['background'])) exception('缺少系统预设背景图片');
  139. if (strlen($data['title']) < 36) {
  140. $text = array(
  141. 'text' => $data['title'],//TODO 标题
  142. 'left' => 76,
  143. 'top' => 100,
  144. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  145. 'fontSize' => 32, //字号
  146. 'fontColor' => '0,0,0', //字体颜色
  147. 'angle' => 0,
  148. );
  149. array_push($config['text'], $text);
  150. } else {
  151. $titleOne = array(
  152. 'text' => mb_strimwidth($data['title'], 0, 24),//TODO 标题
  153. 'left' => 76,
  154. 'top' => 70,
  155. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  156. 'fontSize' => 32, //字号
  157. 'fontColor' => '0,0,0', //字体颜色
  158. 'angle' => 0,
  159. );
  160. $titleTwo = array(
  161. 'text' => mb_strimwidth($data['title'], mb_strlen(mb_strimwidth($data['title'], 0, 24)), 24),//TODO 标题
  162. 'left' => 76,
  163. 'top' => 120,
  164. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  165. 'fontSize' => 32, //字号
  166. 'fontColor' => '0,0,0', //字体颜色
  167. 'angle' => 0,
  168. );
  169. array_push($config['text'], $titleOne);
  170. array_push($config['text'], $titleTwo);
  171. }
  172. return self::setSharePoster($config, $path);
  173. }
  174. /**
  175. * TODO 生成分享二维码图片
  176. * @param array $config
  177. * @param $path
  178. * @return array|bool|string
  179. * @throws \Exception
  180. */
  181. public static function setSharePoster($config = array(), $path)
  182. {
  183. $imageDefault = array(
  184. 'left' => 0,
  185. 'top' => 0,
  186. 'right' => 0,
  187. 'bottom' => 0,
  188. 'width' => 100,
  189. 'height' => 100,
  190. 'opacity' => 100
  191. );
  192. $textDefault = array(
  193. 'text' => '',
  194. 'left' => 0,
  195. 'top' => 0,
  196. 'fontSize' => 32, //字号
  197. 'fontColor' => '255,255,255', //字体颜色
  198. 'angle' => 0,
  199. );
  200. $background = $config['background'];//海报最底层得背景
  201. if (substr($background, 0, 1) === '/') {
  202. $background = substr($background, 1);
  203. }
  204. $backgroundInfo = getimagesize($background);
  205. $background = imagecreatefromstring(file_get_contents($background));
  206. $backgroundWidth = $backgroundInfo[0]; //背景宽度
  207. $backgroundHeight = $backgroundInfo[1]; //背景高度
  208. $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
  209. $color = imagecolorallocate($imageRes, 0, 0, 0);
  210. imagefill($imageRes, 0, 0, $color);
  211. imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background), imagesx($background), imagesy($background));
  212. if (!empty($config['image'])) {
  213. foreach ($config['image'] as $key => $val) {
  214. $val = array_merge($imageDefault, $val);
  215. $info = getimagesize($val['url']);
  216. $function = 'imagecreatefrom' . image_type_to_extension($info[2], false);
  217. if ($val['stream']) {
  218. $info = getimagesizefromstring($val['url']);
  219. $function = 'imagecreatefromstring';
  220. }
  221. $res = $function($val['url']);
  222. $resWidth = $info[0];
  223. $resHeight = $info[1];
  224. $canvas = imagecreatetruecolor($val['width'], $val['height']);
  225. imagefill($canvas, 0, 0, $color);
  226. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
  227. $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) - $val['width'] : $val['left'];
  228. $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) - $val['height'] : $val['top'];
  229. imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], $val['right'], $val['bottom'], $val['width'], $val['height'], $val['opacity']);//左,上,右,下,宽度,高度,透明度
  230. }
  231. }
  232. if (isset($config['text']) && !empty($config['text'])) {
  233. foreach ($config['text'] as $key => $val) {
  234. $val = array_merge($textDefault, $val);
  235. list($R, $G, $B) = explode(',', $val['fontColor']);
  236. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  237. $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
  238. $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
  239. imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
  240. }
  241. }
  242. ob_start();
  243. imagejpeg($imageRes);
  244. imagedestroy($imageRes);
  245. $res = ob_get_contents();
  246. ob_end_clean();
  247. $key = substr(md5(rand(0, 9999)), 0, 5) . date('YmdHis') . rand(0, 999999) . '.jpg';
  248. $uploadType = (int)sys_config('upload_type', 1);
  249. $upload = new Upload($uploadType, [
  250. 'accessKey' => sys_config('accessKey'),
  251. 'secretKey' => sys_config('secretKey'),
  252. 'uploadUrl' => sys_config('uploadUrl'),
  253. 'storageName' => sys_config('storage_name'),
  254. 'storageRegion' => sys_config('storage_region'),
  255. ]);
  256. $res = $upload->to($path)->validate()->stream($res, $key);
  257. if ($res === false) {
  258. return $upload->getError();
  259. } else {
  260. $info = $upload->getUploadInfo();
  261. $info['image_type'] = $uploadType;
  262. return $info;
  263. }
  264. }
  265. /**
  266. * TODO 获取小程序二维码是否生成
  267. * @param $url
  268. * @return array
  269. */
  270. public static function remoteImage($url)
  271. {
  272. $curl = curl_init();
  273. curl_setopt($curl, CURLOPT_URL, $url);
  274. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  275. $result = curl_exec($curl);
  276. $result = json_decode($result, true);
  277. if (is_array($result)) return ['status' => false, 'msg' => $result['errcode'] . '---' . $result['errmsg']];
  278. return ['status' => true];
  279. }
  280. /**
  281. * TODO 修改 https 和 http 移动到common
  282. * @param $url $url 域名
  283. * @param int $type 0 返回https 1 返回 http
  284. * @return string
  285. */
  286. public static function setHttpType($url, $type = 0)
  287. {
  288. $domainTop = substr($url, 0, 5);
  289. if ($type) {
  290. if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url));
  291. } else {
  292. if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url));
  293. }
  294. return $url;
  295. }
  296. /**
  297. * 获取二维码
  298. * @param $url
  299. * @param $name
  300. * @return array|bool|string
  301. */
  302. public static function getQRCodePath($url, $name)
  303. {
  304. if (!strlen(trim($url)) || !strlen(trim($name))) return false;
  305. try {
  306. $uploadType = sys_config('upload_type');
  307. //TODO 没有选择默认使用本地上传
  308. if (!$uploadType) $uploadType = 1;
  309. $uploadType = (int)$uploadType;
  310. $siteUrl = sys_config('site_url');
  311. if (!$siteUrl) return '请前往后台设置->系统设置->网站域名 填写您的域名格式为:http://域名';
  312. $info = [];
  313. $outfile = Config::get('qrcode.cache_dir');
  314. $code = new QRcode();
  315. $wapCodePath = $code->png($url, $outfile . '/' . $name)->getPath(); //获取二维码生成的地址
  316. $content = file_get_contents('.' . $wapCodePath);
  317. if ($uploadType === 1) {
  318. $info["code"] = 200;
  319. $info["name"] = $name;
  320. $info["dir"] = $wapCodePath;
  321. $info["time"] = time();
  322. $info['size'] = 0;
  323. $info['type'] = 'image/png';
  324. $info["image_type"] = 1;
  325. $info['thumb_path'] = $wapCodePath;
  326. return $info;
  327. } else {
  328. $upload = new Upload($uploadType, [
  329. 'accessKey' => sys_config('accessKey'),
  330. 'secretKey' => sys_config('secretKey'),
  331. 'uploadUrl' => sys_config('uploadUrl'),
  332. 'storageName' => sys_config('storage_name'),
  333. 'storageRegion' => sys_config('storage_region'),
  334. ]);
  335. $res = $upload->to($outfile)->validate()->stream($content, $name);
  336. if ($res === false) {
  337. return $upload->getError();
  338. }
  339. $info = $upload->getUploadInfo();
  340. $info['image_type'] = $uploadType;
  341. return $info;
  342. }
  343. } catch (\Exception $e) {
  344. return $e->getMessage();
  345. }
  346. }
  347. }