Poster.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\presale\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 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. $goods_info = $this->getGoodsInfo($qrcode_param[ 'id' ]);
  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. $upload_config_model = new Config();
  43. $upload_config_result = $upload_config_model->getDefaultImg($site_id);
  44. if (empty($goods_info[ 'template_id' ]) || empty($template_info) || $template_info[ 'template_status' ] == 0) {
  45. $poster_width = 720;
  46. $poster_height = 1150;
  47. $poster = new PosterExtend($poster_width, $poster_height);
  48. $option = [
  49. [
  50. 'action' => 'setBackground', // 设背景色
  51. 'data' => [255, 255, 255]
  52. ],
  53. [
  54. 'action' => 'imageCopy', // 写入商品图
  55. 'data' => [
  56. img($goods_info['sku_image'], 'mid'),
  57. 50,
  58. 165,
  59. 620,
  60. 620,
  61. 'square',
  62. true,
  63. 1
  64. ]
  65. ],
  66. [
  67. 'action' => 'imageText', // 写入商品名称
  68. 'data' => [
  69. $goods_info['sku_name'],
  70. 22,
  71. [35, 35, 35],
  72. 50,
  73. 915,
  74. 360,
  75. 2,
  76. true,
  77. 1
  78. ]
  79. ],
  80. [
  81. 'action' => 'imageCopy', // 写入商品二维码
  82. 'data' => [
  83. $qrcode_info['data']['path'],
  84. 435,
  85. 825,
  86. 240,
  87. 240,
  88. 'square',
  89. 0,
  90. 1
  91. ]
  92. ],
  93. [
  94. 'action' => 'imageText', // 写入提示
  95. 'data' => [
  96. '长按识别二维码',
  97. 19,
  98. [102, 102, 102],
  99. 465,
  100. 1110,
  101. 490,
  102. 1,
  103. 1,
  104. 1
  105. ]
  106. ],
  107. [
  108. 'action' => 'imageText', // 写入商品价格
  109. 'data' => [
  110. '定金¥' . $goods_info[ 'presale_deposit' ] . '可抵¥' . $goods_info[ 'presale_price' ],
  111. 20,
  112. [255, 0, 0],
  113. 50,
  114. 862,
  115. 490,
  116. 2,
  117. true,
  118. 1
  119. ]
  120. ],
  121. ];
  122. } else {
  123. $condition = [
  124. [ 'template_id', '=', $goods_info[ 'template_id' ] ],
  125. [ 'site_id', '=', $site_id ]
  126. ];
  127. $poster_template_model = new PosterTemplateModel();
  128. $poster_data = $poster_template_model->getPosterTemplateInfo($condition);
  129. $poster_data[ 'data' ][ 'template_json' ] = json_decode($poster_data[ 'data' ][ 'template_json' ], true);
  130. $poster_width = 720;
  131. $poster_height = 1280;
  132. $poster = new PosterExtend($poster_width, $poster_height);
  133. $fontRate = 0.725; // 20px 等于 14.5磅,换算比率 1px = 0.725磅
  134. $option = [
  135. [
  136. 'action' => 'imageText', // 写入店铺名称
  137. 'data' => [
  138. $site_info[ 'data' ][ 'site_name' ],
  139. $poster_data[ 'data' ][ 'template_json' ][ 'store_name_font_size' ] * $fontRate * 2,
  140. hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'store_name_color' ]),
  141. $poster_data[ 'data' ][ 'template_json' ][ 'store_name_left' ] * 2,
  142. ( $poster_data[ 'data' ][ 'template_json' ][ 'store_name_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'store_name_font_size' ] ) * 2,
  143. $poster_data[ 'data' ][ 'template_json' ][ 'store_name_width' ] * 2,
  144. $poster_data[ 'data' ][ 'template_json' ][ 'store_name_height' ] * 2,
  145. true,
  146. $poster_data[ 'data' ][ 'template_json' ][ 'store_name_is_show' ]
  147. ]
  148. ],
  149. [
  150. 'action' => 'imageCopy', // 店铺logo
  151. 'data' => [
  152. !empty($site_info[ 'data' ][ 'logo_square' ]) ? $site_info[ 'data' ][ 'logo_square' ] : getUrl() . '/app/shop/view/public/img/shop_logo.png',
  153. $poster_data[ 'data' ][ 'template_json' ][ 'store_logo_left' ] * 2,
  154. $poster_data[ 'data' ][ 'template_json' ][ 'store_logo_top' ] * 2,
  155. $poster_data[ 'data' ][ 'template_json' ][ 'store_logo_width' ] * 2,
  156. $poster_data[ 'data' ][ 'template_json' ][ 'store_logo_height' ] * 2,
  157. 'square',
  158. true,
  159. $poster_data[ 'data' ][ 'template_json' ][ 'store_logo_is_show' ]
  160. ]
  161. ],
  162. [
  163. 'action' => 'imageCopy', // 写入商品图
  164. 'data' => [
  165. $goods_info[ 'sku_image' ],
  166. $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_left' ] * 2,
  167. $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_top' ] * 2,
  168. $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_width' ] * 2,
  169. $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_height' ] * 2,
  170. !empty($poster_data[ 'data' ][ 'template_json' ][ 'goods_img_shape' ]) ? $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_shape' ] : 'square',
  171. 0,
  172. $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_is_show' ]
  173. ]
  174. ],
  175. [
  176. 'action' => 'imageText', // 写入商品名称
  177. 'data' => [
  178. $goods_info[ 'sku_name' ],
  179. $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_font_size' ] * $fontRate * 2,
  180. hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'goods_name_color' ]),
  181. $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_left' ] * 2,
  182. ( $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_font_size' ] ) * 2,
  183. $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_width' ] * 2,
  184. 1,//文本行数 $poster_data['data']['template_json']['goods_name_height']*2,
  185. true,
  186. $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_is_show' ]
  187. ]
  188. ],
  189. [
  190. 'action' => 'imageCopy', // 写入商品二维码
  191. 'data' => [
  192. $qrcode_info[ 'data' ][ 'path' ],
  193. $poster_data[ 'data' ][ 'qrcode_left' ] * 2,
  194. $poster_data[ 'data' ][ 'qrcode_top' ] * 2,
  195. $poster_data[ 'data' ][ 'qrcode_width' ] * 2,
  196. $poster_data[ 'data' ][ 'qrcode_height' ] * 2,
  197. 'square',
  198. 0,
  199. 1
  200. ]
  201. ],
  202. [
  203. 'action' => 'imageText', // 写入商品价格
  204. 'data' => [
  205. '定金¥' . $goods_info[ 'presale_deposit' ] . '可抵¥' . $goods_info[ 'presale_price' ],
  206. $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_font_size' ] * $fontRate * 2,
  207. hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'goods_price_color' ]),
  208. $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_left' ] * 2,
  209. ( $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_font_size' ] ) * 2,
  210. $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_width' ] * 2,
  211. $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_height' ] * 2,
  212. true,
  213. $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_is_show' ]
  214. ]
  215. ],
  216. ];
  217. if (!empty($member_info)) {
  218. $member_option = [
  219. [
  220. 'action' => 'imageCopy', // 写入用户头像
  221. 'data' => [
  222. !empty($member_info[ 'headimg' ]) ? $member_info[ 'headimg' ] : $upload_config_result[ 'data' ][ 'value' ][ 'head' ],
  223. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_left' ] * 2,
  224. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_top' ] * 2,
  225. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_width' ] * 2,
  226. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_height' ] * 2,
  227. !empty($poster_data[ 'data' ][ 'template_json' ][ 'headimg_shape' ]) ? $poster_data[ 'data' ][ 'template_json' ][ 'headimg_shape' ] : 'square',
  228. 0,
  229. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_is_show' ]
  230. ]
  231. ],
  232. [
  233. 'action' => 'imageText', // 写入分享人昵称
  234. 'data' => [
  235. $member_info[ 'nickname' ],
  236. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_font_size' ] * $fontRate * 2,
  237. hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'nickname_color' ]),
  238. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_left' ] * 2,
  239. ( $poster_data[ 'data' ][ 'template_json' ][ 'nickname_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'nickname_font_size' ] ) * 2,
  240. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_width' ] * 2,
  241. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_height' ] * 2,
  242. 0,
  243. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_is_show' ]
  244. ]
  245. ],
  246. ];
  247. $option = array_merge($option, $member_option);
  248. }
  249. if (!empty($poster_data[ 'data' ][ 'background' ])) {
  250. list($width, $height, $type, $attr) = getimagesize(img($poster_data[ 'data' ][ 'background' ]));
  251. $back_ground = [
  252. [
  253. 'action' => 'imageCopy', // 写入背景图
  254. 'data' => [
  255. img($poster_data[ 'data' ][ 'background' ]),
  256. 0,
  257. 0,
  258. $poster_width,
  259. $poster_height,
  260. 'square',
  261. 0,
  262. 1
  263. ]
  264. ],
  265. ];
  266. } else {
  267. $back_ground = [
  268. [
  269. 'action' => 'setBackground', // 设背景色
  270. 'data' => [ 255, 255, 255 ]
  271. ],
  272. ];
  273. }
  274. $ground = [
  275. [
  276. 'action' => 'setBackground',
  277. 'data' => [ 255, 255, 255 ]
  278. ]
  279. ];
  280. $option = array_merge($ground, $back_ground, $option);
  281. }
  282. $option_res = $poster->create($option);
  283. if (is_array($option_res)) return $option_res;
  284. $res = $option_res->jpeg('upload/poster/goods', 'goods_' . $promotion_type . '_' . $goods_info[ 'presale_id' ] . '_' . $qrcode_param[ 'source_member' ] . '_' . time() . '_' . $app_type);
  285. if ($res[ 'code' ] == 0) {
  286. $upload = new Upload($site_id);
  287. $cloud_res = $upload->fileCloud($res[ 'data' ][ 'path' ]);
  288. if ($cloud_res[ 'code' ] >= 0) {
  289. return $this->success([ "path" => $cloud_res[ 'data' ] ]);
  290. } else {
  291. return $this->error();
  292. }
  293. }
  294. return $res;
  295. } catch (\Exception $e) {
  296. return $this->error($e->getMessage() . $e->getFile() . $e->getLine());
  297. }
  298. }
  299. /**
  300. * 获取用户信息
  301. * @param unknown $member_id
  302. */
  303. private function getMemberInfo($member_id)
  304. {
  305. $info = model('member')->getInfo([ 'member_id' => $member_id ], 'nickname,headimg');
  306. return $info;
  307. }
  308. /**
  309. * 获取商品信息
  310. * @param unknown $sku_id
  311. */
  312. private function getGoodsInfo($id)
  313. {
  314. $join = [
  315. [ 'goods_sku sku', 'pbg.sku_id = sku.sku_id', 'inner' ],
  316. ];
  317. $field = 'pbg.presale_id,pbg.presale_deposit,pbg.presale_price,sku.sku_name,sku.sku_image,sku.template_id';
  318. $info = model('promotion_presale_goods')->getInfo([ 'pbg.presale_id' => $id ], $field, 'pbg', $join);
  319. return $info;
  320. }
  321. /**
  322. * 获取商品二维码
  323. * @param unknown $app_type 请求类型
  324. * @param unknown $page uniapp页面路径
  325. * @param unknown $qrcode_param 二维码携带参数
  326. * @param string $promotion_type 活动类型 null为无活动
  327. */
  328. private function getGoodsQrcode($app_type, $page, $qrcode_param, $promotion_type, $site_id)
  329. {
  330. $res = event('Qrcode', [
  331. 'site_id' => $site_id,
  332. 'app_type' => $app_type,
  333. 'type' => 'create',
  334. 'data' => $qrcode_param,
  335. 'page' => $page,
  336. 'qrcode_path' => 'upload/qrcode/goods',
  337. 'qrcode_name' => 'goods_' . $promotion_type . '_' . $qrcode_param[ 'id' ] . '_' . $qrcode_param[ 'source_member' ] . '_' . $site_id,
  338. ], true);
  339. return $res;
  340. }
  341. /**
  342. * 获取海报信息
  343. * @param unknown $template_id
  344. */
  345. private function getTemplateInfo($template_id)
  346. {
  347. $info = model('poster_template')->getInfo([ 'template_id' => $template_id ], 'template_id,template_status');
  348. return $info;
  349. }
  350. }