Poster.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\groupbuy\model;
  11. use addon\postertemplate\model\PosterTemplate as PosterTemplateModel;
  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 goods($app_type, $page, $qrcode_param, $promotion_type, $site_id)
  25. {
  26. try {
  27. $goods_info = $this->getGoodsInfo($qrcode_param['id']);
  28. $img_arr = explode(",", $goods_info['goods_image']);
  29. if (empty($goods_info)) return $this->error('未获取到商品信息');
  30. //判断海报是否存在或停用
  31. $template_info = $this->getTemplateInfo($goods_info['template_id']);
  32. $qrcode_info = $this->getGoodsQrcode($app_type, $page, $qrcode_param, $promotion_type, $site_id);
  33. if ($qrcode_info['code'] < 0) return $qrcode_info;
  34. $site_model = new Site();
  35. $condition = array (
  36. [ "site_id", "=", $site_id ]
  37. );
  38. $site_info = $site_model->getSiteInfo($condition);
  39. if (!empty($qrcode_param['source_member'])) {
  40. $member_info = $this->getMemberInfo($qrcode_param['source_member']);
  41. }
  42. if(empty($goods_info['template_id']) || empty($template_info) || $template_info['template_status']==0){
  43. $poster_width = 720;
  44. $poster_height = 1150;
  45. $poster = new PosterExtend($poster_width, $poster_height);
  46. $option = [
  47. [
  48. 'action' => 'setBackground', // 设背景色
  49. 'data' => [255, 255, 255]
  50. ],
  51. [
  52. 'action' => 'imageCopy', // 写入商品图
  53. 'data' => [
  54. $img_arr[0],
  55. 50,
  56. 165,
  57. 620,
  58. 620,
  59. 'square',
  60. true,
  61. 1
  62. ]
  63. ],
  64. [
  65. 'action' => 'imageText', // 写入商品名称
  66. 'data' => [
  67. $goods_info['goods_name'],
  68. 22,
  69. [35, 35, 35],
  70. 50,
  71. 915,
  72. 360,
  73. 2,
  74. true,
  75. 1
  76. ]
  77. ],
  78. [
  79. 'action' => 'imageCopy', // 写入商品二维码
  80. 'data' => [
  81. $qrcode_info['data']['path'],
  82. 435,
  83. 825,
  84. 240,
  85. 240,
  86. 'square',
  87. 0,
  88. 1
  89. ]
  90. ],
  91. [
  92. 'action' => 'imageText', // 写入提示
  93. 'data' => [
  94. '长按识别二维码',
  95. 19,
  96. [102, 102, 102],
  97. 465,
  98. 1110,
  99. 490,
  100. 1,
  101. 1,
  102. 1
  103. ]
  104. ],
  105. [
  106. 'action' => 'imageText', // 写入商品价格单位
  107. 'data' => [
  108. '团购价:¥',
  109. 22,
  110. [255, 0, 0],
  111. 50,
  112. 860,
  113. 490,
  114. 2,
  115. true,
  116. 1
  117. ]
  118. ],
  119. [
  120. 'action' => 'imageText', // 写入商品价格
  121. 'data' => [
  122. $goods_info['groupbuy_price'],
  123. 30,
  124. [255, 0, 0],
  125. 188,
  126. 862,
  127. 490,
  128. 2,
  129. true,
  130. 1
  131. ]
  132. ],
  133. ];
  134. if (isset($member_info) && !empty($member_info)) {
  135. $member_option = [
  136. [
  137. 'action' => 'imageCircularCopy', // 写入用户头像
  138. 'data' => [
  139. !empty($member_info['headimg']) ? $member_info['headimg'] : 'public/static/img/default_img/head.png',
  140. 50,
  141. 30,
  142. 100,
  143. 100
  144. ]
  145. ],
  146. [
  147. 'action' => 'imageText', // 写入分享人昵称
  148. 'data' => [
  149. $member_info['nickname'],
  150. 22,
  151. [10, 10, 10],
  152. 170,
  153. 80,
  154. 580,
  155. 1,
  156. 1,
  157. 1
  158. ]
  159. ],
  160. [
  161. 'action' => 'imageText', // 写入分享人昵称
  162. 'data' => [
  163. '分享给你一个商品',
  164. 18,
  165. [102, 102, 102],
  166. 170,
  167. 115,
  168. 580,
  169. 1,
  170. 1,
  171. 1
  172. ]
  173. ]
  174. ];
  175. $option = array_merge($option, $member_option);
  176. }
  177. }else{
  178. $condition = [
  179. ['template_id', '=', $goods_info['template_id']],
  180. ['site_id', '=', $site_id]
  181. ];
  182. $poster_template_model = new PosterTemplateModel();
  183. $poster_data = $poster_template_model->getPosterTemplateInfo($condition);
  184. $poster_data['data']['template_json'] = json_decode($poster_data['data']['template_json'],true);
  185. $poster_width = 720;
  186. $poster_height = 1280;
  187. $poster = new PosterExtend($poster_width, $poster_height);
  188. $fontRate = 0.725; // 20px 等于 14.5磅,换算比率 1px = 0.725磅
  189. if(!empty($poster_data['data']['background'])){
  190. list($width,$height,$type,$attr)=getimagesize(img($poster_data['data']['background']));
  191. $back_ground = [
  192. 'action' => 'imageCopy', // 写入背景图
  193. 'data' => [
  194. img($poster_data['data']['background']),
  195. 0,
  196. 0,
  197. $poster_width,
  198. $poster_height,
  199. 'square',
  200. 0,
  201. 1
  202. ]
  203. ];
  204. }else{
  205. $back_ground = [
  206. 'action' => 'setBackground', // 设背景色
  207. 'data' => [255, 255, 255]
  208. ];
  209. }
  210. $ground = [
  211. [
  212. 'action'=>'setBackground',
  213. 'data'=>[255,255,255]
  214. ]
  215. ];
  216. $option = [
  217. $back_ground,
  218. [
  219. 'action' => 'imageText', // 写入店铺名称
  220. 'data' => [
  221. $site_info['data']['site_name'],
  222. $poster_data['data']['template_json']['store_name_font_size']*$fontRate,
  223. hex2rgb($poster_data['data']['template_json']['store_name_color']),
  224. $poster_data['data']['template_json']['store_name_left']*2,
  225. ($poster_data['data']['template_json']['store_name_top']+$poster_data['data']['template_json']['store_name_font_size'])*2,
  226. $poster_data['data']['template_json']['store_name_width']*2,
  227. $poster_data['data']['template_json']['store_name_height']*2,
  228. true,
  229. $poster_data['data']['template_json']['store_name_is_show']
  230. ]
  231. ],
  232. [
  233. 'action' => 'imageCopy', // 店铺logo
  234. 'data' => [
  235. !empty($site_info['data']['logo_square']) ? $site_info['data']['logo_square']: getUrl() . '/app/shop/view/public/img/shop_logo.png',
  236. $poster_data['data']['template_json']['store_logo_left']*2,
  237. $poster_data['data']['template_json']['store_logo_top']*2,
  238. $poster_data['data']['template_json']['store_logo_width']*2,
  239. $poster_data['data']['template_json']['store_logo_height']*2,
  240. 'square',
  241. true,
  242. $poster_data['data']['template_json']['store_logo_is_show']
  243. ]
  244. ],
  245. [
  246. 'action' => 'imageCopy', // 写入商品图
  247. 'data' => [
  248. $img_arr[0],
  249. $poster_data['data']['template_json']['goods_img_left']*2,
  250. $poster_data['data']['template_json']['goods_img_top']*2,
  251. $poster_data['data']['template_json']['goods_img_width']*2,
  252. $poster_data['data']['template_json']['goods_img_height']*2,
  253. !empty($poster_data['data']['template_json']['goods_img_shape'])?$poster_data['data']['template_json']['goods_img_shape']:'square',
  254. 0,
  255. $poster_data['data']['template_json']['goods_img_is_show']
  256. ]
  257. ],
  258. [
  259. 'action' => 'imageText', // 写入商品名称
  260. 'data' => [
  261. $goods_info['goods_name'],
  262. $poster_data['data']['template_json']['goods_name_font_size']*$fontRate,
  263. hex2rgb($poster_data['data']['template_json']['goods_name_color']),
  264. $poster_data['data']['template_json']['goods_name_left']*2,
  265. ($poster_data['data']['template_json']['goods_name_top']+$poster_data['data']['template_json']['goods_market_price_font_size'])*2,
  266. $poster_data['data']['template_json']['goods_name_width']*2,
  267. 1,//文本行数 $poster_data['data']['template_json']['goods_name_height']*2,
  268. true,
  269. $poster_data['data']['template_json']['goods_name_is_show']
  270. ]
  271. ],
  272. [
  273. 'action' => 'imageCopy', // 写入商品二维码
  274. 'data' => [
  275. $qrcode_info['data']['path'],
  276. $poster_data['data']['qrcode_left']*2,
  277. $poster_data['data']['qrcode_top']*2,
  278. $poster_data['data']['qrcode_width']*2,
  279. $poster_data['data']['qrcode_height']*2,
  280. 'square',
  281. 0,
  282. 1
  283. ]
  284. ],
  285. [
  286. 'action' => 'imageText', // 写入商品价格
  287. 'data' => [
  288. '¥' . $goods_info['groupbuy_price'],
  289. $poster_data['data']['template_json']['goods_price_font_size']*$fontRate,
  290. hex2rgb($poster_data['data']['template_json']['goods_price_color']),
  291. $poster_data['data']['template_json']['goods_price_left']*2,
  292. ($poster_data['data']['template_json']['goods_price_top']+$poster_data['data']['template_json']['goods_market_price_font_size'])*2,
  293. $poster_data['data']['template_json']['goods_price_width']*2,
  294. $poster_data['data']['template_json']['goods_price_height']*2,
  295. true,
  296. $poster_data['data']['template_json']['goods_price_is_show']
  297. ]
  298. ],
  299. ];
  300. if ($goods_info['price'] ==0 ){
  301. $line = '一一一';
  302. }else{
  303. $line = '一一一一';
  304. }
  305. $market_price = [
  306. [
  307. 'action' => 'imageText', // 写入商品划线价格
  308. 'data' => [
  309. '¥' . $goods_info['price'],
  310. $poster_data['data']['template_json']['goods_market_price_font_size']*$fontRate,
  311. hex2rgb($poster_data['data']['template_json']['goods_market_price_color']),
  312. $poster_data['data']['template_json']['goods_market_price_left']*2,
  313. ($poster_data['data']['template_json']['goods_market_price_top']+$poster_data['data']['template_json']['goods_market_price_font_size'])*2,
  314. $poster_data['data']['template_json']['goods_market_price_width']*2,
  315. $poster_data['data']['template_json']['goods_market_price_height']*2,
  316. true,
  317. $poster_data['data']['template_json']['goods_market_price_is_show'] ?? 0
  318. ]
  319. ],
  320. [
  321. 'action' => 'imageText', // 写入线
  322. 'data' => [
  323. $line,
  324. $poster_data['data']['template_json']['goods_market_price_font_size']*$fontRate*2,
  325. hex2rgb($poster_data['data']['template_json']['goods_market_price_color']),
  326. $poster_data['data']['template_json']['goods_market_price_left']*2-5,
  327. ($poster_data['data']['template_json']['goods_market_price_top']+$poster_data['data']['template_json']['goods_market_price_font_size'])*2,
  328. $poster_data['data']['template_json']['goods_market_price_width']*2,
  329. $poster_data['data']['template_json']['goods_market_price_height']*2,
  330. true,
  331. $poster_data['data']['template_json']['goods_market_price_is_show']
  332. ]
  333. ],
  334. ];
  335. $option = array_merge($option, $market_price);
  336. if (!empty($member_info)) {
  337. $member_option = [
  338. [
  339. 'action' => 'imageCopy', // 写入用户头像
  340. 'data' => [
  341. !empty($member_info['headimg']) ? $member_info['headimg'] : 'public/static/img/default_img/head.png',
  342. $poster_data['data']['template_json']['headimg_left']*2,
  343. $poster_data['data']['template_json']['headimg_top']*2,
  344. $poster_data['data']['template_json']['headimg_width']*2,
  345. $poster_data['data']['template_json']['headimg_height']*2,
  346. !empty($poster_data['data']['template_json']['headimg_shape'])?$poster_data['data']['template_json']['headimg_shape']:'square',
  347. 0,
  348. $poster_data['data']['template_json']['headimg_is_show']
  349. ]
  350. ],
  351. [
  352. 'action' => 'imageText', // 写入分享人昵称
  353. 'data' => [
  354. $member_info['nickname'],
  355. $poster_data['data']['template_json']['nickname_font_size']*$fontRate,
  356. hex2rgb($poster_data['data']['template_json']['nickname_color']),
  357. $poster_data['data']['template_json']['nickname_left']*2,
  358. ($poster_data['data']['template_json']['nickname_top']+$poster_data['data']['template_json']['nickname_font_size'])*2,
  359. $poster_data['data']['template_json']['nickname_width']*2,
  360. $poster_data['data']['template_json']['nickname_height']*2,
  361. 0,
  362. $poster_data['data']['template_json']['nickname_is_show']
  363. ]
  364. ],
  365. ];
  366. $option = array_merge($ground, $option, $member_option);
  367. }
  368. }
  369. $option_res = $poster->create($option);
  370. if (is_array($option_res)) return $option_res;
  371. $res = $option_res->jpeg('upload/poster/goods', 'goods_' . $promotion_type . '_' . $qrcode_param['id'] . '_' . $qrcode_param['source_member'] . '_' .time().'_' . $app_type);
  372. if ($res['code'] == 0) {
  373. $upload = new Upload($site_id);
  374. $cloud_res = $upload->fileCloud($res['data']['path']);
  375. if ($cloud_res['code'] >= 0) {
  376. return $this->success([ "path" => $cloud_res[ 'data' ]]);
  377. } else {
  378. return $this->error();
  379. }
  380. }
  381. return $res;
  382. } catch (\Exception $e) {
  383. return $this->error($e->getMessage() . $e->getFile() . $e->getLine());
  384. }
  385. }
  386. /**
  387. * 获取用户信息
  388. * @param unknown $member_id
  389. */
  390. private function getMemberInfo($member_id)
  391. {
  392. $info = model('member')->getInfo(['member_id' => $member_id], 'nickname,headimg');
  393. return $info;
  394. }
  395. /**
  396. * 获取商品信息
  397. * @param unknown $sku_id
  398. */
  399. private function getGoodsInfo($groupbuy_id)
  400. {
  401. $field = 'pg.*,g.goods_name,g.goods_image,g.template_id,g.price';
  402. $alias = 'pg';
  403. $join = [
  404. ['goods g','g.goods_id = pg.goods_id','inner']
  405. ];
  406. $condition = [
  407. ['pg.groupbuy_id','=',$groupbuy_id],['g.goods_state','=',1],['g.is_delete','=',0]
  408. ];
  409. $info = model('promotion_groupbuy')->getInfo($condition,$field,$alias,$join);
  410. return $info;
  411. }
  412. /**
  413. * 获取商品二维码
  414. * @param unknown $app_type 请求类型
  415. * @param unknown $page uniapp页面路径
  416. * @param unknown $qrcode_param 二维码携带参数
  417. * @param string $promotion_type 活动类型 null为无活动
  418. */
  419. private function getGoodsQrcode($app_type, $page, $qrcode_param, $promotion_type, $site_id)
  420. {
  421. $res = event('Qrcode', [
  422. 'site_id' => $site_id,
  423. 'app_type' => $app_type,
  424. 'type' => 'create',
  425. 'data' => $qrcode_param,
  426. 'page' => $page,
  427. 'qrcode_path' => 'upload/qrcode/goods',
  428. 'qrcode_name' => 'goods_' . $promotion_type . '_' . $qrcode_param['id'] . '_' . $qrcode_param['source_member'] . '_' . $site_id,
  429. ], true);
  430. return $res;
  431. }
  432. /**
  433. * 获取海报信息
  434. * @param unknown $template_id
  435. */
  436. private function getTemplateInfo($template_id)
  437. {
  438. $info = model('poster_template')->getInfo(['template_id' => $template_id], 'template_id,template_status');
  439. return $info;
  440. }
  441. }