Image2.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace addons\poster\library;
  3. use think\Db;
  4. class Image2
  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 lizhen
  16. */
  17. public function createPosterImage($background,$qrcode_url,$text,$new_file_name)
  18. {
  19. $this->poster_url = $new_file_name;
  20. set_time_limit(0);
  21. @ini_set('memory_limit', '256M');
  22. $this->target = imagecreatetruecolor(723, 881); //背景图尺寸
  23. $white = imagecolorallocate($this->target, 255, 255, 255);
  24. imagefill($this->target, 0, 0, $white);
  25. $bg = $this->createImage($background);
  26. if (!empty($bg)) {
  27. $bgWidth = imagesx($bg);
  28. $bgHeight = imagesy($bg);
  29. $ratio = 640 / $bgWidth;
  30. $ratio = 1;
  31. $newWidth = imagesx($bg) * $ratio;
  32. $newHeight = imagesy($bg) * $ratio;
  33. $bgClone = imagecreatetruecolor($newWidth, $newHeight);
  34. imagecopyresampled($bgClone, $bg, 0, 0, 0, 0, $newWidth, $newHeight, $bgWidth, $bgHeight);
  35. imagecopy($this->target, $bgClone, 0, 0, 0, 0, $newWidth, $newHeight);
  36. imagedestroy($bg);
  37. imagedestroy($bgClone);
  38. }
  39. $poster = [
  40. [
  41. "left"=> "125px",
  42. "top"=> "300px",
  43. "type"=> "img",
  44. "width"=> "90px",
  45. "height"=> "90px",
  46. "src"=> $qrcode_url,
  47. ]
  48. ];
  49. //写字
  50. //$text = '一二三四五六七八九十一二三四五';
  51. /* $text_len = mb_strlen($text);
  52. if($text_len > 10){
  53. $poster[] = [
  54. "left"=> "100px",
  55. "top"=> "145px",
  56. "type"=> "nickname",
  57. "width"=> "80px",
  58. "height"=> "40px",
  59. "size"=> "14px",
  60. "color"=> "#FFFFFF",
  61. "text"=> mb_substr($text,0,10),
  62. ];
  63. $poster[] = [
  64. "left"=> "100px",
  65. "top"=> "175px",
  66. "type"=> "nickname",
  67. "width"=> "80px",
  68. "height"=> "40px",
  69. "size"=> "14px",
  70. "color"=> "#FFFFFF",
  71. "text"=> mb_substr($text,10,10),
  72. ];
  73. }else{
  74. $poster[] = [
  75. "left"=> "140px",
  76. "top"=> "220px",
  77. "type"=> "nickname",
  78. "width"=> "80px",
  79. "height"=> "40px",
  80. "size"=> "14px",
  81. "color"=> "#FF0000",
  82. "text"=> $text,
  83. ];
  84. }*/
  85. //写字
  86. foreach ($poster as $d) {
  87. $d = $this->getRealData($d);
  88. if ($d['type'] == 'img' && isset($d['src'])) {
  89. $this->mergeImage($d, $d['src']);
  90. } elseif ($d['type'] == 'nickname') {
  91. $this->mergeText($d, $d['text']);
  92. }
  93. }
  94. imagepng($this->target, $this->poster_url);
  95. imagedestroy($this->target);
  96. return $this->poster_url;
  97. }
  98. public function mergeImage($data, $imgurl)
  99. {
  100. $img = $this->createImage($imgurl);
  101. if (!$img) {
  102. return false;
  103. }
  104. $w = imagesx($img);
  105. $h = imagesy($img);
  106. imagecopyresized($this->target, $img, $data['left'], $data['top'], 0, 0, $data['width'], $data['height'], $w, $h);
  107. imagedestroy($img);
  108. }
  109. public function mergeText($data, $text)
  110. {
  111. $font = ROOT_PATH . '/public/assets/fonts/fangzheng.ttf';
  112. if (!isset($data['color'])) {
  113. $data['color'] = '#000';
  114. }
  115. $colors = $this->hex2rgb($data['color']);
  116. $color = imagecolorallocate($this->target, $colors['red'], $colors['green'], $colors['blue']);
  117. imagettftext($this->target, $data['size'], 0, $data['left'], $data['top'] + $data['size'], $color, $font, $text);
  118. }
  119. public function createImage($imgurl)
  120. {
  121. if (strpos($imgurl, 'data:image') !== false) {
  122. $imgurl = '/assets/addons/poster/images/head.jpg';
  123. }
  124. if (strpos($imgurl, '://') === false) {
  125. $imgurl = ROOT_PATH . '/public' . $imgurl;
  126. if (!is_file($imgurl)) {
  127. return '';
  128. }
  129. } else {
  130. $domain = 'thirdwx.qlogo.cn';
  131. if (strpos($imgurl, $domain) !== false) {
  132. $ip = gethostbyname($domain);
  133. if ($ip) {
  134. $imgurl = str_replace($domain, $ip, $imgurl);
  135. }
  136. }
  137. }
  138. @$content = file_get_contents($imgurl);
  139. if ($content) {
  140. return imagecreatefromstring($content);
  141. }
  142. return '';
  143. }
  144. private function getRealData($data)
  145. {
  146. $data['left'] = isset($data['left']) ? intval(str_replace('px', '', $data['left'])) * 2 : 0;
  147. $data['top'] = isset($data['top']) ? intval(str_replace('px', '', $data['top'])) * 2 : 0;
  148. $data['width'] = isset($data['width']) ? intval(str_replace('px', '', $data['width'])) * 2 : 0;
  149. $data['height'] = isset($data['height']) ? intval(str_replace('px', '', $data['height'])) * 2 : 0;
  150. $data['size'] = isset($data['size']) ? intval(str_replace('px', '', $data['size'])) * 2 : 0;
  151. return $data;
  152. }
  153. /**
  154. * 递归创建文件夹
  155. * @author Created by Xing <464401240@qq.com>
  156. */
  157. private function mkdirs($path)
  158. {
  159. if (!is_dir($path)) {
  160. $this->mkdirs(dirname($path));
  161. mkdir($path);
  162. }
  163. return is_dir($path);
  164. }
  165. /**
  166. * hex转rgb
  167. * @param $colour
  168. * @return array|bool
  169. */
  170. private function hex2rgb($colour)
  171. {
  172. if ($colour[0] == '#') {
  173. $colour = substr($colour, 1);
  174. }
  175. if (strlen($colour) == 6) {
  176. list($r, $g, $b) = array($colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5]);
  177. } elseif (strlen($colour) == 3) {
  178. list($r, $g, $b) = array($colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2]);
  179. } else {
  180. return false;
  181. }
  182. $r = hexdec($r);
  183. $g = hexdec($g);
  184. $b = hexdec($b);
  185. return array('red' => $r, 'green' => $g, 'blue' => $b);
  186. }
  187. }