UtilService.php 14 KB

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