PosterServices.php 12 KB

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