Image.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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, 900);
  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. $this->mergeText($d, $d['content']);
  72. }
  73. }
  74. header("Content-Type:image/png");
  75. imagepng($this->target, $path . $file);
  76. imagedestroy($this->target);
  77. $params['poster_id'] = $poster['id'];
  78. $params['user_id'] = $member['id'];
  79. $params['image'] = $this->poster_url;
  80. $posterLog = new \app\admin\model\PosterLog();
  81. $posterLog->save($params);
  82. return $this->poster_url;
  83. }
  84. public function mergeImage($data, $imgurl)
  85. {
  86. $img = $this->createImage($imgurl);
  87. if (!$img) {
  88. return false;
  89. }
  90. $w = imagesx($img);
  91. $h = imagesy($img);
  92. imagecopyresized($this->target, $img, $data['left'], $data['top'], 0, 0, $data['width'], $data['height'], $w, $h);
  93. imagedestroy($img);
  94. }
  95. public function mergeText($data, $text)
  96. {
  97. // $font = ROOT_PATH . 'public/assets/fonts/SourceHanSansK-Regular.ttf';
  98. $font = ROOT_PATH . '/public/assets/fonts/fangzheng.ttf';
  99. if (!isset($data['color'])) {
  100. $data['color'] = '#000';
  101. }
  102. // $text = mb_convert_encoding($text, "GB2312", "UTF-8");
  103. // $text = iconv('gb2312','utf-8',$text);//解决乱码问题
  104. $colors = $this->hex2rgb($data['color']);
  105. $color = imagecolorallocate($this->target, $colors['red'], $colors['green'], $colors['blue']);
  106. imagettftext($this->target, $data['size'], 0, $data['left'], $data['top'] + $data['size'], $color, $font, $text);
  107. }
  108. public function createImage($imgurl)
  109. {
  110. if (strpos($imgurl, 'data:image') !== false) {
  111. $imgurl = '/assets/addons/poster/images/head.jpg';
  112. }
  113. if (strpos($imgurl, '://') === false) {
  114. $imgurl = ROOT_PATH . '/public' . $imgurl;
  115. if (!is_file($imgurl)) {
  116. return '';
  117. }
  118. }
  119. @$content = file_get_contents($imgurl);
  120. if ($content) {
  121. return imagecreatefromstring($content);
  122. }
  123. return '';
  124. }
  125. private function getRealData($data)
  126. {
  127. $data['left'] = isset($data['left']) ? intval(str_replace('px', '', $data['left'])) * 2 : 0;
  128. $data['top'] = isset($data['top']) ? intval(str_replace('px', '', $data['top'])) * 2 : 0;
  129. $data['width'] = isset($data['width']) ? intval(str_replace('px', '', $data['width'])) * 2 : 0;
  130. $data['height'] = isset($data['height']) ? intval(str_replace('px', '', $data['height'])) * 2 : 0;
  131. $data['size'] = isset($data['size']) ? intval(str_replace('px', '', $data['size'])) * 2 : 0;
  132. return $data;
  133. }
  134. /**
  135. * 递归创建文件夹
  136. * @author Created by Xing <464401240@qq.com>
  137. */
  138. private function mkdirs($path)
  139. {
  140. if (!is_dir($path)) {
  141. $this->mkdirs(dirname($path));
  142. mkdir($path);
  143. }
  144. return is_dir($path);
  145. }
  146. /**
  147. * hex转rgb
  148. * @param $colour
  149. * @return array|bool
  150. */
  151. private function hex2rgb($colour)
  152. {
  153. if ($colour[0] == '#') {
  154. $colour = substr($colour, 1);
  155. }
  156. if (strlen($colour) == 6) {
  157. list($r, $g, $b) = array($colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5]);
  158. } elseif (strlen($colour) == 3) {
  159. list($r, $g, $b) = array($colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2]);
  160. } else {
  161. return false;
  162. }
  163. $r = hexdec($r);
  164. $g = hexdec($g);
  165. $b = hexdec($b);
  166. return array('red' => $r, 'green' => $g, 'blue' => $b);
  167. }
  168. }