CanvasService.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace service;
  12. use service\FileService;
  13. use app\routine\model\routine\RoutineCode;
  14. use Intervention\Image\AbstractFont;
  15. use Intervention\Image\ImageManagerStatic as ImageManager;
  16. class CanvasService
  17. {
  18. const FILELINK = 'uploads/';
  19. const BGIMG_PATH = 'public/';
  20. //背景图
  21. //const VOTEIMG = 'uploads/voteimg/voteimg.jpg';
  22. //字体
  23. //const BG_FONT = 'wap/first/zsff/font/fzcyjt.ttf';
  24. //const FONT = 'uploads/voteimg/simsunb.ttf';
  25. //const FONT_TWO = 'uploads/voteimg/fz-v4.0.ttf';
  26. //const BORDER = 'uploads/voteimg/border.png';
  27. protected static $canvas = null;
  28. protected static $image = null;
  29. protected static $backgroundWidth = 750;
  30. protected static $backgroundHeight = 1334;
  31. public static function CreatJpeg($file = '')
  32. {
  33. $file = $file ? $file : self::VOTEIMG;
  34. $imagesize = getimagesize($file);
  35. $type = image_type_to_extension($imagesize[2], true);
  36. switch ($type) {
  37. case '.png':
  38. $canvas = imagecreatefrompng($file);
  39. break;
  40. case '.jpeg':
  41. $canvas = imagecreatefromjpeg($file);
  42. break;
  43. case '.jpg':
  44. $canvas = imagecreatefromjpeg($file);
  45. break;
  46. case '.gif':
  47. $canvas = imagecreatefromgif($file);
  48. break;
  49. }
  50. return [$canvas, $imagesize];
  51. }
  52. public static function ReatetrueColor($w = 0, $h = 0)
  53. {
  54. return imagecreatetruecolor($w ? $w : self::$backgroundWidth, $h ? $h : self::$backgroundHeight);
  55. }
  56. public static function foundCode1($special, $url, $backgroundImg, $ext = 'poster_code_')
  57. {
  58. vendor('phpqrcode.phpqrcode');
  59. $bg_path = self::BGIMG_PATH;
  60. //底图尺寸大小
  61. $bg = ImageManager::make($bg_path . 'bgimg/bg.png')->resize(750, 950);
  62. $thumb = $backgroundImg;
  63. //海报尺寸大小
  64. if ($thumb) {
  65. $thumb = ImageManager::make($thumb)->resize(690, 590);
  66. //水印
  67. $font_path = ROOT_PATH . 'public/wap/first/zsff/font/';
  68. $title1_font_ttf = $font_path . 'PingFang-SC.ttf';
  69. $site_name = SystemConfigService::get('site_name');
  70. $thumb->text($site_name, 600, 570, function (AbstractFont $text) use ($title1_font_ttf) {
  71. $text->file($title1_font_ttf);
  72. $text->size(20);
  73. $text->color('#DDDDDD');
  74. $text->align('center');
  75. });
  76. $bg->insert($thumb, 'top-left', 30, 35);
  77. }
  78. $qrcodename = self::FILELINK . time() . 'qrcode.png';
  79. \QRcode::png($url, $qrcodename, 'L', 10, 2);
  80. //二维码位置
  81. if ($qrcodename) {
  82. $qr = ImageManager::make($qrcodename)->resize(150, 150);
  83. $bg->insert($qr, 'top-right', 45, 760);
  84. }
  85. //海报文字位置,活动分享标题
  86. $font_path = ROOT_PATH . 'public/wap/first/zsff/font/';
  87. $title1_font_ttf = $font_path . 'PingFang-SC.ttf';
  88. $bg->text($special['title'], 50, 750, function (AbstractFont $text) use ($title1_font_ttf) {
  89. $text->file($title1_font_ttf);
  90. $text->size(14);
  91. $text->color('#333333');
  92. $text->align('left');
  93. });
  94. $bg->text($special['title'], 50, 750, function (AbstractFont $text) use ($title1_font_ttf) {
  95. $text->file($title1_font_ttf);
  96. $text->size(14);
  97. $text->color('#333333');
  98. $text->align('left');
  99. });
  100. $bg->text($special['fake_sales'] . '人已学习', 625, 750, function (AbstractFont $text) use ($title1_font_ttf) {
  101. $text->file($title1_font_ttf);
  102. $text->size(14);
  103. $text->color('#666');
  104. $text->align('center');
  105. });
  106. $title2_font_ttf = $font_path . 'pingfang-sc-blod.ttf';
  107. $bg->text(date("d"), 49, 869, function (AbstractFont $text) use ($title2_font_ttf) {
  108. $text->file($title2_font_ttf);
  109. $text->size(72);
  110. $text->color('#f30606');
  111. $text->align('left');
  112. });
  113. $month = array(
  114. '01' => '一月',
  115. '02' => '二月',
  116. '03' => '三月',
  117. '04' => '四月',
  118. '05' => '五月',
  119. '06' => '六月',
  120. '07' => '七月',
  121. '08' => '八月',
  122. '09' => '九月',
  123. '10' => '十月',
  124. '11' => '十一月',
  125. '12' => '十二月'
  126. );
  127. $title3_font_ttf = $font_path . 'pingfang-sc-blod.ttf';
  128. $bg->text($month[date("m")], 128, 869, function (AbstractFont $text) use ($title3_font_ttf) {
  129. $text->file($title3_font_ttf);
  130. $text->size(21);
  131. $text->color('#000000');
  132. $text->align('left');
  133. });
  134. $week = date("w");
  135. if ($week == 0) {
  136. $week = 7;
  137. }
  138. $week_path = $bg_path . "bgimg/" . $week . '.png';
  139. $qr = ImageManager::make($week_path)->resize(114, 27);
  140. $bg->insert($qr, 'top-left', 53, 876);
  141. $filename = self::FILELINK . $ext . $special['id'] . ".png";
  142. $bg->save($filename);
  143. $FileService = new FileService();
  144. $FileService->unlink_file($qrcodename);
  145. return $filename;
  146. }
  147. /**签到海报
  148. * @param $special_id
  149. * @param $url
  150. * @param $backgroundImg
  151. * @param string $ext
  152. * @return string
  153. */
  154. public static function foundSignCode($uid, $url, $sign_info, $ext = 'poster_sign_')
  155. {
  156. vendor('phpqrcode.phpqrcode');
  157. $bg_path = self::BGIMG_PATH;
  158. //底图尺寸大小
  159. $bg = ImageManager::make($bg_path . 'bgimg/bg.png')->resize(750, 950);
  160. $thumb = $sign_info['poster'];
  161. //海报尺寸大小
  162. if ($thumb) {
  163. $thumb = ImageManager::make($thumb)->resize(690, 590);
  164. //水印
  165. $font_path = ROOT_PATH . 'public/wap/first/zsff/font/';
  166. $title1_font_ttf = $font_path . 'PingFang-SC.ttf';
  167. $site_name = SystemConfigService::get('site_name');
  168. $thumb->text($site_name, 600, 570, function (AbstractFont $text) use ($title1_font_ttf) {
  169. $text->file($title1_font_ttf);
  170. $text->size(20);
  171. $text->color('#DDDDDD');
  172. $text->align('center');
  173. });
  174. $bg->insert($thumb, 'top-left', 30, 35);
  175. }
  176. $qrcodename = self::FILELINK . $ext . time() . 'qrcode.png';
  177. \QRcode::png($url, $qrcodename, 'L', 10, 2);
  178. //二维码位置
  179. if ($qrcodename) {
  180. $qr = ImageManager::make($qrcodename)->resize(150, 150);
  181. $bg->insert($qr, 'top-right', 45, 760);
  182. }
  183. //海报文字位置,活动分享标题
  184. $font_path = ROOT_PATH . 'public/wap/first/zsff/font/';
  185. $title1_font_ttf = $font_path . 'PingFang-SC.ttf';
  186. $bg->text($sign_info['sign_talk'], 50, 750, function (AbstractFont $text) use ($title1_font_ttf) {
  187. $text->file($title1_font_ttf);
  188. $text->size(14);
  189. $text->color('#333333');
  190. $text->align('left');
  191. });
  192. $title2_font_ttf = $font_path . 'pingfang-sc-blod.ttf';
  193. $bg->text(date("d"), 49, 869, function (AbstractFont $text) use ($title2_font_ttf) {
  194. $text->file($title2_font_ttf);
  195. $text->size(72);
  196. $text->color('#f30606');
  197. $text->align('left');
  198. });
  199. $month = array(
  200. '01' => '一月',
  201. '02' => '二月',
  202. '03' => '三月',
  203. '04' => '四月',
  204. '05' => '五月',
  205. '06' => '六月',
  206. '07' => '七月',
  207. '08' => '八月',
  208. '09' => '九月',
  209. '10' => '十月',
  210. '11' => '十一月',
  211. '12' => '十二月'
  212. );
  213. $title3_font_ttf = $font_path . 'pingfang-sc-blod.ttf';
  214. $bg->text($month[date("m")], 128, 869, function (AbstractFont $text) use ($title3_font_ttf) {
  215. $text->file($title3_font_ttf);
  216. $text->size(21);
  217. $text->color('#000000');
  218. $text->align('left');
  219. });
  220. $week = date("w");
  221. if ($week == 0) {
  222. $week = 7;
  223. }
  224. $week_path = $bg_path . "bgimg/" . $week . '.png';
  225. $qr = ImageManager::make($week_path)->resize(114, 27);
  226. $bg->insert($qr, 'top-left', 53, 876);
  227. $filename = self::FILELINK . $ext . $uid . ".png";
  228. $bg->save($filename);
  229. $FileService = new FileService();
  230. $FileService->unlink_file($qrcodename);
  231. return $filename;
  232. }
  233. public static function startPosterSpeclialIng($special, $url, $uid)
  234. {
  235. vendor('phpqrcode.phpqrcode');
  236. $qrcodename = time() . '_show_qrcode.png';
  237. $bg_path = self::BGIMG_PATH;
  238. //底图尺寸大小
  239. $bg = ImageManager::make($bg_path . 'bgimg/group_poster.png')->resize(600, 723);
  240. $thumb = $special['poster_image'];
  241. $backg = ImageManager::make($bg_path . 'bgimg/backg.png')->resize(600, 553);
  242. $bg->insert($backg, 'top-left', 0, 0);
  243. //海报尺寸大小
  244. if ($thumb) {
  245. $thumb = ImageManager::make($thumb)->resize(540, 303);
  246. $bg->insert($thumb, 'top-left', 30, 30);
  247. }
  248. \QRcode::png($url, $qrcodename, 'L', 10, 0);
  249. //二维码位置
  250. if ($qrcodename) {
  251. $qr = ImageManager::make($qrcodename)->resize(126, 126);
  252. $bg->insert($qr, 'top-left', 64, 579);
  253. }
  254. //海报文字位置,活动分享标题
  255. $font_path = ROOT_PATH . 'public/wap/first/zsff/font/';
  256. $title1_font_ttf = $font_path . 'PingFang-SC.ttf';
  257. $bg->text('¥', 35, 400, function (AbstractFont $text) use ($title1_font_ttf) {
  258. $text->file($title1_font_ttf);
  259. $text->size(26);
  260. $text->color('#FEB720');
  261. $text->align('left');
  262. });
  263. $bg->text($special['pink_money'], 53, 400, function (AbstractFont $text) use ($title1_font_ttf) {
  264. $text->file($title1_font_ttf);
  265. $text->size(42);
  266. $text->color('#FEB720');
  267. $text->align('left');
  268. });
  269. $title = $special['title'];
  270. $len = mb_strlen($title, 'utf-8');
  271. if ($len > 18) {
  272. $bg->text(mb_substr($title, 0, 18, 'utf-8'), 35, 450, function (AbstractFont $text) use ($title1_font_ttf) {
  273. $text->file($title1_font_ttf);
  274. $text->size(28);
  275. $text->color('#282828');
  276. $text->align('left');
  277. });
  278. $bg->text(mb_substr($title, 19, 34, 'utf-8') . '...', 35, 500, function (AbstractFont $text) use ($title1_font_ttf) {
  279. $text->file($title1_font_ttf);
  280. $text->size(28);
  281. $text->color('#282828');
  282. $text->align('left');
  283. });
  284. } else {
  285. $bg->text($title, 35, 450, function (AbstractFont $text) use ($title1_font_ttf) {
  286. $text->file($title1_font_ttf);
  287. $text->size(28);
  288. $text->color('#282828');
  289. $text->align('left');
  290. });
  291. }
  292. $bg->text('邀您参与拼团课程', 226, 625, function (AbstractFont $text) use ($title1_font_ttf) {
  293. $text->file($title1_font_ttf);
  294. $text->size(22);
  295. $text->color('#999999');
  296. $text->align('left');
  297. });
  298. $bg->text('长按识别参与拼团', 226, 665, function (AbstractFont $text) use ($title1_font_ttf) {
  299. $text->file($title1_font_ttf);
  300. $text->size(22);
  301. $text->color('#999999');
  302. $text->align('left');
  303. });
  304. $filename = self::FILELINK . 'poster_' . $uid . '_' . $special['id'] . ".png";
  305. $bg->save($filename);
  306. $FileService = new FileService();
  307. $FileService->unlink_file($qrcodename);
  308. return $filename;
  309. }
  310. public static function startPosterIng($backgroundImgList, $uid)
  311. {
  312. if (!is_array($backgroundImgList)) return false;
  313. //放二维码
  314. $path = 'public/uploads/routine/' . $uid . '.jpg';
  315. if (!file_exists($path)) file_put_contents($path, RoutineCode::getCode($uid));
  316. $link = [];
  317. foreach ($backgroundImgList as $item) {
  318. if ($item['pic']) {
  319. $image = self::ReatetrueColor();
  320. //放背景
  321. list($canvas, $borderRes) = self::CreatJpeg(ROOT_PATH . $item['pic']);
  322. $color = imagecolorallocate($canvas, 0, 0, 0);
  323. imagefill($canvas, 0, 0, $color);
  324. imagecopyresampled($image, $canvas, 0, 0, 0, 0, imagesx($canvas), imagesy($canvas), imagesx($canvas), imagesy($canvas));
  325. list($code, $codeRes) = self::CreatJpeg($path);
  326. imagecopyresampled($image, $code, 170, 545, 0, 0, 220, 220, (int)$codeRes[0], (int)$codeRes[1]);
  327. $res = pathinfo($item['pic']);
  328. $save_file = self::FILELINK . $res['filename'] . 'haibao_.jpg';
  329. imagejpeg($image, $save_file, 70);
  330. imagedestroy($image);
  331. $link[] = $save_file;
  332. }
  333. }
  334. return $link;
  335. }
  336. public static function startPainting($userinfo = [], $merinfo = [], $mer_id)
  337. {
  338. $image = self::ReatetrueColor();
  339. //放背景
  340. list($canvas, $res) = self::CreatJpeg();
  341. $color = imagecolorallocate($canvas, 0, 0, 0);
  342. imagefill($canvas, 0, 0, $color);
  343. imagecopyresampled($image, $canvas, 0, 0, 0, 0, imagesx($canvas), imagesy($canvas), imagesx($canvas), imagesy($canvas));
  344. //放边框
  345. list($border, $borderRes) = self::CreatJpeg(self::BORDER);
  346. imagecopyresampled($image, $border, 10, 380, 0, 0, 450, 483, (int)$borderRes[0], (int)$borderRes[1]);
  347. //放二维码
  348. $path = 'public/uploads/routine/' . $userinfo['uid'] . '.jpg';
  349. if (!file_exists($path)) file_put_contents($path, RoutineCode::getCode($userinfo['uid']));
  350. list($code, $codeRes) = self::CreatJpeg($path);
  351. imagecopyresampled($image, $code, 150, 950, 0, 0, 180, 180, (int)$codeRes[0], (int)$codeRes[1]);
  352. //放头部
  353. list($heade, $headeRes) = self::CreatJpeg(self::FILELINK . 'heade.png');
  354. imagecopyresampled($image, $heade, 10, 50, 0, 0, 446, 281, (int)$headeRes[0], (int)$headeRes[1]);
  355. //放头像
  356. //下载头像为jpg头像
  357. if (!$userinfo['avatar_name'] && !file_exists($userinfo['avatar_name'])) {
  358. $avatar_name = time() . '.jpg';
  359. FileService::down_remote_file($userinfo['avatar'], self::FILELINK, $avatar_name);
  360. $link_image = self::FILELINK . 'user' . time() . '.png';
  361. self::CutChart(self::FILELINK . $avatar_name, $link_image);
  362. //删除刚下载的图片
  363. if (file_exists(self::FILELINK . $avatar_name)) unlink(self::FILELINK . $avatar_name);
  364. } else {
  365. $link_image = $userinfo['avatar_name'];
  366. }
  367. //放置头像
  368. list($avatar_c, $avatarRes) = self::CreatJpeg($link_image);
  369. imagecopyresampled($image, $avatar_c, 85, 58, 0, 0, 95, 95, (int)$avatarRes[0], (int)$avatarRes[1]);
  370. $text = [
  371. [
  372. 'fontSize' => 20,
  373. 'fontColor' => '231,180,52',
  374. 'left' => 70,
  375. 'top' => 200,
  376. 'text' => self::getUtf8Str($userinfo['nickname']),
  377. 'fontPath' => self::FONT,
  378. 'angle' => 0,
  379. ],
  380. [
  381. 'fontSize' => 19,
  382. 'fontColor' => '255,255,255',
  383. 'left' => 70,
  384. 'top' => 235,
  385. 'text' => self::getUtf8Str('吃货值:' . $userinfo['integral']),
  386. 'fontPath' => self::FONT,
  387. 'angle' => 0,
  388. ],
  389. [
  390. 'fontSize' => 19,
  391. 'fontColor' => '255,255,255',
  392. 'left' => 70,
  393. 'top' => 265,
  394. 'text' => self::getUtf8Str('吃货头衔:' . $userinfo['grade_name']),
  395. 'fontPath' => self::FONT,
  396. 'angle' => 0,
  397. ],
  398. [
  399. 'fontSize' => 19,
  400. 'fontColor' => '255,255,255',
  401. 'left' => 70,
  402. 'top' => 295,
  403. 'text' => self::getUtf8Str('吃货折扣:' . $userinfo['discount_num'] . '折'),
  404. 'fontPath' => self::FONT,
  405. 'angle' => 0,
  406. ],
  407. [
  408. 'fontSize' => 20,
  409. 'fontColor' => '255,255,255',
  410. 'left' => 20,
  411. 'top' => 800,
  412. 'text' => self::getUtf8Str($userinfo['mer_name']),
  413. 'fontPath' => self::FONT,
  414. 'angle' => 0,
  415. ],
  416. [
  417. 'fontSize' => 15,
  418. 'fontColor' => '255,255,255',
  419. 'left' => 20,
  420. 'top' => 838,
  421. 'text' => self::getUtf8Str($userinfo['details_address']),
  422. 'fontPath' => self::FONT,
  423. 'angle' => 0,
  424. ],
  425. [
  426. 'fontSize' => 15,
  427. 'fontColor' => '255,255,255',
  428. 'left' => 159,
  429. 'top' => 900,
  430. 'text' => self::getUtf8Str('(扫码获取吃货值)'),
  431. 'fontPath' => self::FONT,
  432. 'angle' => 0,
  433. ]
  434. ];
  435. $images = [
  436. [
  437. 'url' => isset($merinfo[0]) ? $merinfo[0] : '',
  438. 'left' => 15,
  439. 'top' => 390,
  440. 'right' => 0,
  441. 'stream' => 0,
  442. 'bottom' => 0,
  443. 'width' => 218,
  444. 'height' => 155,
  445. 'opacity' => 100,
  446. ],
  447. [
  448. 'url' => isset($merinfo[1]) ? $merinfo[1] : '',
  449. 'left' => 235,
  450. 'top' => 390,
  451. 'right' => 0,
  452. 'stream' => 0,
  453. 'bottom' => 0,
  454. 'width' => 218,
  455. 'height' => 155,
  456. 'opacity' => 100,
  457. ],
  458. [
  459. 'url' => isset($merinfo[2]) ? $merinfo[2] : '',
  460. 'left' => 15,
  461. 'top' => 548,
  462. 'right' => 0,
  463. 'stream' => 0,
  464. 'bottom' => 0,
  465. 'width' => 218,
  466. 'height' => 155,
  467. 'opacity' => 100,
  468. ],
  469. [
  470. 'url' => isset($merinfo[3]) ? $merinfo[3] : '',
  471. 'left' => 235,
  472. 'top' => 548,
  473. 'right' => 0,
  474. 'bottom' => 0,
  475. 'width' => 218,
  476. 'height' => 155,
  477. 'opacity' => 100,
  478. ]
  479. ];
  480. foreach ($images as $item) {
  481. if ($item['url']) {
  482. list($mer, $Res) = self::CreatJpeg(ROOT_PATH . $item['url']);
  483. if ($mer && $Res) imagecopyresampled($image, $mer, $item['left'], $item['top'], $item['right'], $item['bottom'], $item['width'], $item['height'], (int)$Res[0], (int)$Res[1]);
  484. }
  485. }
  486. foreach ($text as $key => $val) {
  487. list($R, $G, $B) = explode(',', $val['fontColor']);
  488. $fontColor = imagecolorallocate($image, $R, $G, $B);
  489. $val['left'] = $val['left'] < 0 ? self::$backgroundWidth - abs($val['left']) : $val['left'];
  490. $val['top'] = $val['top'] < 0 ? self::$backgroundHeight - abs($val['top']) : $val['top'];
  491. imagettftext($image, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
  492. }
  493. $save_file = self::FILELINK . 'haibao_' . time() . '.jpg';
  494. imagejpeg($image, $save_file, 70);
  495. imagedestroy($image);
  496. return ['file' => $save_file, 'avatar_name' => $link_image];
  497. }
  498. public static function getUtf8Str($str, $utf8len = 28, $chaet = 'UTF-8', $file = '...')
  499. {
  500. if (mb_strlen($str, $chaet) > $utf8len) {
  501. $str = mb_substr($str, 0, $utf8len, $chaet) . $file;
  502. }
  503. return $str;
  504. }
  505. public static function CutChart($imgpath, $savefilename)
  506. {
  507. $ext = pathinfo($imgpath);
  508. $src_img = null;
  509. switch ($ext['extension']) {
  510. case 'jpg':
  511. $src_img = imagecreatefromjpeg($imgpath);
  512. break;
  513. case 'png':
  514. $src_img = imagecreatefrompng($imgpath);
  515. break;
  516. case 'gif':
  517. $src_img = imagecreatefromgif($imgpath);
  518. break;
  519. }
  520. $wh = getimagesize($imgpath);
  521. $w = $wh[0];
  522. $h = $wh[1];
  523. $w = min($w, $h);
  524. $h = $w;
  525. $img = imagecreatetruecolor($w, $h);
  526. //这一句一定要有
  527. imagesavealpha($img, true);
  528. //拾取一个完全透明的颜色,最后一个参数127为全透明
  529. $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
  530. imagefill($img, 0, 0, $bg);
  531. $r = $w / 2; //圆半径
  532. $y_x = $r; //圆心X坐标
  533. $y_y = $r; //圆心Y坐标
  534. for ($x = 0; $x < $w; $x++) {
  535. for ($y = 0; $y < $h; $y++) {
  536. $rgbColor = imagecolorat($src_img, $x, $y);
  537. if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
  538. imagesetpixel($img, $x, $y, $rgbColor);
  539. }
  540. }
  541. }
  542. imagepng($img, $savefilename, 0);
  543. imagedestroy($img);
  544. }
  545. /*
  546. * 生成画笔
  547. * */
  548. public static function brush($one, $two, $three)
  549. {
  550. if (self::$canvas === null) return false;
  551. return imagecolorallocate(self::$canvas, $one, $two, $three);
  552. }
  553. /*
  554. * 粉丝介绍字体拆分为数组排列
  555. * */
  556. public static function getContentArray($str, $len = 4, $strlen = 24, $chaet = 'UTF-8')
  557. {
  558. $array = [];
  559. for ($i = 0; $i < $len; $i++) {
  560. array_push($array, '');
  561. }
  562. if (mb_strlen($str, $chaet) > $strlen) {
  563. for ($i = 0; $i < $len; $i++) {
  564. $lin = mb_substr($str, $i * $strlen, $strlen, $chaet);
  565. if ($lin) $array[$i] = $lin;
  566. }
  567. } else {
  568. $array[0] = $str;
  569. }
  570. return $array;
  571. }
  572. }