UtilService.php 14 KB

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