PosterServices.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\other;
  12. use dh2y\qrcode\QRcode;
  13. use think\facade\Config;
  14. class PosterServices
  15. {
  16. /**
  17. * TODO 砍价 拼团 分享海报生成
  18. * @param array $data
  19. * @param $path
  20. * @return array|bool|string
  21. * @throws \Exception
  22. */
  23. public static function setShareMarketingPoster($data = array(), $path)
  24. {
  25. $config = array(
  26. 'text' => array(
  27. array(
  28. 'text' => $data['price'],//TODO 价格
  29. 'left' => 116,
  30. 'top' => 200,
  31. 'fontPath' => app()->getRootPath() . 'public/statics/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  32. 'fontSize' => 50, //字号
  33. 'fontColor' => '255,0,0', //字体颜色
  34. 'angle' => 0,
  35. ),
  36. array(
  37. 'text' => $data['label'],//TODO 标签
  38. 'left' => 450,
  39. 'top' => 188,
  40. 'fontPath' => app()->getRootPath() . 'public/statics/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  41. 'fontSize' => 24, //字号
  42. 'fontColor' => '255,255,255', //字体颜色
  43. 'angle' => 0,
  44. ),
  45. array(
  46. 'text' => $data['msg'],//TODO 简述
  47. 'left' => 80,
  48. 'top' => 270,
  49. 'fontPath' => app()->getRootPath() . 'public/statics/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  50. 'fontSize' => 22, //字号
  51. 'fontColor' => '40,40,40', //字体颜色
  52. 'angle' => 0,
  53. )
  54. ),
  55. 'image' => array(
  56. array(
  57. 'url' => $data['image'], //图片
  58. 'stream' => 0,
  59. 'left' => 120,
  60. 'top' => 340,
  61. 'right' => 0,
  62. 'bottom' => 0,
  63. 'width' => 450,
  64. 'height' => 450,
  65. 'opacity' => 100
  66. ),
  67. array(
  68. 'url' => $data['url'], //二维码资源
  69. 'stream' => 0,
  70. 'left' => 260,
  71. 'top' => 890,
  72. 'right' => 0,
  73. 'bottom' => 0,
  74. 'width' => 160,
  75. 'height' => 160,
  76. 'opacity' => 100
  77. )
  78. ),
  79. 'background' => 'statics/poster/poster.jpg'
  80. );
  81. if (!file_exists($config['background'])) exception('缺少系统预设背景图片');
  82. if (strlen($data['title']) < 36) {
  83. $text = array(
  84. 'text' => $data['title'],//TODO 标题
  85. 'left' => 76,
  86. 'top' => 100,
  87. 'fontPath' => app()->getRootPath() . 'public/statics/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  88. 'fontSize' => 32, //字号
  89. 'fontColor' => '0,0,0', //字体颜色
  90. 'angle' => 0,
  91. );
  92. array_push($config['text'], $text);
  93. } else {
  94. $titleOne = array(
  95. 'text' => mb_strimwidth($data['title'], 0, 24),//TODO 标题
  96. 'left' => 76,
  97. 'top' => 70,
  98. 'fontPath' => app()->getRootPath() . 'public/statics/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  99. 'fontSize' => 32, //字号
  100. 'fontColor' => '0,0,0', //字体颜色
  101. 'angle' => 0,
  102. );
  103. $titleTwo = array(
  104. 'text' => mb_strimwidth($data['title'], mb_strlen(mb_strimwidth($data['title'], 0, 24)), 24),//TODO 标题
  105. 'left' => 76,
  106. 'top' => 120,
  107. 'fontPath' => app()->getRootPath() . 'public/statics/font/Alibaba-PuHuiTi-Regular.otf', //字体文件
  108. 'fontSize' => 32, //字号
  109. 'fontColor' => '0,0,0', //字体颜色
  110. 'angle' => 0,
  111. );
  112. array_push($config['text'], $titleOne);
  113. array_push($config['text'], $titleTwo);
  114. }
  115. return self::setSharePoster($config, $path);
  116. }
  117. /**
  118. * TODO 生成分享二维码图片
  119. * @param array $config
  120. * @param $path
  121. * @return array|bool|string
  122. * @throws \Exception
  123. */
  124. public static function setSharePoster($config = array(), $path, $name = '')
  125. {
  126. $imageDefault = array(
  127. 'left' => 0,
  128. 'top' => 0,
  129. 'right' => 0,
  130. 'bottom' => 0,
  131. 'width' => 100,
  132. 'height' => 100,
  133. 'opacity' => 100
  134. );
  135. $textDefault = array(
  136. 'text' => '',
  137. 'left' => 0,
  138. 'top' => 0,
  139. 'fontSize' => 32, //字号
  140. 'fontColor' => '255,255,255', //字体颜色
  141. 'angle' => 0,
  142. );
  143. $background = $config['background'];//海报最底层得背景
  144. if (substr($background, 0, 1) === '/') {
  145. $background = substr($background, 1);
  146. }
  147. $background = str_replace('https://', 'http://', $background);
  148. $backgroundInfo = getimagesize($background);
  149. $background = imagecreatefromstring(file_get_contents($background));
  150. $backgroundWidth = $backgroundInfo[0]; //背景宽度
  151. $backgroundHeight = $backgroundInfo[1]; //背景高度
  152. $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
  153. $color = imagecolorallocate($imageRes, 0, 0, 0);
  154. imagefill($imageRes, 0, 0, $color);
  155. imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background), imagesx($background), imagesy($background));
  156. if (!empty($config['image'])) {
  157. foreach ($config['image'] as $key => $val) {
  158. $val = array_merge($imageDefault, $val);
  159. $val['url'] = str_replace('https', 'http', $val['url']);
  160. $info = getimagesize($val['url']);
  161. $function = 'imagecreatefrom' . image_type_to_extension($info[2], false);
  162. if ($val['stream']) {
  163. $info = getimagesizefromstring($val['url']);
  164. $function = 'imagecreatefromstring';
  165. }
  166. $res = $function($val['url']);
  167. $resWidth = $info[0];
  168. $resHeight = $info[1];
  169. $canvas = imagecreatetruecolor($val['width'], $val['height']);
  170. imagefill($canvas, 0, 0, $color);
  171. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
  172. $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) - $val['width'] : $val['left'];
  173. $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) - $val['height'] : $val['top'];
  174. imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], $val['right'], $val['bottom'], $val['width'], $val['height'], $val['opacity']);//左,上,右,下,宽度,高度,透明度
  175. }
  176. }
  177. if (isset($config['text']) && !empty($config['text'])) {
  178. foreach ($config['text'] as $key => $val) {
  179. $val = array_merge($textDefault, $val);
  180. list($R, $G, $B) = explode(',', $val['fontColor']);
  181. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  182. $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
  183. $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
  184. imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
  185. }
  186. }
  187. ob_start();
  188. imagejpeg($imageRes);
  189. imagedestroy($imageRes);
  190. $res = ob_get_contents();
  191. ob_end_clean();
  192. if ($name == '') {
  193. $key = substr(md5(rand(0, 9999)), 0, 5) . date('YmdHis') . rand(0, 999999) . '.jpg';
  194. } else {
  195. $key = $name;
  196. }
  197. $uploadType = (int)sys_config('upload_type', 1);
  198. $upload = UploadService::init();
  199. $res = $upload->to($path)->validate()->setAuthThumb(false)->stream($res, $key);
  200. if ($res === false) {
  201. return $upload->getError();
  202. } else {
  203. $info = $upload->getUploadInfo();
  204. $info['image_type'] = $uploadType;
  205. return $info;
  206. }
  207. }
  208. /**
  209. * TODO 获取小程序二维码是否生成
  210. * @param $url
  211. * @return array
  212. */
  213. public static function remoteImage($url)
  214. {
  215. $curl = curl_init();
  216. curl_setopt($curl, CURLOPT_URL, $url);
  217. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  218. $result = curl_exec($curl);
  219. $result = json_decode($result, true);
  220. if (is_array($result)) return ['status' => false, 'msg' => $result['errcode'] . '---' . $result['errmsg']];
  221. return ['status' => true];
  222. }
  223. /**
  224. * TODO 修改 https 和 http 移动到common
  225. * @param $url $url 域名
  226. * @param int $type 0 返回https 1 返回 http
  227. * @return string
  228. */
  229. public static function setHttpType($url, $type = 0)
  230. {
  231. $domainTop = substr($url, 0, 5);
  232. if ($type) {
  233. if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url));
  234. } else {
  235. if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url));
  236. }
  237. return $url;
  238. }
  239. /**
  240. * 获取二维码
  241. * @param $url
  242. * @param $name
  243. * @return array|bool|string
  244. */
  245. public static function getQRCodePath($url, $name)
  246. {
  247. if (!strlen(trim($url)) || !strlen(trim($name))) return false;
  248. try {
  249. $uploadType = sys_config('upload_type');
  250. //TODO 没有选择默认使用本地上传
  251. if (!$uploadType) $uploadType = 1;
  252. $uploadType = (int)$uploadType;
  253. $siteUrl = sys_config('site_url');
  254. if (!$siteUrl) return '请前往后台设置->系统设置->网站域名 填写您的域名格式为:http://域名';
  255. $info = [];
  256. $outfiles = Config::get('qrcode.cache_dir');
  257. $code = new QRcode();
  258. if (!file_exists($outfiles)) mkdir($outfiles, 0775, true);
  259. $wapCodePath = $code->png($url, $outfiles . '/' . $name)->getPath(); //获取二维码生成的地址
  260. $content = file_get_contents('.' . $wapCodePath);
  261. if ($uploadType === 1) {
  262. $info["code"] = 200;
  263. $info["name"] = $name;
  264. $info["dir"] = '/' . $outfiles . '/' . $name;
  265. $info["time"] = time();
  266. $info['size'] = 0;
  267. $info['type'] = 'image/png';
  268. $info["image_type"] = 1;
  269. $info['thumb_path'] = '/' . $outfiles . '/' . $name;
  270. return $info;
  271. } else {
  272. $upload = UploadService::init($uploadType);
  273. $res = $upload->to($outfiles)->validate()->setAuthThumb(false)->stream($content, $name);
  274. if ($res === false) {
  275. return $upload->getError();
  276. }
  277. $info = $upload->getUploadInfo();
  278. $info['image_type'] = $uploadType;
  279. return $info;
  280. }
  281. } catch (\Exception $e) {
  282. return $e->getMessage();
  283. }
  284. }
  285. /**分级返回下级所有分类ID
  286. * @param $data
  287. * @param string $children
  288. * @param string $field
  289. * @param string $pk
  290. * @return string
  291. */
  292. public static function getChildrenPid($data, $pid, $field = 'pid', $pk = 'id')
  293. {
  294. static $pids = '';
  295. foreach ($data as $k => $res) {
  296. if ($res[$field] == $pid) {
  297. $pids .= ',' . $res[$pk];
  298. self::getChildrenPid($data, $res[$pk], $field, $pk);
  299. }
  300. }
  301. return $pids;
  302. }
  303. }