Gd.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php namespace Image\Lib\Tool;
  2. use Dever;
  3. class Gd extends Core
  4. {
  5. # 缩放
  6. public function thumb($set)
  7. {
  8. if (!$this->im) {
  9. return false;
  10. }
  11. $result = array();
  12. if (!is_array($set)) {
  13. $set = explode(',', $set);
  14. }
  15. $source_x = imagesx($this->im);
  16. $source_y = imagesy($this->im);
  17. $source_w = $source_x/$source_y;
  18. $source_h = $source_y/$source_x;
  19. foreach ($set as $k => $v) {
  20. $result[$k] = $this->getDest($v . '_thumb');
  21. if ($this->check($result[$k])) {
  22. list($dest_x, $dest_y, $offset) = $this->getXy($v, $source_x, $source_y, $source_w, $source_h);
  23. $im = $this->copy($this->im, $dest_x, $dest_y, $source_x, $source_y, 0, 0, false, 1);
  24. imagejpeg($im, $result[$k]);
  25. $this->destroy($im);
  26. }
  27. }
  28. return $result;
  29. }
  30. # 裁剪
  31. public function crop($set, $position)
  32. {
  33. if (!$this->im) {
  34. return false;
  35. }
  36. $result = array();
  37. if (!is_array($set)) {
  38. $set = explode(',', $set);
  39. }
  40. $source_x = imagesx($this->im);
  41. $source_y = imagesy($this->im);
  42. foreach ($set as $k => $v) {
  43. $result[$k] = $this->getDest($v . '_crop');
  44. if ($this->check($result[$k])) {
  45. $x = 0;
  46. $y = 0;
  47. $offset = explode('_', $v);
  48. if (isset($offset[2]) && $offset[2]) {
  49. $offset[0] += $offset[2];
  50. $offset[1] += $offset[2];
  51. }
  52. if ($position) {
  53. if (!is_array($position)) {
  54. list($x, $y) = $this->position($source_x, $source_y, $offset[0], $offset[1], $position);
  55. } else {
  56. # 加入根据百分比计算裁图
  57. if ($position[0] <= 0) {
  58. $position[0] = $source_x/2 - $offset[0]/2;
  59. } elseif (strstr($position[0], '%')) {
  60. $position[0] = $source_x * intval(str_replace('%', '', $position[0]))/100;
  61. }
  62. if ($position[1] <= 0) {
  63. $position[1] = $source_y/2 - $offset[1]/2;
  64. } elseif (strstr($position[1], '%')) {
  65. $position[1] = $source_y * intval(str_replace('%', '', $position[1]))/100;
  66. }
  67. $x = $position[0];
  68. $y = $position[1];
  69. }
  70. } else {
  71. $x = $source_x/2 - $offset[0]/2;
  72. $y = $source_y/2 - $offset[1]/2;
  73. }
  74. if ($x < 0) {
  75. $x = 0;
  76. }
  77. if ($y < 0) {
  78. $y = 0;
  79. }
  80. $im = $this->copy($this->im, $offset[0], $offset[1], $offset[0], $offset[1], $x, $y);
  81. imagejpeg($im, $result[$k]);
  82. $this->destroy($im);
  83. }
  84. }
  85. return $result;
  86. }
  87. # 图片水印
  88. public function pic($water, $position = 5, $offset = 0, $width = 0, $height = 0, $radius = 0)
  89. {
  90. if (!$this->im) {
  91. return false;
  92. }
  93. $result = $this->getDest('mark');
  94. if ($this->check($result)) {
  95. if ($radius) {
  96. $water = $this->get_radius($water, $radius);
  97. } else {
  98. $water = $this->get($water);
  99. }
  100. $source_x = imagesx($this->im);
  101. $source_y = imagesy($this->im);
  102. $water_x = imagesx($water);
  103. $water_y = imagesy($water);
  104. if ($width || $height) {
  105. $water_w = $water_x/$water_y;
  106. $water_h = $water_y/$water_x;
  107. if ($water_x > $width) {
  108. $dest_x = $width;
  109. $dest_y = $width*$water_h;
  110. } elseif ($height > 0 && $water_y > $height) {
  111. $dest_x = $height*$water_w;
  112. $dest_y = $height;
  113. } else {
  114. $dest_x = $water_x;
  115. $dest_y = $water_y;
  116. }
  117. $water = $this->copy($water, $dest_x, $dest_y, $water_x, $water_y, 0, 0, false, 2);
  118. $xy = $this->position($source_x, $source_y, $dest_x, $dest_y, $position, $offset);
  119. $water_x = $dest_x;
  120. $water_y = $dest_y;
  121. } else {
  122. $xy = $this->position($source_x, $source_y, $water_x, $water_y, $position, $offset);
  123. }
  124. if ($xy[0] == -1) {
  125. # 水印平铺 gd的先不做了
  126. $xy[0] = $xy[1] = 0;
  127. }
  128. if ($xy[2] == false) {
  129. $this->destroy($water);
  130. return;
  131. }
  132. $im = $this->copy($water, $water_x, $water_y, 0, 0, $xy[0], $xy[1], $this->im);
  133. imagejpeg($im, $result);
  134. $this->destroy($water);
  135. }
  136. return $result;
  137. }
  138. # 文字水印
  139. public function txt($name, $position = 5, $offset = 0, $size = 10, $color = '', $angle = 0, $width = 0, $font = 'SIMSUN.TTC')
  140. {
  141. if (!$this->im) {
  142. return false;
  143. }
  144. $result = $this->getDest('txt');
  145. if ($this->check($result)) {
  146. $autowrap = 0;
  147. if ($width > 0) {
  148. $name = $this->autowrap($autowrap, $size, $angle, $font, $name, $width);
  149. }
  150. $position = imagettfbbox($size, $angle, $font, $name);
  151. if ($position) {
  152. $source_x = imagesx($this->im);
  153. $source_y = imagesy($this->im);
  154. $water_x = $position[2] - $position[0];
  155. $water_y = $position[1] - $position[7];
  156. $xy = $this->position($source_x, $source_y, $water_x, $water_y, $position, $offset);
  157. }
  158. if ($color && (strlen($color)==7)) {
  159. $left = $xy[0] ?? 0;
  160. $top = $xy[1] ?? 0;
  161. $R = hexdec(substr($color,1,2));
  162. $G = hexdec(substr($color,3,2));
  163. $B = hexdec(substr($color,5));
  164. putenv('GDFONTPATH=' . realpath('.'));
  165. imagettftext($this->im, $size, $angle, $left, $top + $autowrap, imagecolorallocate($this->im, $R, $G, $B), $font, $name);
  166. }
  167. imagejpeg($this->im, $result);
  168. }
  169. return $result;
  170. }
  171. protected function copy($im, $w, $h, $x, $y, $l, $t, $dim = false, $ti = 1)
  172. {
  173. if ($dim == false) {
  174. $dim = $this->create($w, $h, $ti);
  175. imagecopyresized($dim, $im, 0, 0, $l, $t, $w, $h, $x, $y);
  176. } else {
  177. imagecopy($dim, $im, $l, $t, 0, 0, $w, $h);
  178. //imagecopyresampled($dim, $im, $l,$t, 0, 0, $w, $h, $x, $y);
  179. }
  180. return $dim;
  181. }
  182. protected function create($w, $h, $t = 1)
  183. {
  184. $im = imagecreatetruecolor($w,$h);
  185. if ($t == 1) {
  186. # 空白背景
  187. $wite = ImageColorAllocate($im, 255,255,255);
  188. imagefilledrectangle($im, 0, 0, $w, $h, $wite);
  189. imagefilledrectangle($im, $w, $h, 0,0, $wite);
  190. ImageColorTransparent($im, $wite);
  191. } elseif ($t == 2) {
  192. # 透明背景
  193. imagealphablending($im, false);
  194. imagesavealpha($im,true);
  195. $transparent = imagecolorallocatealpha($im, 255, 255, 255, 127);
  196. imagefilledrectangle($im, 0, 0, $w, $h, $transparent);
  197. }
  198. return $im;
  199. }
  200. protected function get($im)
  201. {
  202. $im = file_get_contents($im);
  203. $im = imagecreatefromstring($im);
  204. return $im;
  205. }
  206. # 圆角图片
  207. private function get_radius($imgpath = '', $radius = 0)
  208. {
  209. $ext = pathinfo($imgpath);
  210. $src_img = null;
  211. switch ($ext['extension']) {
  212. case 'jpg':
  213. $src_img = imagecreatefromjpeg($imgpath);
  214. break;
  215. case 'png':
  216. $src_img = imagecreatefrompng($imgpath);
  217. break;
  218. }
  219. $wh = getimagesize($imgpath);
  220. $w = $wh[0];
  221. $h = $wh[1];
  222. $radius = $radius <= 0 ? (min($w, $h) / 2) : $radius;
  223. $img = imagecreatetruecolor($w, $h);
  224. //这一句一定要有
  225. imagesavealpha($img, true);
  226. //拾取一个完全透明的颜色,最后一个参数127为全透明
  227. $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
  228. imagefill($img, 0, 0, $bg);
  229. $r = $radius; //圆 角半径
  230. for ($x = 0; $x < $w; $x++) {
  231. for ($y = 0; $y < $h; $y++) {
  232. $rgbColor = imagecolorat($src_img, $x, $y);
  233. if (($x >= $radius && $x <= ($w - $radius)) || ($y >= $radius && $y <= ($h - $radius))) {
  234. //不在四角的范围内,直接画
  235. imagesetpixel($img, $x, $y, $rgbColor);
  236. } else {
  237. //在四角的范围内选择画
  238. //上左
  239. $y_x = $r; //圆心X坐标
  240. $y_y = $r; //圆心Y坐标
  241. if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
  242. imagesetpixel($img, $x, $y, $rgbColor);
  243. }
  244. //上右
  245. $y_x = $w - $r; //圆心X坐标
  246. $y_y = $r; //圆心Y坐标
  247. if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
  248. imagesetpixel($img, $x, $y, $rgbColor);
  249. }
  250. //下左
  251. $y_x = $r; //圆心X坐标
  252. $y_y = $h - $r; //圆心Y坐标
  253. if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
  254. imagesetpixel($img, $x, $y, $rgbColor);
  255. }
  256. //下右
  257. $y_x = $w - $r; //圆心X坐标
  258. $y_y = $h - $r; //圆心Y坐标
  259. if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
  260. imagesetpixel($img, $x, $y, $rgbColor);
  261. }
  262. }
  263. }
  264. }
  265. return $img;
  266. }
  267. protected function _destroy()
  268. {
  269. imagedestroy($this->im);
  270. $this->im = false;
  271. }
  272. }