Poster.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\pintuan\model;
  11. use addon\postertemplate\model\PosterTemplate;
  12. use app\model\BaseModel;
  13. use app\model\system\Site;
  14. use app\model\upload\Upload;
  15. use app\model\web\Config;
  16. use extend\Poster as PosterExtend;
  17. /**
  18. * 海报生成类
  19. */
  20. class Poster extends BaseModel
  21. {
  22. /**
  23. * 商品海报
  24. */
  25. public function goods($app_type, $page, $qrcode_param, $promotion_type, $site_id)
  26. {
  27. try {
  28. $pintuan_id = $qrcode_param[ 'id' ];
  29. $goods_info = $this->getGoodsInfo($pintuan_id);
  30. if (empty($goods_info)) return $this->error('未获取到商品信息');
  31. $qrcode_info = $this->getGoodsQrcode($app_type, $page, $qrcode_param, $promotion_type, $site_id);
  32. if ($qrcode_info[ 'code' ] < 0) return $qrcode_info;
  33. //判断海报是否存在或停用
  34. $template_info = $this->getTemplateInfo($goods_info[ 'template_id' ]);
  35. $site_model = new Site();
  36. $condition = array (
  37. [ "site_id", "=", $site_id ]
  38. );
  39. $site_info = $site_model->getSiteInfo($condition);
  40. $member_info = [];
  41. if (!empty($qrcode_param[ 'source_member' ])) {
  42. $member_info = $this->getMemberInfo($qrcode_param[ 'source_member' ]);
  43. }
  44. $upload_config_model = new Config();
  45. $upload_config_result = $upload_config_model->getDefaultImg($site_id);
  46. $goods_pintuan_price = $goods_info[ 'pintuan_price' ];
  47. $goods_img = $goods_info[ 'sku_image' ];
  48. $goods_name = $goods_info[ 'sku_name' ];
  49. $group_id = $qrcode_param[ 'group_id' ] ?? 0;
  50. $group_info = model('promotion_pintuan_group')->getInfo([ [ 'group_id', '=', $group_id ] ]);
  51. if (!empty($group_info)) {
  52. $pintuan_num = $group_info[ 'pintuan_num' ];
  53. // if($pintuan_num > 0){
  54. // $pintuan_num_name = $pintuan_num.'人团';
  55. $pintuan_order_info = model('promotion_pintuan_order')->getInfo([ [ 'group_id', '=', $group_id ] ]);
  56. if (!empty($pintuan_order_info)) {
  57. $order_goods_info = model('order_goods')->getInfo([ [ 'order_id', '=', $pintuan_order_info[ 'order_id' ] ] ]);
  58. if (!empty($order_goods_info)) {
  59. $sku_id = $order_goods_info[ 'sku_id' ];
  60. $pintuan_id = $qrcode_param[ 'id' ] ?? 0;
  61. $pintuan_info = model('promotion_pintuan')->getInfo([ [ 'pintuan_id', '=', $pintuan_id ] ]);
  62. if (!empty($pintuan_info)) {
  63. $pintuan_type = $pintuan_info[ 'pintuan_type' ];
  64. $pintuan_goods_info = model('promotion_pintuan_goods')->getInfo([ [ 'pintuan_id', '=', $pintuan_id ], [ 'sku_id', '=', $sku_id ] ]);
  65. if ($pintuan_type == 'ladder') {
  66. $sku_info = model('goods_sku')->getInfo([ [ 'sku_id', '=', $sku_id ] ], 'sku_image,sku_name');
  67. if (!empty($sku_info)) {
  68. $goods_img = $sku_info[ 'sku_image' ];
  69. $goods_name = $sku_info[ 'sku_name' ];
  70. }
  71. if (!empty($pintuan_goods_info)) {
  72. switch ( $pintuan_num ) {
  73. case $pintuan_info[ 'pintuan_num' ]:
  74. $goods_pintuan_price = $pintuan_goods_info[ 'pintuan_price' ];//一级参团价
  75. break;
  76. case $pintuan_info[ 'pintuan_num_2' ]:
  77. $goods_pintuan_price = $pintuan_goods_info[ 'pintuan_price_2' ];//二级参团价
  78. break;
  79. case $pintuan_info[ 'pintuan_num_3' ]:
  80. $goods_pintuan_price = $pintuan_goods_info[ 'pintuan_price_3' ];//三级参团价
  81. break;
  82. default:
  83. break;
  84. }
  85. }
  86. } else {
  87. // $goods_pintuan_price = $pintuan_goods_info[ 'pintuan_price' ];
  88. }
  89. }
  90. }
  91. }
  92. // }
  93. }
  94. if (empty($goods_info[ 'template_id' ]) || empty($template_info) || $template_info[ 'template_status' ] == 0) {
  95. $poster_width = 720;
  96. $poster_height = 1280;
  97. $poster = new PosterExtend($poster_width, $poster_height);
  98. $option = [
  99. [
  100. 'action' => 'imageCopy', // 背景图
  101. 'data' => [
  102. img('upload/poster/bg/promotion_pintuan.png'),
  103. 0,
  104. 0,
  105. 720,
  106. 1280,
  107. 'square',
  108. 0,
  109. 1
  110. ]
  111. ],
  112. [
  113. 'action' => 'imageCopy', // 商品图
  114. 'data' => [
  115. $goods_info['sku_image'],
  116. 86,
  117. 174,
  118. 548,
  119. 548,
  120. 'square',
  121. 5,
  122. 1
  123. ]
  124. ],
  125. [
  126. 'action' => 'imageCopy', // 二维码
  127. 'data' => [
  128. $qrcode_info['data']['path'],
  129. 273,
  130. 916,
  131. 175,
  132. 175,
  133. 'square',
  134. 0,
  135. 1
  136. ]
  137. ],
  138. [
  139. 'action' => 'imageText', // 写入商品价格
  140. 'data' => [
  141. '原价 ¥ '. $goods_info['price'] .' 参团价 ¥ ' . $goods_pintuan_price,
  142. 18,
  143. [51, 51, 51],
  144. 86,
  145. 828,
  146. 548,
  147. 1,
  148. false,
  149. 1
  150. ]
  151. ],
  152. [
  153. 'action' => 'imageText', // 写入商品名称
  154. 'data' => [
  155. $goods_info['sku_name'],
  156. 25,
  157. [34, 34, 34],
  158. 86,
  159. 780,
  160. 548,
  161. 1,
  162. true,
  163. 1
  164. ]
  165. ]
  166. ];
  167. if (!empty($member_info)) {
  168. $member_option = [
  169. [
  170. 'action' => 'imageCircularCopy', // 写入用户头像
  171. 'data' => [
  172. !empty($member_info['headimg']) ? $member_info['headimg'] : $upload_config_result[ 'data' ][ 'value' ][ 'head' ],
  173. 86,
  174. 40,
  175. 101,
  176. 101
  177. ]
  178. ],
  179. [
  180. 'action' => 'imageText', // 写入分享人昵称
  181. 'data' => [
  182. $member_info['nickname'],
  183. 24,
  184. [51, 51, 51],
  185. 210,
  186. 85,
  187. 420,
  188. 1,
  189. 0,
  190. 1
  191. ]
  192. ]
  193. ];
  194. $option = array_merge($option, $member_option);
  195. }
  196. } else {
  197. $condition = [
  198. [ 'template_id', '=', $goods_info[ 'template_id' ] ],
  199. [ 'site_id', '=', $site_id ]
  200. ];
  201. $poster_template_model = new PosterTemplate();
  202. $poster_data = $poster_template_model->getPosterTemplateInfo($condition);
  203. $poster_data[ 'data' ][ 'template_json' ] = json_decode($poster_data[ 'data' ][ 'template_json' ], true);
  204. $poster_width = 720;
  205. $poster_height = 1280;
  206. $poster = new PosterExtend($poster_width, $poster_height);
  207. $fontRate = 0.725; // 20px 等于 14.5磅,换算比率 1px = 0.725磅
  208. if (!empty($poster_data[ 'data' ][ 'background' ])) {
  209. list($width, $height, $type, $attr) = getimagesize(img($poster_data[ 'data' ][ 'background' ]));
  210. $back_ground = [
  211. 'action' => 'imageCopy', // 写入背景图
  212. 'data' => [
  213. img($poster_data[ 'data' ][ 'background' ]),
  214. 0,
  215. 0,
  216. $poster_width,
  217. $poster_height,
  218. 'square',
  219. 0,
  220. 1
  221. ]
  222. ];
  223. } else {
  224. $back_ground = [
  225. 'action' => 'setBackground', // 设背景色
  226. 'data' => [ 255, 255, 255 ]
  227. ];
  228. }
  229. $option = [
  230. $back_ground,
  231. [
  232. 'action' => 'imageText', // 写入店铺名称
  233. 'data' => [
  234. $site_info[ 'data' ][ 'site_name' ],
  235. $poster_data[ 'data' ][ 'template_json' ][ 'store_name_font_size' ] * $fontRate * 2,
  236. hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'store_name_color' ]),
  237. $poster_data[ 'data' ][ 'template_json' ][ 'store_name_left' ] * 2,
  238. ( $poster_data[ 'data' ][ 'template_json' ][ 'store_name_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'store_name_font_size' ] ) * 2,
  239. $poster_data[ 'data' ][ 'template_json' ][ 'store_name_width' ] * 2,
  240. $poster_data[ 'data' ][ 'template_json' ][ 'store_name_height' ] * 2,
  241. true,
  242. $poster_data[ 'data' ][ 'template_json' ][ 'store_name_is_show' ]
  243. ]
  244. ],
  245. [
  246. 'action' => 'imageCopy', // 店铺logo
  247. 'data' => [
  248. !empty($site_info[ 'data' ][ 'logo_square' ]) ? $site_info[ 'data' ][ 'logo_square' ] : getUrl() . '/app/shop/view/public/img/shop_logo.png',
  249. $poster_data[ 'data' ][ 'template_json' ][ 'store_logo_left' ] * 2,
  250. $poster_data[ 'data' ][ 'template_json' ][ 'store_logo_top' ] * 2,
  251. $poster_data[ 'data' ][ 'template_json' ][ 'store_logo_width' ] * 2,
  252. $poster_data[ 'data' ][ 'template_json' ][ 'store_logo_height' ] * 2,
  253. 'square',
  254. 1,
  255. $poster_data[ 'data' ][ 'template_json' ][ 'store_logo_is_show' ]
  256. ]
  257. ],
  258. [
  259. 'action' => 'imageCopy', // 写入商品图
  260. 'data' => [
  261. img($goods_img, 'mid'),
  262. $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_left' ] * 2,
  263. $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_top' ] * 2,
  264. $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_width' ] * 2,
  265. $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_height' ] * 2,
  266. !empty($poster_data[ 'data' ][ 'template_json' ][ 'goods_img_shape' ]) ? $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_shape' ] : 'square',
  267. 0,
  268. $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_is_show' ]
  269. ]
  270. ],
  271. [
  272. 'action' => 'imageText', // 写入商品名称
  273. 'data' => [
  274. $goods_name,
  275. $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_font_size' ] * $fontRate * 2,
  276. hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'goods_name_color' ]),
  277. $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_left' ] * 2,
  278. ( $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_font_size' ] ) * 2,
  279. $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_width' ] * 2,
  280. 1,//文本行数 $poster_data['data']['template_json']['goods_name_height']*2,
  281. true,
  282. $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_is_show' ]
  283. ]
  284. ],
  285. [
  286. 'action' => 'imageCopy', // 写入商品二维码
  287. 'data' => [
  288. $qrcode_info[ 'data' ][ 'path' ],
  289. $poster_data[ 'data' ][ 'qrcode_left' ] * 2,
  290. $poster_data[ 'data' ][ 'qrcode_top' ] * 2,
  291. $poster_data[ 'data' ][ 'qrcode_width' ] * 2,
  292. $poster_data[ 'data' ][ 'qrcode_height' ] * 2,
  293. 'square',
  294. 0,
  295. 1
  296. ]
  297. ],
  298. [
  299. 'action' => 'imageText', // 写入商品价格
  300. 'data' => [
  301. '¥' . $goods_pintuan_price,
  302. $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_font_size' ] * $fontRate * 2,
  303. hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'goods_price_color' ]),
  304. $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_left' ] * 2,
  305. ( $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_font_size' ] ) * 2,
  306. $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_width' ] * 2,
  307. $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_height' ] * 2,
  308. true,
  309. $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_is_show' ]
  310. ]
  311. ],
  312. ];
  313. if ($goods_info['price'] ==0 ){
  314. $line = '一一一';
  315. }else{
  316. $line = '一一一一';
  317. }
  318. $market_price = [
  319. [
  320. 'action' => 'imageText', // 写入商品划线价格
  321. 'data' => [
  322. '¥' . $goods_info[ 'price' ],
  323. $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_font_size' ] * $fontRate * 2,
  324. hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_color' ]),
  325. $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_left' ] * 2,
  326. ( $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_font_size' ] ) * 2,
  327. $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_width' ] * 2,
  328. $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_height' ] * 2,
  329. true,
  330. $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_is_show' ] ?? 0
  331. ]
  332. ],
  333. [
  334. 'action' => 'imageText', // 写入线
  335. 'data' => [
  336. $line,
  337. $poster_data['data']['template_json']['goods_market_price_font_size']*$fontRate*2,
  338. hex2rgb($poster_data['data']['template_json']['goods_market_price_color']),
  339. $poster_data['data']['template_json']['goods_market_price_left']*2-5,
  340. ($poster_data['data']['template_json']['goods_market_price_top']+$poster_data['data']['template_json']['goods_market_price_font_size'])*2,
  341. $poster_data['data']['template_json']['goods_market_price_width']*2,
  342. $poster_data['data']['template_json']['goods_market_price_height']*2,
  343. true,
  344. $poster_data['data']['template_json']['goods_market_price_is_show']
  345. ]
  346. ],
  347. ];
  348. $option = array_merge($option, $market_price);
  349. if (!empty($member_info)) {
  350. $member_option = [
  351. [
  352. 'action' => 'imageCopy', // 写入用户头像
  353. 'data' => [
  354. !empty($member_info[ 'headimg' ]) ? $member_info[ 'headimg' ] : $upload_config_result[ 'data' ][ 'value' ][ 'head' ],
  355. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_left' ] * 2,
  356. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_top' ] * 2,
  357. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_width' ] * 2,
  358. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_height' ] * 2,
  359. !empty($poster_data[ 'data' ][ 'template_json' ][ 'headimg_shape' ]) ? $poster_data[ 'data' ][ 'template_json' ][ 'headimg_shape' ] : 'square',
  360. 0,
  361. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_is_show' ]
  362. ]
  363. ],
  364. [
  365. 'action' => 'imageText', // 写入分享人昵称
  366. 'data' => [
  367. $member_info[ 'nickname' ],
  368. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_font_size' ] * $fontRate * 2,
  369. hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'nickname_color' ]),
  370. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_left' ] * 2,
  371. ( $poster_data[ 'data' ][ 'template_json' ][ 'nickname_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'nickname_font_size' ] ) * 2,
  372. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_width' ] * 2,
  373. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_height' ] * 2,
  374. 0,
  375. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_is_show' ]
  376. ]
  377. ],
  378. ];
  379. $ground = [
  380. [
  381. 'action' => 'setBackground',
  382. 'data' => [ 255, 255, 255 ]
  383. ]
  384. ];
  385. $option = array_merge($ground, $option, $member_option);
  386. }
  387. }
  388. $option_res = $poster->create($option);
  389. if (is_array($option_res)) return $option_res;
  390. $res = $option_res->jpeg('upload/poster/goods', 'goods_' . $promotion_type . '_' . $goods_info[ 'pintuan_id' ] . '_' . $qrcode_param[ 'source_member' ] . '_' . time() . '_' . $app_type);
  391. if ($res[ 'code' ] == 0) {
  392. $upload = new Upload($site_id);
  393. $cloud_res = $upload->fileCloud($res[ 'data' ][ 'path' ]);
  394. if ($cloud_res[ 'code' ] >= 0) {
  395. return $this->success([ "path" => $cloud_res[ 'data' ] ]);
  396. } else {
  397. return $this->error();
  398. }
  399. }
  400. return $res;
  401. } catch (\Exception $e) {
  402. return $this->error($e->getMessage() . $e->getFile() . $e->getLine());
  403. }
  404. }
  405. /**
  406. * 获取用户信息
  407. * @param unknown $member_id
  408. */
  409. private function getMemberInfo($member_id)
  410. {
  411. $info = model('member')->getInfo([ 'member_id' => $member_id ], 'nickname,headimg');
  412. return $info;
  413. }
  414. /**
  415. * 获取商品信息
  416. * @param unknown
  417. */
  418. private function getGoodsInfo($id, $group_id = 0)
  419. {
  420. $alias = 'nppg';
  421. $join = [
  422. [ 'goods_sku ngs', 'nppg.sku_id = ngs.sku_id', 'inner' ],
  423. [ 'promotion_pintuan pp', 'pp.pintuan_id = nppg.pintuan_id', 'left' ]
  424. ];
  425. $field = 'ngs.sku_name,ngs.introduction,ngs.sku_image,ngs.sku_id,nppg.pintuan_price,nppg.pintuan_id,ngs.template_id,ngs.price,nppg.pintuan_price_2,nppg.pintuan_price_3,pp.pintuan_num,pp.pintuan_num_2,pp.pintuan_num_3';
  426. $info = model('promotion_pintuan_goods')->getInfo([ 'nppg.pintuan_id' => $id ], $field, $alias, $join);
  427. // 按参团人数获取拼团价
  428. if (!empty($group_id)) {
  429. $group_info = model('promotion_pintuan_group')->getInfo([ [ 'group_id', '=', $group_id ] ], 'pintuan_num');
  430. if (!empty($group_info)) {
  431. if ($info[ 'pintuan_num_2' ] == $group_info[ 'pintuan_num' ]) $info[ 'pintuan_price' ] = $info[ 'pintuan_price_2' ];
  432. else if ($info[ 'pintuan_num_3' ] == $group_info[ 'pintuan_num' ]) $info[ 'pintuan_price' ] = $info[ 'pintuan_price_3' ];
  433. $info[ 'pintuan_num' ] = $group_info[ 'pintuan_num' ];
  434. }
  435. }
  436. return $info;
  437. }
  438. /**
  439. * 获取商品二维码
  440. * @param unknown $app_type 请求类型
  441. * @param unknown $page uniapp页面路径
  442. * @param unknown $qrcode_param 二维码携带参数
  443. * @param string $promotion_type 活动类型 null为无活动
  444. */
  445. private function getGoodsQrcode($app_type, $page, $qrcode_param, $promotion_type, $site_id)
  446. {
  447. $res = event('Qrcode', [
  448. 'site_id' => $site_id,
  449. 'app_type' => $app_type,
  450. 'type' => 'create',
  451. 'data' => $qrcode_param,
  452. 'page' => $page,
  453. 'qrcode_path' => 'upload/qrcode/goods',
  454. 'qrcode_name' => 'goods_' . $promotion_type . '_' . $qrcode_param[ 'id' ] . '_' . $qrcode_param[ 'source_member' ] . '_' . $site_id,
  455. ], true);
  456. return $res;
  457. }
  458. /**
  459. * 获取海报信息
  460. * @param unknown $template_id
  461. */
  462. private function getTemplateInfo($template_id)
  463. {
  464. $info = model('poster_template')->getInfo([ 'template_id' => $template_id ], 'template_id,template_status');
  465. return $info;
  466. }
  467. /**
  468. * 分享图片
  469. * @param $page
  470. * @param $qrcode_param
  471. * @param $site_id
  472. * @return array
  473. */
  474. public function shareImg($page, $qrcode_param, $site_id)
  475. {
  476. try {
  477. $goods_info = $this->getGoodsInfo($qrcode_param[ 'id' ], $qrcode_param[ 'group_id' ] ?? 0);
  478. if (empty($goods_info)) return $this->error('未获取到商品信息');
  479. $file_path = 'upload/share_img/pintuan_' . $goods_info[ 'pintuan_id' ] . '/sku_' . $goods_info[ 'sku_id' ] . '_' . $goods_info[ 'pintuan_num' ] . '.jpg';
  480. if (file_exists($file_path)) return $this->success([ 'path' => $file_path ]);
  481. $poster_width = 600;
  482. $poster_height = 480;
  483. $poster = new PosterExtend($poster_width, $poster_height);
  484. $option = [
  485. [
  486. 'action' => 'setBackground', // 设背景色
  487. 'data' => [ 255, 255, 255 ]
  488. ],
  489. [
  490. 'action' => 'imageCopy', // 商品图
  491. 'data' => [
  492. img($goods_info[ 'sku_image' ], 'mid'),
  493. 30,
  494. 145,
  495. 200,
  496. 200,
  497. 'square',
  498. 50,
  499. 1
  500. ]
  501. ],
  502. [
  503. 'action' => 'imageText', // 写入商品名称
  504. 'data' => [
  505. $goods_info[ 'sku_name' ],
  506. 22,
  507. [ 51, 51, 51 ],
  508. 250,
  509. 190,
  510. 330,
  511. 2,
  512. false,
  513. 1
  514. ]
  515. ],
  516. [
  517. 'action' => 'imageText', // 写入商品价格
  518. 'data' => [
  519. '拼团价:¥',
  520. 15,
  521. [ 255, 0, 0 ],
  522. 250,
  523. 300,
  524. 300,
  525. 2,
  526. false,
  527. 1
  528. ]
  529. ],
  530. [
  531. 'action' => 'imageText', // 写入商品价格
  532. 'data' => [
  533. $goods_info[ 'pintuan_price' ],
  534. 30,
  535. [ 255, 0, 0 ],
  536. 345,
  537. 300,
  538. 300,
  539. 2,
  540. false,
  541. 1,
  542. PUBLIC_PATH . 'static/font/custom.ttf'
  543. ]
  544. ],
  545. [
  546. 'action' => 'imageText', // 写入商品原价
  547. 'data' => [
  548. '原 价:¥',
  549. 15,
  550. [ 153, 153, 153 ],
  551. 250,
  552. 340,
  553. 300,
  554. 2,
  555. false,
  556. 1
  557. ]
  558. ],
  559. [
  560. 'action' => 'imageText', // 写入商品原价
  561. 'data' => [
  562. $goods_info[ 'price' ],
  563. 16,
  564. [ 153, 153, 153 ],
  565. 345,
  566. 338,
  567. 300,
  568. 2,
  569. false,
  570. 1,
  571. PUBLIC_PATH . 'static/font/custom.ttf',
  572. ]
  573. ],
  574. // 划线(两条线)
  575. [
  576. 'action' => 'imageline',
  577. 'data' => [
  578. 325,
  579. 330,
  580. 325 + imagettfbbox(16, 0, PUBLIC_PATH . 'static/font/custom.ttf', '¥ ' . $goods_info[ 'price' ])[ 2 ],
  581. 330,
  582. [ 153, 153, 153 ],
  583. ]
  584. ],
  585. [
  586. 'action' => 'imageline',
  587. 'data' => [
  588. 325,
  589. 331,
  590. 325 + imagettfbbox(16, 0, PUBLIC_PATH . 'static/font/custom.ttf', '¥ ' . $goods_info[ 'price' ])[ 2 ],
  591. 331,
  592. [ 153, 153, 153 ],
  593. ]
  594. ],
  595. [
  596. 'action' => 'imageCopy', // 背景图
  597. 'data' => [
  598. img('upload/share_img/bg/pintuan_1.png'),
  599. 0,
  600. 0,
  601. 600,
  602. 480,
  603. 'square',
  604. 0,
  605. 1
  606. ]
  607. ],
  608. ];
  609. $option_res = $poster->create($option);
  610. if (is_array($option_res)) {
  611. return $option_res;
  612. }
  613. $res = $option_res->jpeg('upload/share_img/pintuan_' . $goods_info[ 'pintuan_id' ],
  614. 'sku_' . $goods_info[ 'sku_id' ] . '_' . $goods_info[ 'pintuan_num' ]);
  615. return $res;
  616. } catch (\Exception $e) {
  617. return $this->error($e->getMessage());
  618. }
  619. }
  620. /**
  621. * 删除分享图片
  622. * @param int $pintuan_id
  623. */
  624. public function clearShareImg(int $pintuan_id)
  625. {
  626. $dir = 'upload/share_img/pintuan_' . $pintuan_id;
  627. @deleteDir($dir);
  628. }
  629. }