UtilService.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/02
  6. */
  7. namespace crmeb\services;
  8. use crmeb\exceptions\UtilException;
  9. use think\facade\Config;
  10. use dh2y\qrcode\QRcode;
  11. use crmeb\services\upload\Upload;
  12. class UtilService
  13. {
  14. public static function filtrate($string, $admin = false)
  15. {
  16. $ra = array(
  17. // '/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/',
  18. '/script/',
  19. '/javascript/',
  20. '/vbscript/',
  21. '/expression/',
  22. '/applet/',
  23. '/meta/',
  24. '/xml/',
  25. '/blink/',
  26. '/link/',
  27. '/style/',
  28. '/embed/',
  29. '/object/',
  30. '/frame/',
  31. '/layer/',
  32. '/title/',
  33. '/bgsound/',
  34. '/base/',
  35. '/onload/',
  36. '/onunload/',
  37. '/onchange/',
  38. '/onsubmit/',
  39. '/onreset/',
  40. '/onselect/',
  41. '/onblur/',
  42. '/onfocus/',
  43. '/onabort/',
  44. '/onkeydown/',
  45. '/onkeypress/',
  46. '/onkeyup/',
  47. '/onclick/',
  48. '/ondblclick/',
  49. '/onmousedown/',
  50. '/onmousemove/',
  51. '/onmouseout/',
  52. '/onmouseover/',
  53. '/onmouseup/',
  54. '/onunload/',
  55. "/<(\\/?)(script|i?frame|style|html|body|title|link|meta|object|\\?|\\%)([^>]*?)>/isU",
  56. "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU",
  57. );
  58. $str = preg_replace($ra, "", $string);
  59. if ($admin) {
  60. return $str;
  61. }
  62. return addslashes($str);
  63. }
  64. public static function sweep($array, $admin = false)
  65. {
  66. if (is_array($array)) {
  67. foreach ($array as $k => $v) {
  68. $array[$k] = self::sweep($v, $admin);
  69. }
  70. } else {
  71. $array = self::filtrate($array, $admin);
  72. }
  73. return $array;
  74. }
  75. /**
  76. * 获取POST请求的数据
  77. * @param $params
  78. * @param null $request
  79. * @param bool $suffix
  80. * @return array
  81. */
  82. public static function postMore($params, $request = null, $suffix = false, $admin = false)
  83. {
  84. if ($request === null) $request = app('request');
  85. $p = [];
  86. $i = 0;
  87. foreach ($params as $param) {
  88. if (!is_array($param)) {
  89. $p[$suffix == true ? $i++ : $param] = self::sweep($request->param($param), $admin);
  90. } else {
  91. if (!isset($param[1])) $param[1] = null;
  92. if (!isset($param[2])) $param[2] = '';
  93. if (!isset($param[3])) $param[3] = '';
  94. if (!isset($param[4])) $param[4] = '';
  95. if (!isset($param[5])) $param[5] = '';
  96. if (is_array($param[0])) {
  97. $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
  98. $keyName = $param[0][0];
  99. } else {
  100. $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
  101. $keyName = $param[0];
  102. }
  103. $p[$suffix == true ? $i++ : ($param[3] ? $param[3] : $keyName)] = self::sweep($request->param($name, $param[1], $param[2]), $admin);
  104. if (not_empty_check($param[4])) {
  105. if (!is_array($param[4])) {
  106. if (is_string($param[4]) && !function_exists($param[4])) {
  107. throw new UtilException('验证的方法' . $param[4] . '未定义');
  108. }
  109. if (!$param[4]($request->param($name, $param[1], $param[2]))) {
  110. throw new UtilException($param[5]);
  111. }
  112. } else {
  113. foreach ($param[4] as $k => $v) {
  114. if (is_string($v) && !function_exists($v)) {
  115. throw new UtilException('验证的方法' . $v . '未定义');
  116. }
  117. if (!$v($request->param($name, $param[1], $param[2]))) {
  118. throw new UtilException(is_array($param[5]) ? $param[5][$k] : $param[5]);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. return $p;
  126. }
  127. /**
  128. * 获取请求的数据
  129. * @param $params
  130. * @param null $request
  131. * @param bool $suffix
  132. * @return array
  133. */
  134. public static function getMore($params, $request = null, $suffix = false)
  135. {
  136. if ($request === null) $request = app('request');
  137. $p = [];
  138. $i = 0;
  139. foreach ($params as $param) {
  140. if (!is_array($param)) {
  141. $p[$suffix == true ? $i++ : $param] = self::sweep($request->param($param));
  142. } else {
  143. if (!isset($param[1])) $param[1] = null;
  144. if (!isset($param[2])) $param[2] = '';
  145. if (!isset($param[3])) $param[3] = '';
  146. if (!isset($param[4])) $param[4] = '';
  147. if (!isset($param[5])) $param[5] = '参数错误';
  148. if (is_array($param[0])) {
  149. $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
  150. $keyName = $param[0][0];
  151. } else {
  152. $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
  153. $keyName = $param[0];
  154. }
  155. $p[$suffix == true ? $i++ : ($param[3] ? $param[3] : $keyName)] = self::sweep($request->param($name, $param[1], $param[2]));
  156. if (not_empty_check($param[4])) {
  157. if (!is_array($param[4])) {
  158. if (is_string($param[4]) && !function_exists($param[4])) {
  159. throw new UtilException('验证的方法' . $param[4] . '未定义');
  160. }
  161. if (!$param[4]($request->param($name, $param[1], $param[2]))) {
  162. throw new UtilException($param[5]);
  163. }
  164. } else {
  165. foreach ($param[4] as $k => $v) {
  166. if (is_string($v) && !function_exists($v)) {
  167. throw new UtilException('验证的方法' . $v . '未定义');
  168. }
  169. if (!$v($request->param($name, $param[1], $param[2]))) {
  170. throw new UtilException(is_array($param[5]) ? $param[5][$k] : $param[5]);
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }
  177. return $p;
  178. }
  179. /**
  180. * TODO 砍价 拼团 分享海报生成
  181. * @param array $data
  182. * @param $path
  183. * @return array|bool|string
  184. * @throws \Exception
  185. */
  186. public static function setShareMarketingPoster($data = array(), $path)
  187. {
  188. $config = array(
  189. 'text' => array(
  190. array(
  191. 'text' => $data['price'],//TODO 价格
  192. 'left' => 116,
  193. 'top' => 200,
  194. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  195. 'fontSize' => 50, //字号
  196. 'fontColor' => '255,0,0', //字体颜色
  197. 'angle' => 0,
  198. ),
  199. array(
  200. 'text' => $data['label'],//TODO 标签
  201. 'left' => 450,
  202. 'top' => 188,
  203. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  204. 'fontSize' => 24, //字号
  205. 'fontColor' => '255,255,255', //字体颜色
  206. 'angle' => 0,
  207. ),
  208. array(
  209. 'text' => $data['msg'],//TODO 简述
  210. 'left' => 80,
  211. 'top' => 270,
  212. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  213. 'fontSize' => 22, //字号
  214. 'fontColor' => '40,40,40', //字体颜色
  215. 'angle' => 0,
  216. )
  217. ),
  218. 'image' => array(
  219. array(
  220. 'url' => $data['image'], //图片
  221. 'stream' => 0,
  222. 'left' => 120,
  223. 'top' => 340,
  224. 'right' => 0,
  225. 'bottom' => 0,
  226. 'width' => 450,
  227. 'height' => 450,
  228. 'opacity' => 100
  229. ),
  230. array(
  231. 'url' => $data['url'], //二维码资源
  232. 'stream' => 0,
  233. 'left' => 260,
  234. 'top' => 890,
  235. 'right' => 0,
  236. 'bottom' => 0,
  237. 'width' => 160,
  238. 'height' => 160,
  239. 'opacity' => 100
  240. )
  241. ),
  242. 'background' => 'static/poster/poster.jpg'
  243. );
  244. if (!file_exists($config['background'])) exception('缺少系统预设背景图片');
  245. if (strlen($data['title']) < 36) {
  246. $text = array(
  247. 'text' => $data['title'],//TODO 标题
  248. 'left' => 76,
  249. 'top' => 100,
  250. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  251. 'fontSize' => 32, //字号
  252. 'fontColor' => '0,0,0', //字体颜色
  253. 'angle' => 0,
  254. );
  255. array_push($config['text'], $text);
  256. } else {
  257. $titleOne = array(
  258. 'text' => mb_strimwidth($data['title'], 0, 24),//TODO 标题
  259. 'left' => 76,
  260. 'top' => 70,
  261. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  262. 'fontSize' => 32, //字号
  263. 'fontColor' => '0,0,0', //字体颜色
  264. 'angle' => 0,
  265. );
  266. $titleTwo = array(
  267. 'text' => mb_strimwidth($data['title'], 24, 24),//TODO 标题
  268. 'left' => 76,
  269. 'top' => 120,
  270. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  271. 'fontSize' => 32, //字号
  272. 'fontColor' => '0,0,0', //字体颜色
  273. 'angle' => 0,
  274. );
  275. array_push($config['text'], $titleOne);
  276. array_push($config['text'], $titleTwo);
  277. }
  278. return self::setSharePoster($config, $path);
  279. }
  280. /**
  281. * TODO 生成分享二维码图片
  282. * @param array $config
  283. * @param $path
  284. * @return array|bool|string
  285. * @throws \Exception
  286. */
  287. // public static function setSharePoster($config = array(), $path)
  288. // {
  289. // try {
  290. // $imageDefault = array(
  291. // 'left' => 0,
  292. // 'top' => 0,
  293. // 'right' => 0,
  294. // 'bottom' => 0,
  295. // 'width' => 100,
  296. // 'height' => 100,
  297. // 'opacity' => 100,
  298. // 'circle' => false // 新增:是否裁剪为圆形
  299. // );
  300. // $textDefault = array(
  301. // 'text' => '',
  302. // 'left' => 0,
  303. // 'top' => 0,
  304. // 'fontSize' => 32, //字号
  305. // 'fontColor' => '255,255,255', //字体颜色
  306. // 'angle' => 0,
  307. // );
  308. // $background = $config['background'];//海报最底层得背景
  309. // if (substr($background, 0, 1) === '/') {
  310. // $background = substr($background, 1);
  311. // }
  312. //
  313. // // 检查背景图片是否存在
  314. // if (!file_exists($background) && !filter_var($background, FILTER_VALIDATE_URL)) {
  315. // throw new \Exception('背景图片不存在: ' . $background);
  316. // }
  317. //
  318. // $backgroundInfo = getimagesize($background);
  319. // if ($backgroundInfo === false) {
  320. // throw new \Exception('无法获取背景图片信息');
  321. // }
  322. //
  323. // $backgroundContent = file_get_contents($background);
  324. // if ($backgroundContent === false) {
  325. // throw new \Exception('无法读取背景图片');
  326. // }
  327. //
  328. // $background = imagecreatefromstring($backgroundContent);
  329. // if ($background === false) {
  330. // throw new \Exception('无法创建背景图片资源');
  331. // }
  332. //
  333. // $backgroundWidth = $backgroundInfo[0]; //背景宽度
  334. // $backgroundHeight = $backgroundInfo[1]; //背景高度
  335. // $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
  336. //
  337. // // 创建黑色背景
  338. // $blackColor = imagecolorallocate($imageRes, 0, 0, 0);
  339. // imagefill($imageRes, 0, 0, $blackColor);
  340. // imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background), imagesx($background), imagesy($background));
  341. //
  342. // if (!empty($config['image'])) {
  343. // foreach ($config['image'] as $key => $val) {
  344. // $val = array_merge($imageDefault, $val);
  345. //
  346. // // 检查图片URL是否存在
  347. // if (empty($val['url'])) {
  348. // continue; // 跳过空的图片URL
  349. // }
  350. //
  351. // // 处理URL:如果是相对路径,转换为绝对路径
  352. // $imageUrl = $val['url'];
  353. // if (!filter_var($imageUrl, FILTER_VALIDATE_URL) && file_exists($imageUrl)) {
  354. // // 如果是本地文件路径,直接使用
  355. // $imageContent = file_get_contents($imageUrl);
  356. // } else {
  357. // // 尝试获取远程图片
  358. // $imageContent = @file_get_contents($imageUrl);
  359. // }
  360. //
  361. // if ($imageContent === false) {
  362. // // 如果无法获取图片,跳过
  363. // continue;
  364. // }
  365. //
  366. // $res = imagecreatefromstring($imageContent);
  367. // if ($res === false) {
  368. // // 如果无法创建图片资源,跳过
  369. // continue;
  370. // }
  371. //
  372. // $resWidth = imagesx($res);
  373. // $resHeight = imagesy($res);
  374. //
  375. // // 如果是圆形,先创建圆形图片
  376. // if ($val['circle']) {
  377. // // 创建一个正方形的画布
  378. // $canvas = imagecreatetruecolor($val['width'], $val['height']);
  379. // if ($canvas === false) {
  380. // imagedestroy($res);
  381. // continue;
  382. // }
  383. //
  384. // // 用白色填充背景
  385. // $whiteColor = imagecolorallocate($canvas, 255, 255, 255);
  386. // if ($whiteColor === false) {
  387. // imagedestroy($res);
  388. // imagedestroy($canvas);
  389. // continue;
  390. // }
  391. //
  392. // $fillResult = imagefill($canvas, 0, 0, $whiteColor);
  393. // if ($fillResult === false) {
  394. // imagedestroy($res);
  395. // imagedestroy($canvas);
  396. // continue;
  397. // }
  398. //
  399. // // 将原图缩放到指定大小
  400. // $sourceResized = imagecreatetruecolor($val['width'], $val['height']);
  401. // if ($sourceResized === false) {
  402. // imagedestroy($res);
  403. // imagedestroy($canvas);
  404. // continue;
  405. // }
  406. //
  407. // $copyResult = imagecopyresampled($sourceResized, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
  408. // if ($copyResult === false) {
  409. // imagedestroy($res);
  410. // imagedestroy($canvas);
  411. // imagedestroy($sourceResized);
  412. // continue;
  413. // }
  414. //
  415. // // 创建圆形:只复制圆形区域内的像素
  416. // $centerX = $val['width'] / 2;
  417. // $centerY = $val['height'] / 2;
  418. // $radius = min($val['width'], $val['height']) / 2;
  419. //
  420. // for ($x = 0; $x < $val['width']; $x++) {
  421. // for ($y = 0; $y < $val['height']; $y++) {
  422. // $distance = sqrt(pow($x - $centerX, 2) + pow($y - $centerY, 2));
  423. // if ($distance <= $radius) {
  424. // $pixelColor = imagecolorat($sourceResized, $x, $y);
  425. // imagesetpixel($canvas, $x, $y, $pixelColor);
  426. // }
  427. // }
  428. // }
  429. //
  430. // imagedestroy($sourceResized);
  431. // imagedestroy($res);
  432. // } else {
  433. // // 非圆形,按原逻辑处理
  434. // $canvas = imagecreatetruecolor($val['width'], $val['height']);
  435. // if ($canvas === false) {
  436. // imagedestroy($res);
  437. // continue;
  438. // }
  439. //
  440. // $bgColor = imagecolorallocate($canvas, 255, 255, 255);
  441. // if ($bgColor === false) {
  442. // imagedestroy($res);
  443. // imagedestroy($canvas);
  444. // continue;
  445. // }
  446. //
  447. // $fillResult = imagefill($canvas, 0, 0, $bgColor);
  448. // if ($fillResult === false) {
  449. // imagedestroy($res);
  450. // imagedestroy($canvas);
  451. // continue;
  452. // }
  453. //
  454. // $copyResult = imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
  455. // if ($copyResult === false) {
  456. // imagedestroy($res);
  457. // imagedestroy($canvas);
  458. // continue;
  459. // }
  460. //
  461. // imagedestroy($res);
  462. // }
  463. //
  464. // $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) - $val['width'] : $val['left'];
  465. // $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) - $val['height'] : $val['top'];
  466. //
  467. // // 合并到主图像
  468. // $mergeResult = imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], 0, 0, $val['width'], $val['height'], $val['opacity']);
  469. // if ($mergeResult === false) {
  470. // // 合并失败,但继续处理其他图片
  471. // }
  472. //
  473. // imagedestroy($canvas);
  474. // }
  475. // }
  476. //
  477. // // ... 后面的代码保持不变
  478. // if (isset($config['text']) && !empty($config['text'])) {
  479. // foreach ($config['text'] as $key => $val) {
  480. // $val = array_merge($textDefault, $val);
  481. // list($R, $G, $B) = explode(',', $val['fontColor']);
  482. // $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  483. // $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
  484. // $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
  485. // imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
  486. // }
  487. // }
  488. //
  489. // ob_start();
  490. // imagejpeg($imageRes);
  491. // imagedestroy($imageRes);
  492. // $res = ob_get_contents();
  493. // ob_end_clean();
  494. //
  495. // $key = substr(md5(rand(0, 9999)), 0, 5) . date('YmdHis') . rand(0, 999999) . '.jpg';
  496. // $uploadType = (int)sys_config('upload_type', 1);
  497. // $upload = new Upload($uploadType, [
  498. // 'accessKey' => sys_config('accessKey'),
  499. // 'secretKey' => sys_config('secretKey'),
  500. // 'uploadUrl' => sys_config('uploadUrl'),
  501. // 'storageName' => sys_config('storage_name'),
  502. // 'storageRegion' => sys_config('storage_region'),
  503. // ]);
  504. // $res = $upload->to($path)->validate()->stream($res, $key);
  505. // if ($res === false) {
  506. // return $upload->getError();
  507. // } else {
  508. // $info = $upload->getUploadInfo();
  509. // $info['image_type'] = $uploadType;
  510. // return $info;
  511. // }
  512. // } catch (\Exception $e) {
  513. // @file_put_contents('quanju.txt', json_encode(['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]) . "-报错内容\r\n", 8);
  514. // return '生成图片时,系统错误: ' . $e->getMessage();
  515. // }
  516. // }
  517. public static function setSharePoster($config = array(), $path)
  518. {
  519. try {
  520. $imageDefault = array(
  521. 'left' => 0,
  522. 'top' => 0,
  523. 'right' => 0,
  524. 'bottom' => 0,
  525. 'width' => 100,
  526. 'height' => 100,
  527. 'opacity' => 100,
  528. 'circle' => false // 是否裁剪为圆形
  529. );
  530. $textDefault = array(
  531. 'text' => '',
  532. 'left' => 0,
  533. 'top' => 0,
  534. 'fontSize' => 32, //字号
  535. 'fontColor' => '255,255,255', //字体颜色
  536. 'angle' => 0,
  537. );
  538. $background = $config['background'];//海报最底层得背景
  539. if (substr($background, 0, 1) === '/') {
  540. $background = substr($background, 1);
  541. }
  542. // 检查背景图片是否存在
  543. if (!file_exists($background) && !filter_var($background, FILTER_VALIDATE_URL)) {
  544. throw new \Exception('背景图片不存在: ' . $background);
  545. }
  546. $backgroundInfo = getimagesize($background);
  547. if ($backgroundInfo === false) {
  548. throw new \Exception('无法获取背景图片信息');
  549. }
  550. $backgroundContent = file_get_contents($background);
  551. if ($backgroundContent === false) {
  552. throw new \Exception('无法读取背景图片');
  553. }
  554. $background = imagecreatefromstring($backgroundContent);
  555. if ($background === false) {
  556. throw new \Exception('无法创建背景图片资源');
  557. }
  558. $backgroundWidth = $backgroundInfo[0]; //背景宽度
  559. $backgroundHeight = $backgroundInfo[1]; //背景高度
  560. // 创建主图像,支持透明度
  561. $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
  562. // 设置透明背景
  563. imagealphablending($imageRes, false);
  564. imagesavealpha($imageRes, true);
  565. $transparent = imagecolorallocatealpha($imageRes, 255, 255, 255, 127);
  566. imagefilledrectangle($imageRes, 0, 0, $backgroundWidth, $backgroundHeight, $transparent);
  567. // 复制背景图片
  568. imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background), imagesx($background), imagesy($background));
  569. // 恢复混合模式
  570. imagealphablending($imageRes, true);
  571. if (!empty($config['image'])) {
  572. foreach ($config['image'] as $key => $val) {
  573. $val = array_merge($imageDefault, $val);
  574. // 检查图片URL是否存在
  575. if (empty($val['url'])) {
  576. continue; // 跳过空的图片URL
  577. }
  578. // 处理URL:如果是相对路径,转换为绝对路径
  579. $imageUrl = $val['url'];
  580. if (!filter_var($imageUrl, FILTER_VALIDATE_URL) && file_exists($imageUrl)) {
  581. // 如果是本地文件路径,直接使用
  582. $imageContent = file_get_contents($imageUrl);
  583. } else {
  584. // 尝试获取远程图片
  585. $imageContent = @file_get_contents($imageUrl);
  586. }
  587. if ($imageContent === false) {
  588. // 如果无法获取图片,跳过
  589. continue;
  590. }
  591. $res = imagecreatefromstring($imageContent);
  592. if ($res === false) {
  593. // 如果无法创建图片资源,跳过
  594. continue;
  595. }
  596. $resWidth = imagesx($res);
  597. $resHeight = imagesy($res);
  598. // 如果是圆形,创建圆形图片
  599. if ($val['circle']) {
  600. // 创建一个支持透明的画布
  601. $canvas = imagecreatetruecolor($val['width'], $val['height']);
  602. // 设置透明背景
  603. imagealphablending($canvas, false);
  604. imagesavealpha($canvas, true);
  605. $transparent = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
  606. imagefilledrectangle($canvas, 0, 0, $val['width'], $val['height'], $transparent);
  607. // 将原图缩放到指定大小
  608. $sourceResized = imagecreatetruecolor($val['width'], $val['height']);
  609. imagecopyresampled($sourceResized, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
  610. // 创建圆形:只复制圆形区域内的像素
  611. $centerX = $val['width'] / 2;
  612. $centerY = $val['height'] / 2;
  613. $radius = min($val['width'], $val['height']) / 2;
  614. // 恢复混合模式
  615. imagealphablending($canvas, true);
  616. for ($x = 0; $x < $val['width']; $x++) {
  617. for ($y = 0; $y < $val['height']; $y++) {
  618. $distance = sqrt(pow($x - $centerX, 2) + pow($y - $centerY, 2));
  619. if ($distance <= $radius) {
  620. $pixelColor = imagecolorat($sourceResized, $x, $y);
  621. imagesetpixel($canvas, $x, $y, $pixelColor);
  622. }
  623. }
  624. }
  625. imagedestroy($sourceResized);
  626. imagedestroy($res);
  627. } else {
  628. // 非圆形,按原逻辑处理
  629. $canvas = imagecreatetruecolor($val['width'], $val['height']);
  630. $bgColor = imagecolorallocate($canvas, 255, 255, 255);
  631. imagefill($canvas, 0, 0, $bgColor);
  632. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
  633. imagedestroy($res);
  634. }
  635. $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) - $val['width'] : $val['left'];
  636. $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) - $val['height'] : $val['top'];
  637. // 合并到主图像
  638. if ($val['circle']) {
  639. // 对于圆形图片,使用imagecopy以支持透明度
  640. imagecopy($imageRes, $canvas, $val['left'], $val['top'], 0, 0, $val['width'], $val['height']);
  641. } else {
  642. // 对于普通图片,使用imagecopymerge
  643. imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], 0, 0, $val['width'], $val['height'], $val['opacity']);
  644. }
  645. imagedestroy($canvas);
  646. }
  647. }
  648. if (isset($config['text']) && !empty($config['text'])) {
  649. foreach ($config['text'] as $key => $val) {
  650. $val = array_merge($textDefault, $val);
  651. list($R, $G, $B) = explode(',', $val['fontColor']);
  652. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  653. $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
  654. $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
  655. imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
  656. }
  657. }
  658. // 输出图片前再次确保保存alpha通道
  659. imagesavealpha($imageRes, true);
  660. ob_start();
  661. imagejpeg($imageRes, null, 90); // 使用较高质量
  662. imagedestroy($imageRes);
  663. $res = ob_get_contents();
  664. ob_end_clean();
  665. $key = substr(md5(rand(0, 9999)), 0, 5) . date('YmdHis') . rand(0, 999999) . '.jpg';
  666. $uploadType = (int)sys_config('upload_type', 1);
  667. $upload = new Upload($uploadType, [
  668. 'accessKey' => sys_config('accessKey'),
  669. 'secretKey' => sys_config('secretKey'),
  670. 'uploadUrl' => sys_config('uploadUrl'),
  671. 'storageName' => sys_config('storage_name'),
  672. 'storageRegion' => sys_config('storage_region'),
  673. ]);
  674. $res = $upload->to($path)->validate()->stream($res, $key);
  675. if ($res === false) {
  676. return $upload->getError();
  677. } else {
  678. $info = $upload->getUploadInfo();
  679. $info['image_type'] = $uploadType;
  680. return $info;
  681. }
  682. } catch (\Exception $e) {
  683. @file_put_contents('quanju.txt', json_encode(['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]) . "-报错内容\r\n", 8);
  684. return '生成图片时,系统错误: ' . $e->getMessage();
  685. }
  686. }
  687. /**
  688. * 辅助函数:带透明度的图像复制
  689. * 注意:这个函数在PHP 5.5+中可能不需要,因为imagecopy已经支持透明度
  690. */
  691. function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct) {
  692. // 创建一个临时图像
  693. $tmp = imagecreatetruecolor($src_w, $src_h);
  694. // 复制源图像到临时图像
  695. imagecopy($tmp, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
  696. // 合并源图像到临时图像
  697. imagecopy($tmp, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
  698. // 合并临时图像到目标图像
  699. imagecopymerge($dst_im, $tmp, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
  700. // 销毁临时图像
  701. imagedestroy($tmp);
  702. }
  703. /**
  704. * TODO 获取小程序二维码是否生成
  705. * @param $url
  706. * @return array
  707. */
  708. public static function remoteImage($url)
  709. {
  710. $curl = curl_init();
  711. curl_setopt($curl, CURLOPT_URL, $url);
  712. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  713. $result = curl_exec($curl);
  714. $result = json_decode($result, true);
  715. if (is_array($result)) return ['status' => false, 'msg' => $result['errcode'] . '---' . $result['errmsg']];
  716. return ['status' => true];
  717. }
  718. /**
  719. * TODO 修改 https 和 http 移动到common
  720. * @param $url $url 域名
  721. * @param int $type 0 返回https 1 返回 http
  722. * @return string
  723. */
  724. public static function setHttpType($url, $type = 0)
  725. {
  726. $domainTop = substr($url, 0, 5);
  727. if ($type) {
  728. if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url));
  729. } else {
  730. if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url));
  731. }
  732. return $url;
  733. }
  734. /**
  735. * 获取二维码
  736. * @param $url
  737. * @param $name
  738. * @return array|bool|string
  739. */
  740. public static function getQRCodePath($url, $name)
  741. {
  742. if (!strlen(trim($url)) || !strlen(trim($name))) return false;
  743. try {
  744. $uploadType = sys_config('upload_type');
  745. //TODO 没有选择默认使用本地上传
  746. if (!$uploadType) $uploadType = 1;
  747. $uploadType = (int)$uploadType;
  748. $siteUrl = sys_config('site_url');
  749. if (!$siteUrl) return '请前往后台设置->系统设置->网站域名 填写您的域名格式为:http://域名';
  750. $info = [];
  751. $outfile = Config::get('qrcode.cache_dir');
  752. $code = new QRcode();
  753. $wapCodePath = $code->png($url, $outfile . '/' . $name)->getPath(); //获取二维码生成的地址
  754. $content = file_get_contents('.' . $wapCodePath);
  755. if ($uploadType === 1) {
  756. $info["code"] = 200;
  757. $info["name"] = $name;
  758. $info["dir"] = $wapCodePath;
  759. $info["time"] = time();
  760. $info['size'] = 0;
  761. $info['type'] = 'image/png';
  762. $info["image_type"] = 1;
  763. $info['thumb_path'] = $wapCodePath;
  764. return $info;
  765. } else {
  766. $upload = new Upload($uploadType, [
  767. 'accessKey' => sys_config('accessKey'),
  768. 'secretKey' => sys_config('secretKey'),
  769. 'uploadUrl' => sys_config('uploadUrl'),
  770. 'storageName' => sys_config('storage_name'),
  771. 'storageRegion' => sys_config('storage_region'),
  772. ]);
  773. $res = $upload->to($outfile)->validate()->stream($content, $name);
  774. if ($res === false) {
  775. return $upload->getError();
  776. }
  777. $info = $upload->getUploadInfo();
  778. $info['image_type'] = $uploadType;
  779. return $info;
  780. }
  781. } catch (\Exception $e) {
  782. return $e->getMessage();
  783. }
  784. }
  785. /**
  786. * @param array $data
  787. * @param string $path
  788. * @return array|bool|string
  789. * @throws \Exception
  790. */
  791. public static function setShareProductPoster($data = array(), $path = '')
  792. {
  793. $config = array(
  794. 'text' => array(
  795. array(
  796. 'text' => $data['price'],//TODO 价格
  797. 'left' => 219,
  798. 'top' => 1055,
  799. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  800. 'fontSize' => 50, //字号
  801. 'fontColor' => '131,28,32', //字体颜色
  802. 'angle' => 0,
  803. ),
  804. array(
  805. 'text' => '原价:¥' . $data['ot_price'],//TODO 原价
  806. 'left' => 94,
  807. 'top' => 1105,
  808. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Light-My.otf', //字体文件
  809. 'fontSize' => 18, //字号
  810. 'fontColor' => '0,0,0', //字体颜色
  811. 'angle' => 0,
  812. ),
  813. array(
  814. 'text' => $data['user_nickname'],//TODO 用户名
  815. 'left' => 220,
  816. 'top' => 240,
  817. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Light.ttf', //字体文件
  818. 'fontSize' => 20, //字号
  819. 'fontColor' => '0,0,0', //字体颜色
  820. 'angle' => 0,
  821. ),
  822. array(
  823. 'text' => implode($data['user_nickname_pinyin'], ' '),//TODO 用户名拼音
  824. 'left' => 220,
  825. 'top' => 260,
  826. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Light.ttf', //字体文件
  827. 'fontSize' => 10, //字号
  828. 'fontColor' => '0,0,0', //字体颜色
  829. 'angle' => 0,
  830. ),
  831. ),
  832. 'image' => array(
  833. array(
  834. 'url' => $data['image'], //图片
  835. 'stream' => 0,
  836. 'left' => 94,
  837. 'top' => 359,
  838. 'right' => 0,
  839. 'bottom' => 0,
  840. 'width' => 561,
  841. 'height' => 613,
  842. 'opacity' => 100
  843. ),
  844. array(
  845. 'url' => $data['url'], //二维码资源
  846. 'stream' => 0,
  847. 'left' => 557,
  848. 'top' => 1013,
  849. 'right' => 0,
  850. 'bottom' => 0,
  851. 'width' => 99,
  852. 'height' => 99,
  853. 'opacity' => 100
  854. ),
  855. array(
  856. 'url' => $data['user_avatar'], //头像资源
  857. 'stream' => 0,
  858. 'left' => 98,
  859. 'top' => 211,
  860. 'right' => 0,
  861. 'bottom' => 0,
  862. 'width' => 92,
  863. 'height' => 92,
  864. 'opacity' => 100
  865. )
  866. ),
  867. 'background' => 'static/poster/product.jpg'
  868. );
  869. if (!file_exists($config['background'])) exception('缺少系统预设背景图片');
  870. if (strlen($data['title']) < 27) {
  871. $text = array(
  872. 'text' => $data['title'],//TODO 标题
  873. 'left' => 130,
  874. 'top' => 1172,
  875. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  876. 'fontSize' => 22, //字号
  877. 'fontColor' => '255,255,255', //字体颜色
  878. 'angle' => 0,
  879. );
  880. array_push($config['text'], $text);
  881. } else {
  882. $titleOne = array(
  883. 'text' => mb_substr($data['title'], 0, 8) . '…',//TODO 标题
  884. 'left' => 130,
  885. 'top' => 1172,
  886. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  887. 'fontSize' => 22, //字号
  888. 'fontColor' => '255,255,255', //字体颜色
  889. 'angle' => 0,
  890. );
  891. /*$titleTwo = array(
  892. 'text' => mb_substr($data['title'], 12, 12),//TODO 标题
  893. 'left' => 60,
  894. 'top' => 1310,
  895. 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  896. 'fontSize' => 40, //字号
  897. 'fontColor' => '255,255,255', //字体颜色
  898. 'angle' => 0,
  899. );*/
  900. array_push($config['text'], $titleOne);
  901. // array_push($config['text'], $titleTwo);
  902. }
  903. return self::setSharePoster($config, $path);
  904. }
  905. }