Image.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace addons\poster\library;
  3. use think\Db;
  4. class Image
  5. {
  6. protected $target; //图像资源
  7. protected $poster_url; //海报url
  8. protected $model; //海报模型
  9. public function __construct()
  10. {
  11. $this->model = new \app\admin\model\Poster;
  12. }
  13. /**
  14. * 创建海报
  15. * @author Created by Xing <464401240@qq.com>
  16. */
  17. public function createPosterImage($poster, $member)
  18. {
  19. $path = $this->model->getDirs($poster['id']);
  20. if (!is_dir($path)) {
  21. $this->mkdirs($path);
  22. }
  23. $md5 = md5(json_encode(array(
  24. 'user_id' => $member['id'],
  25. 'bg' => $poster['bg_image'],
  26. 'data' => $poster['data'],
  27. 'poster_id' => $poster['id']
  28. )));
  29. $file = $md5 . '.png';
  30. $this->poster_url = $path . $file;
  31. if (is_file($this->poster_url)) {
  32. return $this->poster_url; //文件存在则直接返回
  33. }
  34. set_time_limit(0);
  35. @ini_set('memory_limit', '256M');
  36. $this->target = imagecreatetruecolor(640, 1008);
  37. $white = imagecolorallocate($this->target, 255, 255, 255);
  38. imagefill($this->target, 0, 0, $white);
  39. $bg = $this->createImage($poster['bg_image']);
  40. if (!empty($bg)) {
  41. $bgWidth = imagesx($bg);
  42. $bgHeight = imagesy($bg);
  43. $ratio = 640 / $bgWidth;
  44. $newWidth = imagesx($bg) * $ratio;
  45. $newHeight = imagesy($bg) * $ratio;
  46. $bgClone = imagecreatetruecolor($newWidth, $newHeight);
  47. imagecopyresampled($bgClone, $bg, 0, 0, 0, 0, $newWidth, $newHeight, $bgWidth, $bgHeight);
  48. imagecopy($this->target, $bgClone, 0, 0, 0, 0, $newWidth, $newHeight);
  49. imagedestroy($bg);
  50. imagedestroy($bgClone);
  51. }
  52. $data = json_decode(str_replace('&quot;', '\'', $poster['data']), true);
  53. foreach ($data as $d) {
  54. $d = $this->getRealData($d);
  55. if ($d['type'] == 'head') {
  56. $avatar = preg_replace('/\\/0$/i', '/96', $member['avatar']);
  57. $this->mergeImage($d, $avatar);
  58. } elseif ($d['type'] == 'img' && isset($d['src'])) {
  59. $this->mergeImage($d, $d['src']);
  60. } elseif ($d['type'] == 'qr' && isset($d['qr_table']) && isset($d['qr_relation']) && isset($d['qr_field'])) {
  61. $exist = Db::query('show tables like "' . $d['qr_table'] . '"');
  62. if ($exist) {
  63. $fields = Db::getConnection()->getFields($d['qr_table']); //传入数据表名称 $tablename
  64. if (isset($fields[$d['qr_relation']])) {
  65. $qrimg = Db::table($d['qr_table'])->where([$d['qr_relation'] => $member['id']])->value($d['qr_field']);
  66. $this->mergeImage($d, $qrimg);
  67. }
  68. }
  69. } elseif ($d['type'] == 'nickname') {
  70. $this->mergeText($d, $member['nickname']);
  71. }
  72. }
  73. imagepng($this->target, $path . $file);
  74. imagedestroy($this->target);
  75. $params['poster_id'] = $poster['id'];
  76. $params['user_id'] = $member['id'];
  77. $params['image'] = $this->poster_url;
  78. $posterLog = new \app\admin\model\PosterLog();
  79. $posterLog->save($params);
  80. return $this->poster_url;
  81. }
  82. public function mergeImage($data, $imgurl)
  83. {
  84. $img = $this->createImage($imgurl);
  85. if (!$img) {
  86. return false;
  87. }
  88. $w = imagesx($img);
  89. $h = imagesy($img);
  90. imagecopyresized($this->target, $img, $data['left'], $data['top'], 0, 0, $data['width'], $data['height'], $w, $h);
  91. imagedestroy($img);
  92. }
  93. public function mergeText($data, $text)
  94. {
  95. $font = ROOT_PATH . '/public/assets/fonts/SourceHanSansK-Regular.ttf';
  96. if (!isset($data['color'])) {
  97. $data['color'] = '#000';
  98. }
  99. $colors = $this->hex2rgb($data['color']);
  100. $color = imagecolorallocate($this->target, $colors['red'], $colors['green'], $colors['blue']);
  101. imagettftext($this->target, $data['size'], 0, $data['left'], $data['top'] + $data['size'], $color, $font, $text);
  102. }
  103. public function createImage($imgurl)
  104. {
  105. if (strpos($imgurl, 'data:image') !== false) {
  106. $imgurl = '/assets/addons/poster/images/head.jpg';
  107. }
  108. if (strpos($imgurl, '://') === false) {
  109. $imgurl = ROOT_PATH . '/public' . $imgurl;
  110. if (!is_file($imgurl)) {
  111. return '';
  112. }
  113. } else {
  114. $domain = 'thirdwx.qlogo.cn';
  115. if (strpos($imgurl, $domain) !== false) {
  116. $ip = gethostbyname($domain);
  117. if ($ip) {
  118. $imgurl = str_replace($domain, $ip, $imgurl);
  119. }
  120. }
  121. }
  122. @$content = file_get_contents($imgurl);
  123. if ($content) {
  124. return imagecreatefromstring($content);
  125. }
  126. return '';
  127. }
  128. private function getRealData($data)
  129. {
  130. $data['left'] = isset($data['left']) ? intval(str_replace('px', '', $data['left'])) * 2 : 0;
  131. $data['top'] = isset($data['top']) ? intval(str_replace('px', '', $data['top'])) * 2 : 0;
  132. $data['width'] = isset($data['width']) ? intval(str_replace('px', '', $data['width'])) * 2 : 0;
  133. $data['height'] = isset($data['height']) ? intval(str_replace('px', '', $data['height'])) * 2 : 0;
  134. $data['size'] = isset($data['size']) ? intval(str_replace('px', '', $data['size'])) * 2 : 0;
  135. return $data;
  136. }
  137. /**
  138. * 递归创建文件夹
  139. * @author Created by Xing <464401240@qq.com>
  140. */
  141. private function mkdirs($path)
  142. {
  143. if (!is_dir($path)) {
  144. $this->mkdirs(dirname($path));
  145. mkdir($path);
  146. }
  147. return is_dir($path);
  148. }
  149. /**
  150. * hex转rgb
  151. * @param $colour
  152. * @return array|bool
  153. */
  154. private function hex2rgb($colour)
  155. {
  156. if ($colour[0] == '#') {
  157. $colour = substr($colour, 1);
  158. }
  159. if (strlen($colour) == 6) {
  160. list($r, $g, $b) = array($colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5]);
  161. } elseif (strlen($colour) == 3) {
  162. list($r, $g, $b) = array($colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2]);
  163. } else {
  164. return false;
  165. }
  166. $r = hexdec($r);
  167. $g = hexdec($g);
  168. $b = hexdec($b);
  169. return array('red' => $r, 'green' => $g, 'blue' => $b);
  170. }
  171. }