Postertemplate.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\postertemplate\shop\controller;
  11. use addon\postertemplate\model\PosterTemplate as PosterTemplateModel;
  12. use app\model\system\Site;
  13. use app\model\upload\Upload;
  14. use app\shop\controller\BaseShop;
  15. use extend\Poster as PosterExtend;
  16. /**
  17. * 海报模板 控制器
  18. */
  19. class Postertemplate extends BaseShop
  20. {
  21. /**
  22. * 海报模板列表
  23. * @return mixed
  24. */
  25. public function lists()
  26. {
  27. if (request()->isAjax()) {
  28. $page_index = input('page', 1);
  29. $page_size = input('page_size', PAGE_LIST_ROWS);
  30. $search_text = input('search_text', '');
  31. $condition = [ [ 'site_id', '=', $this->site_id ] ];
  32. $condition[] = [ 'template_type', '=', 'goods' ];
  33. if (!empty($search_text)) {
  34. $condition[] = [ 'poster_name', 'like', '%' . $search_text . '%' ];
  35. }
  36. $poster_template_model = new PosterTemplateModel();
  37. $res = $poster_template_model->getPosterTemplatePageList($condition, $page_index, $page_size);
  38. return $res;
  39. } else {
  40. return $this->fetch('poster_template/lists');
  41. }
  42. }
  43. /**
  44. * 添加海报模板
  45. * @return mixed
  46. */
  47. public function addPosterTemplate()
  48. {
  49. $poster_template_model = new PosterTemplateModel();
  50. if (request()->isAjax()) {
  51. $default_template = PosterTemplateModel::DEFAULT_TEMPLATE;
  52. $add_data = [];
  53. $template_json = [];
  54. foreach ($default_template as $field => $field_value) {
  55. if ($field == 'template_json') {
  56. foreach ($default_template[ 'template_json' ] as $json_field => $json_field_value) {
  57. $template_json[ $json_field ] = input($json_field, $json_field_value);
  58. }
  59. } else {
  60. $add_data[ $field ] = input($field, $field_value);
  61. }
  62. }
  63. unset($add_data[ 'template_id' ]);
  64. $add_data[ 'site_id' ] = $this->site_id;
  65. $add_data[ 'template_json' ] = json_encode($template_json, true);
  66. return $poster_template_model->addPosterTemplate($add_data);
  67. } else {
  68. //模板信息
  69. $muban_id = input('muban_id', 0);
  70. $muban_info = $muban_info = $poster_template_model->getMubanInfo([ [ 'muban_id', '=', $muban_id ] ])[ 'data' ];
  71. if (!empty($muban_info)) {
  72. $template_info = $muban_info;
  73. $template_info[ 'template_json' ] = json_decode($template_info[ 'template_json' ], true);
  74. $template_info[ 'template_id' ] = 0;
  75. } else {
  76. $template_info = PosterTemplateModel::DEFAULT_TEMPLATE;
  77. }
  78. $template_info = array_merge($template_info, $template_info[ 'template_json' ]);
  79. unset($template_info[ 'template_json' ]);
  80. $this->assign('template_info', $template_info);
  81. //站点信息
  82. $site_model = new Site();
  83. $site_info = $site_model->getSiteInfo([ [ "site_id", "=", $this->site_id ] ], 'site_name,logo')[ 'data' ];
  84. $this->assign('site_data', $site_info);
  85. return $this->fetch('poster_template/add');
  86. }
  87. }
  88. /**
  89. * 添加海报模板
  90. * @return mixed
  91. */
  92. public function editPosterTemplate()
  93. {
  94. $template_id = input('template_id', '');
  95. $poster_template_model = new PosterTemplateModel();
  96. if (request()->isAjax()) {
  97. $default_template = PosterTemplateModel::DEFAULT_TEMPLATE;
  98. $edit_data = [];
  99. $template_json = [];
  100. foreach ($default_template as $field => $field_value) {
  101. if ($field == 'template_json') {
  102. foreach ($default_template[ 'template_json' ] as $json_field => $json_field_value) {
  103. $template_json[ $json_field ] = input($json_field, $json_field_value);
  104. }
  105. } else {
  106. $edit_data[ $field ] = input($field, $field_value);
  107. }
  108. }
  109. unset($edit_data[ 'template_id' ]);
  110. $edit_data[ 'site_id' ] = $this->site_id;
  111. $edit_data[ 'template_json' ] = json_encode($template_json, true);
  112. return $poster_data = $poster_template_model->editPosterTemplate($edit_data, [
  113. [ 'template_id', '=', $template_id ],
  114. [ 'site_id', '=', $this->site_id ]
  115. ]);
  116. } else {
  117. //模板信息
  118. $template_info = $poster_template_model->getPosterTemplateInfo([
  119. [ 'site_id', '=', $this->site_id ],
  120. [ 'template_id', '=', $template_id ],
  121. ], '*')[ 'data' ];
  122. $template_info = array_merge($template_info, json_decode($template_info[ 'template_json' ], true));
  123. if (empty($template_info)) $this->error('模板信息有误');
  124. unset($template_info[ 'template_json' ]);
  125. $this->assign('template_info', $template_info);
  126. //站点信息
  127. $site_model = new Site();
  128. $site_info = $site_model->getSiteInfo([ [ "site_id", "=", $this->site_id ] ], 'site_name,logo');
  129. $this->assign('site_data', $site_info[ 'data' ]);
  130. return $this->fetch('poster_template/add');
  131. }
  132. }
  133. /**
  134. * 获取海报模板预览
  135. * @return mixed
  136. */
  137. public function posterTemplateDetail()
  138. {
  139. $template_id = input('template_id', '');
  140. $condition = [
  141. [ 'template_id', '=', $template_id ],
  142. [ 'site_id', '=', $this->site_id ]
  143. ];
  144. $poster_template_model = new PosterTemplateModel();
  145. $poster_data = $poster_template_model->getPosterTemplateInfo($condition);
  146. if (empty($poster_data[ 'data' ])) {
  147. return $poster_template_model->error(null, '模板信息有误');
  148. }
  149. $poster_data[ 'data' ][ 'template_json' ] = json_decode($poster_data[ 'data' ][ 'template_json' ], true);
  150. $site_model = new Site();
  151. $site_info = $site_model->getSiteInfo([ [ "site_id", "=", $this->site_id ] ]);
  152. $poster_width = 720;
  153. $poster_height = 1280;
  154. $poster = new PosterExtend($poster_width, $poster_height);
  155. $ground = [
  156. [
  157. 'action' => 'setBackground', // 设背景色
  158. 'data' => [ 255, 255, 255 ]
  159. ],
  160. ];
  161. $fontRate = 0.725; // 20px 等于 14.5磅,换算比率 1px = 0.725磅
  162. $option = [
  163. [
  164. 'action' => 'imageText', // 写入店铺名称
  165. 'data' => [
  166. $site_info[ 'data' ][ 'site_name' ],
  167. $poster_data[ 'data' ][ 'template_json' ][ 'store_name_font_size' ] * $fontRate * 2,
  168. hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'store_name_color' ]),
  169. $poster_data[ 'data' ][ 'template_json' ][ 'store_name_left' ] * 2,
  170. ( $poster_data[ 'data' ][ 'template_json' ][ 'store_name_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'store_name_font_size' ] ) * 2,
  171. $poster_data[ 'data' ][ 'template_json' ][ 'store_name_width' ] * 2,
  172. $poster_data[ 'data' ][ 'template_json' ][ 'store_name_height' ] * 2,
  173. 0,
  174. $poster_data[ 'data' ][ 'template_json' ][ 'store_name_is_show' ]
  175. ]
  176. ],
  177. [
  178. 'action' => 'imageCopy', // 店铺logo
  179. 'data' => [
  180. !empty($site_info[ 'data' ][ 'logo_square' ]) ? $site_info[ 'data' ][ 'logo_square' ] : getUrl() . '/app/shop/view/public/img/shop_logo.png',
  181. $poster_data[ 'data' ][ 'template_json' ][ 'store_logo_left' ] * 2,
  182. $poster_data[ 'data' ][ 'template_json' ][ 'store_logo_top' ] * 2,
  183. $poster_data[ 'data' ][ 'template_json' ][ 'store_logo_width' ] * 2,
  184. $poster_data[ 'data' ][ 'template_json' ][ 'store_logo_height' ] * 2,
  185. 'square',
  186. 0,
  187. $poster_data[ 'data' ][ 'template_json' ][ 'store_logo_is_show' ]
  188. ]
  189. ],
  190. [
  191. 'action' => 'imageCopy', // 写入商品图
  192. 'data' => [
  193. getUrl() . '/public/static/img/goods_template.png',
  194. $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_left' ] * 2,
  195. $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_top' ] * 2,
  196. $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_width' ] * 2,
  197. $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_height' ] * 2,
  198. !empty($poster_data[ 'data' ][ 'template_json' ][ 'goods_img_shape' ]) ? $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_shape' ] : 'square',
  199. 0,
  200. $poster_data[ 'data' ][ 'template_json' ][ 'goods_img_is_show' ]
  201. ]
  202. ],
  203. [
  204. 'action' => 'imageText', // 写入商品名称
  205. 'data' => [
  206. '商品名称-商品名称-商品名称',
  207. $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_font_size' ] * $fontRate * 2,
  208. hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'goods_name_color' ]),
  209. $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_left' ] * 2,
  210. ( $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_font_size' ] ) * 2 + 10,
  211. $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_width' ] * 2,
  212. $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_height' ] * 2,
  213. 0,
  214. $poster_data[ 'data' ][ 'template_json' ][ 'goods_name_is_show' ]
  215. ]
  216. ],
  217. [
  218. 'action' => 'imageCopy', // 写入商品二维码
  219. 'data' => [
  220. getUrl() . '/public/static/img/caner_erweima.png',
  221. $poster_data[ 'data' ][ 'qrcode_left' ] * 2,
  222. $poster_data[ 'data' ][ 'qrcode_top' ] * 2,
  223. $poster_data[ 'data' ][ 'qrcode_width' ] * 2,
  224. $poster_data[ 'data' ][ 'qrcode_height' ] * 2,
  225. 'square',
  226. 0,
  227. 1
  228. ]
  229. ],
  230. [
  231. 'action' => 'imageText', // 写入商品价格
  232. 'data' => [
  233. '¥' . '100.00',
  234. $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_font_size' ] * $fontRate * 2,
  235. hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'goods_price_color' ]),
  236. $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_left' ] * 2,
  237. ( $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_font_size' ] ) * 2,
  238. $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_width' ] * 2,
  239. $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_height' ] * 2,
  240. 0,
  241. $poster_data[ 'data' ][ 'template_json' ][ 'goods_price_is_show' ]
  242. ]
  243. ],
  244. [
  245. 'action' => 'imageText', // 写入商品划线价格
  246. 'data' => [
  247. '¥' . '199.00',
  248. $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_font_size' ] * $fontRate * 2,
  249. hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_color' ]),
  250. $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_left' ] * 2,
  251. ( $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_font_size' ] ) * 2,
  252. $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_width' ] * 2,
  253. $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_height' ] * 2,
  254. 0,
  255. $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_is_show' ]
  256. ]
  257. ],
  258. [
  259. 'action' => 'imageText', // 写入线
  260. 'data' => [
  261. '一一一一',
  262. $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_font_size' ] * $fontRate * 2,
  263. hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_color' ]),
  264. $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_left' ] * 2 - 5,
  265. ( $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_font_size' ] ) * 2,
  266. $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_width' ] * 2,
  267. $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_height' ] * 2,
  268. 0,
  269. $poster_data[ 'data' ][ 'template_json' ][ 'goods_market_price_is_show' ]
  270. ]
  271. ],
  272. ];
  273. $member_option = [
  274. [
  275. 'action' => 'imageCopy', // 写入用户头像
  276. 'data' => [
  277. getUrl() . '/public/static/img/default_img/head.png',
  278. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_left' ] * 2,
  279. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_top' ] * 2,
  280. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_width' ] * 2,
  281. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_height' ] * 2,
  282. !empty($poster_data[ 'data' ][ 'template_json' ][ 'headimg_shape' ]) ? $poster_data[ 'data' ][ 'template_json' ][ 'headimg_shape' ] : 'square',
  283. 0,
  284. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_is_show' ]
  285. ]
  286. ],
  287. [
  288. 'action' => 'imageText', // 写入分享人昵称
  289. 'data' => [
  290. '用户昵称',
  291. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_font_size' ] * $fontRate * 2,
  292. hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'nickname_color' ]),
  293. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_left' ] * 2,
  294. ( $poster_data[ 'data' ][ 'template_json' ][ 'nickname_top' ] + $poster_data[ 'data' ][ 'template_json' ][ 'nickname_font_size' ] ) * 2,
  295. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_width' ] * 2,
  296. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_height' ] * 2,
  297. 0,
  298. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_is_show' ]
  299. ]
  300. ],
  301. ];
  302. if (!empty($poster_data[ 'data' ][ 'background' ])) {
  303. list($width, $height, $type, $attr) = getimagesize(img(trim($poster_data[ 'data' ][ 'background' ])));
  304. $height = 720 * $height / $width;
  305. $back_ground = [
  306. [
  307. 'action' => 'imageCopy', // 写入背景图
  308. 'data' => [
  309. img($poster_data[ 'data' ][ 'background' ]),
  310. 0,
  311. 0,
  312. 720,
  313. $height,
  314. 'square',
  315. 0,
  316. 1
  317. ]
  318. ],
  319. ];
  320. } else {
  321. $back_ground = [
  322. [
  323. 'action' => 'setBackground', // 设背景色
  324. 'data' => [ 255, 255, 255 ]
  325. ],
  326. ];
  327. }
  328. $option = array_merge($ground, $back_ground, $option, $member_option);
  329. $option_res = $poster->create($option);
  330. if (is_array($option_res)) return $option_res;
  331. $pic_name = rand(10000, 99999);
  332. $res = $option_res->jpeg('upload/poster/goods', 'goods_' . $pic_name);
  333. if ($res[ 'code' ] == 0) {
  334. $upload = new Upload($this->site_id);
  335. $res = $upload->fileCloud($res[ 'data' ][ 'path' ]);
  336. }
  337. return $res;
  338. }
  339. /**
  340. * 删除海报模板
  341. * @return mixed
  342. */
  343. public function delPosterTemplate()
  344. {
  345. if (request()->isAjax()) {
  346. $template_ids = input('template_ids', '');
  347. $condition = [
  348. [ 'template_id', 'in', $template_ids ],
  349. [ 'site_id', '=', $this->site_id ],
  350. ];
  351. $poster_template_model = new PosterTemplateModel();
  352. $res = $poster_template_model->deletePosterTemplate($condition);
  353. return $res;
  354. }
  355. }
  356. /**
  357. * 编辑模板状态啊
  358. * @return array
  359. */
  360. public function editstatus()
  361. {
  362. if (request()->isAjax()) {
  363. $template_id = input('template_id', 0);
  364. $template_status = input('template_status', 0);
  365. $condition = [
  366. [ 'template_id', 'in', $template_id ],
  367. [ 'site_id', '=', $this->site_id ]
  368. ];
  369. $data = [ 'template_status' => $template_status ];
  370. $poster_template_model = new PosterTemplateModel();
  371. $res = $poster_template_model->editPosterTemplate($data, $condition);
  372. return $res;
  373. }
  374. }
  375. }