Poster.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\giftcard\model\membercard;
  11. use addon\giftcard\model\transfer\Blessing;
  12. use app\model\BaseModel;
  13. use app\model\system\Site;
  14. use app\model\upload\Upload;
  15. use extend\Poster as PosterExtend;
  16. /**
  17. * 海报生成类
  18. */
  19. class Poster extends BaseModel
  20. {
  21. /**
  22. * 海报
  23. */
  24. public function poster($app_type, $page, $qrcode_param, $promotion_type, $site_id)
  25. {
  26. try {
  27. $blessing_model = new Blessing();
  28. $params = array (
  29. 'no' => $qrcode_param[ 'no' ],
  30. 'member_id' => $qrcode_param[ 'source_member' ]
  31. );
  32. $card_info = $blessing_model->getBlessingDetail($params)[ 'data' ] ?? [];
  33. if (empty($card_info)) return $this->error('未获取到信息');
  34. $qrcode_info = $this->getQrcode($app_type, $page, $qrcode_param, $promotion_type, $site_id);
  35. if ($qrcode_info[ 'code' ] < 0) return $qrcode_info;
  36. //判断海报是否存在或停用
  37. $site_model = new Site();
  38. $condition = array (
  39. [ "site_id", "=", $site_id ]
  40. );
  41. $site_info = $site_model->getSiteInfo($condition);
  42. if (!empty($qrcode_param[ 'source_member' ])) {
  43. $member_info = $this->getMemberInfo($qrcode_param[ 'source_member' ]);
  44. }
  45. $poster_width = 720;
  46. $poster_height = 1280;
  47. $poster = new PosterExtend($poster_width, $poster_height);
  48. $option = [
  49. [
  50. 'action' => 'imageCopy', // 背景图
  51. 'data' => [
  52. img('upload/poster/bg/promotion_4.jpg'),
  53. 0,
  54. 0,
  55. 720,
  56. 1280,
  57. 'square',
  58. 0,
  59. 1
  60. ]
  61. ],
  62. [
  63. 'action' => 'imageCopy', // 商品图
  64. 'data' => [
  65. $card_info[ 'card_cover' ],
  66. 100,
  67. 300,
  68. 520,
  69. 310,
  70. 'square',
  71. 5,
  72. 1
  73. ]
  74. ],
  75. [
  76. 'action' => 'imageCopy', // 二维码
  77. 'data' => [
  78. $qrcode_info[ 'data' ][ 'path' ],
  79. 100,
  80. 930,
  81. 150,
  82. 150,
  83. 'square',
  84. 0,
  85. 1
  86. ]
  87. ],
  88. [
  89. 'action' => 'imageText', // 写入礼品卡名称
  90. 'data' => [
  91. $card_info[ 'card_name' ],
  92. 25,
  93. [ 34, 34, 34 ],
  94. 100,
  95. 660,
  96. 540,
  97. 2,
  98. true,
  99. 1
  100. ]
  101. ],
  102. [
  103. 'action' => 'imageText', // 写入祝福语
  104. 'data' => [
  105. $card_info[ 'blessing' ],
  106. 22,
  107. [ 255, 0, 0 ],
  108. 150,
  109. 800,
  110. 480,
  111. 1,
  112. false,
  113. 1
  114. ]
  115. ],
  116. [
  117. 'action' => 'imageText', // 写入固定文字
  118. 'data' => [
  119. '扫码查看礼品详情',
  120. 22,
  121. [ 51, 51, 51 ],
  122. 290,
  123. 1000,
  124. 548,
  125. 1,
  126. true,
  127. 1
  128. ]
  129. ],
  130. [
  131. 'action' => 'imageText', // 写入有效期
  132. 'data' => [
  133. '有效期:' . $card_info[ 'valid_time' ] > 0 ? time_to_date($card_info[ 'valid_time' ]) : '永久有效',
  134. 18,
  135. [ 51, 51, 51 ],
  136. 290,
  137. 1060,
  138. 548,
  139. 1,
  140. false,
  141. 1
  142. ]
  143. ],
  144. ];
  145. if (!empty($member_info)) {
  146. $member_option = [
  147. [
  148. 'action' => 'imageCircularCopy', // 写入用户头像
  149. 'data' => [
  150. !empty($member_info[ 'headimg' ]) ? $member_info[ 'headimg' ] : 'public/static/img/default_img/head.png',
  151. 86,
  152. 90,
  153. 101,
  154. 101
  155. ]
  156. ],
  157. [
  158. 'action' => 'imageText', // 写入分享人昵称
  159. 'data' => [
  160. $member_info[ 'nickname' ],
  161. 28,
  162. [ 255, 255, 255 ],
  163. 210,
  164. 135,
  165. 420,
  166. 1,
  167. true,
  168. 1
  169. ]
  170. ],
  171. [
  172. 'action' => 'imageText',
  173. 'data' => [
  174. '送给您一张礼品卡',
  175. 22,
  176. [ 255, 255, 255 ],
  177. 200,
  178. 185,
  179. 430,
  180. 1,
  181. 0,
  182. 1
  183. ]
  184. ]
  185. ];
  186. $option = array_merge($option, $member_option);
  187. }
  188. $option_res = $poster->create($option);
  189. if (is_array($option_res)) return $option_res;
  190. $res = $option_res->jpeg('upload/poster/goods', 'goods_' . $promotion_type . '_' . $card_info[ 'no' ] . '_' . $qrcode_param[ 'source_member' ] . '_' . time() . '_' . $app_type);
  191. if ($res[ 'code' ] == 0) {
  192. $upload = new Upload($site_id);
  193. $cloud_res = $upload->fileCloud($res[ 'data' ][ 'path' ]);
  194. if ($cloud_res[ 'code' ] >= 0) {
  195. return $this->success([ "path" => $cloud_res[ 'data' ] ]);
  196. } else {
  197. return $this->error();
  198. }
  199. }
  200. return $res;
  201. } catch (\Exception $e) {
  202. return $this->error($e->getMessage() . $e->getFile() . $e->getLine());
  203. }
  204. }
  205. /**
  206. * 获取用户信息
  207. * @param unknown $member_id
  208. */
  209. private function getMemberInfo($member_id)
  210. {
  211. $info = model('member')->getInfo([ 'member_id' => $member_id ], 'nickname,headimg');
  212. return $info;
  213. }
  214. /**
  215. * 获取二维码
  216. * @param unknown $app_type 请求类型
  217. * @param unknown $page uniapp页面路径
  218. * @param unknown $qrcode_param 二维码携带参数
  219. * @param string $promotion_type 活动类型 null为无活动
  220. */
  221. private function getQrcode($app_type, $page, $qrcode_param, $promotion_type, $site_id)
  222. {
  223. $res = event('Qrcode', [
  224. 'site_id' => $site_id,
  225. 'app_type' => $app_type,
  226. 'type' => 'create',
  227. 'data' => $qrcode_param,
  228. 'page' => $page,
  229. 'qrcode_path' => 'upload/qrcode/goods',
  230. 'qrcode_name' => 'goods_' . $promotion_type . '_' . $qrcode_param[ 'no' ] . '_' . $qrcode_param[ 'source_member' ] . '_' . $site_id,
  231. ], true);
  232. return $res;
  233. }
  234. /**
  235. * 获取海报信息
  236. * @param unknown $template_id
  237. */
  238. private function getTemplateInfo($template_id)
  239. {
  240. $info = model('poster_template')->getInfo([ 'template_id' => $template_id ], 'template_id,template_status');
  241. return $info;
  242. }
  243. /**
  244. * 分享图片
  245. * @param $page
  246. * @param $qrcode_param
  247. * @param $site_id
  248. * @return array
  249. */
  250. public function shareImg($page, $qrcode_param, $site_id)
  251. {
  252. try {
  253. $goods_info = $this->getGoodsInfo($qrcode_param[ 'id' ]);
  254. if (empty($goods_info)) return $this->error('未获取到商品信息');
  255. $file_path = 'upload/share_img/bargain_' . $goods_info[ 'pintuan_id' ] . '/sku_' . $goods_info[ 'sku_id' ] . '.jpg';
  256. if (file_exists($file_path)) return $this->success([ 'path' => $file_path ]);
  257. $poster_width = 600;
  258. $poster_height = 480;
  259. $poster = new PosterExtend($poster_width, $poster_height);
  260. $option = [
  261. [
  262. 'action' => 'setBackground', // 设背景色
  263. 'data' => [ 255, 255, 255 ]
  264. ],
  265. [
  266. 'action' => 'imageCopy', // 商品图
  267. 'data' => [
  268. $goods_info[ 'sku_image' ],
  269. 30,
  270. 145,
  271. 200,
  272. 200,
  273. 'square',
  274. 50,
  275. 1
  276. ]
  277. ],
  278. [
  279. 'action' => 'imageText', // 写入商品名称
  280. 'data' => [
  281. $goods_info[ 'sku_name' ],
  282. 22,
  283. [ 51, 51, 51 ],
  284. 250,
  285. 190,
  286. 330,
  287. 2,
  288. false,
  289. 1
  290. ]
  291. ],
  292. [
  293. 'action' => 'imageText', // 写入商品价格
  294. 'data' => [
  295. '拼团价:¥',
  296. 15,
  297. [ 255, 0, 0 ],
  298. 250,
  299. 300,
  300. 300,
  301. 2,
  302. false,
  303. 1
  304. ]
  305. ],
  306. [
  307. 'action' => 'imageText', // 写入商品价格
  308. 'data' => [
  309. $goods_info[ 'pintuan_price' ],
  310. 30,
  311. [ 255, 0, 0 ],
  312. 345,
  313. 300,
  314. 300,
  315. 2,
  316. false,
  317. 1,
  318. PUBLIC_PATH . 'static/font/custom.ttf'
  319. ]
  320. ],
  321. [
  322. 'action' => 'imageText', // 写入商品原价
  323. 'data' => [
  324. '原 价:¥',
  325. 15,
  326. [ 153, 153, 153 ],
  327. 250,
  328. 340,
  329. 300,
  330. 2,
  331. false,
  332. 1
  333. ]
  334. ],
  335. [
  336. 'action' => 'imageText', // 写入商品原价
  337. 'data' => [
  338. $goods_info[ 'price' ],
  339. 16,
  340. [ 153, 153, 153 ],
  341. 345,
  342. 338,
  343. 300,
  344. 2,
  345. false,
  346. 1,
  347. PUBLIC_PATH . 'static/font/custom.ttf',
  348. ]
  349. ],
  350. // 划线(两条线)
  351. [
  352. 'action' => 'imageline',
  353. 'data' => [
  354. 325,
  355. 330,
  356. 325 + imagettfbbox(16, 0, PUBLIC_PATH . 'static/font/custom.ttf', '¥ ' . $goods_info[ 'price' ])[ 2 ],
  357. 330,
  358. [ 153, 153, 153 ],
  359. ]
  360. ],
  361. [
  362. 'action' => 'imageline',
  363. 'data' => [
  364. 325,
  365. 331,
  366. 325 + imagettfbbox(16, 0, PUBLIC_PATH . 'static/font/custom.ttf', '¥ ' . $goods_info[ 'price' ])[ 2 ],
  367. 331,
  368. [ 153, 153, 153 ],
  369. ]
  370. ],
  371. [
  372. 'action' => 'imageCopy', // 背景图
  373. 'data' => [
  374. img('upload/share_img/bg/pintuan_1.png'),
  375. 0,
  376. 0,
  377. 600,
  378. 480,
  379. 'square',
  380. 0,
  381. 1
  382. ]
  383. ],
  384. ];
  385. $option_res = $poster->create($option);
  386. if (is_array($option_res)) {
  387. return $option_res;
  388. }
  389. $res = $option_res->jpeg('upload/share_img/pintuan_' . $goods_info[ 'pintuan_id' ],
  390. 'sku_' . $goods_info[ 'sku_id' ]);
  391. return $res;
  392. } catch (\Exception $e) {
  393. return $this->error($e->getMessage());
  394. }
  395. }
  396. /**
  397. * 删除分享图片
  398. * @param int $pintuan_id
  399. */
  400. public function clearShareImg(int $pintuan_id)
  401. {
  402. $dir = 'upload/share_img/pintuan_' . $pintuan_id;
  403. @deleteDir($dir);
  404. }
  405. }