Poster.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\fenxiao\model;
  11. use app\model\BaseModel;
  12. use extend\Poster as PosterExtend;
  13. use app\model\upload\Upload;
  14. /**
  15. * 海报生成类
  16. */
  17. class Poster extends BaseModel
  18. {
  19. /**
  20. * 分销海报
  21. * @param $app_type
  22. * @param $page
  23. * @param $qrcode_param
  24. * @param $site_id
  25. * @param null $template_info
  26. * @return array|\extend\multitype|mixed|string|void
  27. */
  28. public function distribution($app_type, $page, $qrcode_param, $site_id, $template_info = null)
  29. {
  30. try {
  31. $qrcode_info = $this->getQrcode($app_type, $page, $qrcode_param, $site_id);
  32. if ($qrcode_info[ 'code' ] < 0) return $qrcode_info;
  33. $member_info = $this->getMemberInfo($qrcode_param[ 'source_member' ]);
  34. if (empty($member_info)) return $this->error('未获取到会员信息');
  35. $poster = new PosterExtend(720, 1280);
  36. if (empty($template_info)) {
  37. $option = [
  38. [
  39. 'action' => 'imageCopy', // 背景图
  40. 'data' => [
  41. 'upload/poster/bg/fenxiao_2.png',
  42. 0,
  43. 0,
  44. 720,
  45. 1280,
  46. 'square',
  47. 0,
  48. 1
  49. ]
  50. ],
  51. [
  52. 'action' => 'imageCopy', // 写入二维码
  53. 'data' => [
  54. $qrcode_info[ 'data' ][ 'path' ],
  55. 492,
  56. 1035,
  57. 150,
  58. 150,
  59. 'square',
  60. 0,
  61. 1
  62. ]
  63. ],
  64. [
  65. 'action' => 'imageCircularCopy', // 写入用户头像
  66. 'data' => [
  67. !empty($member_info[ 'headimg' ]) ? $member_info[ 'headimg' ] : 'public/static/img/default_img/head.png',
  68. 65,
  69. 1020,
  70. 100,
  71. 100
  72. ]
  73. ],
  74. [
  75. 'action' => 'imageText', // 写入分享人昵称
  76. 'data' => [
  77. $member_info[ 'nickname' ],
  78. 22,
  79. [ 255, 129, 61 ],
  80. 180,
  81. 1060,
  82. 440,
  83. 1,
  84. true,
  85. 1
  86. ]
  87. ]
  88. ];
  89. } else {
  90. $params = $template_info;
  91. $poster = new PosterExtend(740, 1250);
  92. $fontRate = 0.725;
  93. $nickname_color = is_array($params[ 'template_json' ][ 'nickname_color' ]) ? $params[ 'template_json' ][ 'nickname_color' ] : hex2rgb($params[ 'template_json' ][ 'nickname_color' ]);
  94. $option = [
  95. [
  96. 'action' => 'imageCopy', // 写入背景图
  97. 'data' => [
  98. img($params[ 'background' ]),
  99. 0,
  100. 0,
  101. 740,
  102. 1250,
  103. 'square',
  104. 0,
  105. 1
  106. ]
  107. ],
  108. [
  109. 'action' => 'imageCopy', // 写入二维码
  110. 'data' => [
  111. $qrcode_info[ 'data' ][ 'path' ],
  112. (int) $params[ 'qrcode_left' ] * 2,
  113. (int) $params[ 'qrcode_top' ] * 2,
  114. (int) $params[ 'qrcode_width' ] * 2,
  115. (int) $params[ 'qrcode_height' ] * 2,
  116. 'square',
  117. 0,
  118. 1
  119. ]
  120. ],
  121. [
  122. 'action' => 'imageText', // 写入分享语
  123. 'data' => [
  124. $params[ 'template_json' ][ 'share_content' ],
  125. $params[ 'template_json' ][ 'share_content_font_size' ] * $fontRate * 2,
  126. is_array($params[ 'template_json' ][ 'share_content_color' ]) ? $params[ 'template_json' ][ 'share_content_color' ] : hex2rgb($params[ 'template_json' ][ 'share_content_color' ]),
  127. $params[ 'template_json' ][ 'share_content_left' ] * 2,
  128. ( $params[ 'template_json' ][ 'share_content_top' ] + $params[ 'template_json' ][ 'share_content_font_size' ] ) * 2,
  129. $params[ 'template_json' ][ 'share_content_width' ] * 2,
  130. 1
  131. ]
  132. ],
  133. [
  134. 'action' => 'imageCopy', // 写入用户头像
  135. 'data' => [
  136. !empty($member_info[ 'headimg' ]) ? img($member_info[ 'headimg' ]) : img('public/static/img/default_img/head.png'),
  137. $params[ 'template_json' ][ 'headimg_left' ] * 2,
  138. $params[ 'template_json' ][ 'headimg_top' ] * 2,
  139. $params[ 'template_json' ][ 'headimg_width' ] * 2,
  140. $params[ 'template_json' ][ 'headimg_height' ] * 2,
  141. !empty($params[ 'template_json' ][ 'headimg_shape' ]) ? $params[ 'template_json' ][ 'headimg_shape' ] : 'square',
  142. 0,
  143. $params[ 'template_json' ][ 'headimg_is_show' ]
  144. ]
  145. ],
  146. [
  147. 'action' => 'imageText', // 写入分享人昵称
  148. 'data' => [
  149. $member_info[ 'nickname' ],
  150. $params[ 'template_json' ][ 'nickname_font_size' ] * $fontRate * 2,
  151. $nickname_color,
  152. $params[ 'template_json' ][ 'nickname_left' ] * 2,
  153. ( $params[ 'template_json' ][ 'nickname_top' ] + $params[ 'template_json' ][ 'nickname_font_size' ] ) * 2,
  154. $params[ 'template_json' ][ 'nickname_width' ] * 2,
  155. 1,
  156. true,
  157. $params[ 'template_json' ][ 'nickname_is_show' ]
  158. ]
  159. ],
  160. ];
  161. }
  162. $option_res = $poster->create($option);
  163. if (is_array($option_res)) return $option_res;
  164. $pic_name = "";
  165. if (!empty($params)) {
  166. $pic_name = md5(json_encode([
  167. 'id' => $params[ 'template_id' ],
  168. 'poster_name' => $params[ 'poster_name' ]
  169. ]));
  170. $pic_name = "_" . $pic_name;
  171. }
  172. $res = $option_res->jpeg('upload/poster/distribution', 'distribution_' . $qrcode_param[ 'source_member' ] . $pic_name . '_' . $app_type);
  173. if ($res[ 'code' ] == 0) {
  174. $upload = new Upload($site_id);
  175. $cloud_res = $upload->fileCloud($res[ 'data' ][ 'path' ]);
  176. if ($cloud_res[ 'code' ] >= 0) {
  177. return $this->success([ "path" => $cloud_res[ 'data' ] ]);
  178. } else {
  179. return $this->error();
  180. }
  181. }
  182. return $res;
  183. } catch (\Exception $e) {
  184. return $this->error($e->getMessage() . $e->getFile() . $e->getLine());
  185. }
  186. }
  187. /**
  188. * 获取用户信息
  189. * @param unknown $member_id
  190. */
  191. private function getMemberInfo($member_id)
  192. {
  193. $info = model('member')->getInfo([ 'member_id' => $member_id ], 'nickname,headimg');
  194. return $info;
  195. }
  196. /**
  197. * 获取二维码
  198. * @param unknown $app_type 请求类型
  199. * @param unknown $page uniapp页面路径
  200. * @param unknown $qrcode_param 二维码携带参数
  201. * @param string $promotion_type 活动类型 null为无活动
  202. */
  203. private function getQrcode($app_type, $page, $qrcode_param, $site_id)
  204. {
  205. $res = event('Qrcode', [
  206. 'site_id' => $site_id,
  207. 'app_type' => $app_type,
  208. 'type' => 'get',
  209. 'data' => $qrcode_param,
  210. 'page' => $page,
  211. 'qrcode_path' => 'upload/qrcode/distribution',
  212. 'qrcode_name' => 'distribution' . '_' . $qrcode_param[ 'source_member' ] . '_' . $site_id,
  213. ], true);
  214. return $res;
  215. }
  216. /**
  217. * 获取分销海报
  218. * @param $param
  219. * @return array|\extend\multitype|PosterExtend|mixed|string|void
  220. */
  221. public function getFenxiaoPoster($param)
  222. {
  223. $app_type = $param[ 'app_type' ] ?? 'h5';
  224. $qrcode_param = $param[ 'qrcode_param' ] ?? [];
  225. $site_id = $param[ 'site_id' ] ?? 0;
  226. $page = $param[ 'page' ] ?? '';
  227. $template_id = $param[ 'template_id' ] ?? 'default';
  228. if ($template_id == 'default') {
  229. //默认海报
  230. $template_info = null;
  231. } else {
  232. //设置的海报
  233. $template_info = model('poster_template')->getInfo([
  234. [ 'site_id', '=', $site_id ],
  235. [ 'template_type', '=', 'fenxiao' ],
  236. [ 'template_status', '=', 1 ],
  237. [ 'template_id', '=', $template_id ],
  238. ]);
  239. if (empty($template_info)) return $this->error(null, '模板信息有误');
  240. $template_info[ 'template_json' ] = json_decode($template_info[ 'template_json' ], true);
  241. }
  242. $res = $this->distribution($app_type, $page, $qrcode_param, $site_id, $template_info);
  243. return $res;
  244. }
  245. }