ImageWaterMarkService.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace ln\services;
  3. use think\exception\ValidateException;
  4. class ImageWaterMarkService
  5. {
  6. public function run($in_img, $water_text = '商城入驻专用其它无效', $font_size = 30, $water_w = 300, $water_h = 450, $angle = -45)
  7. {
  8. if (!is_file($in_img))
  9. throw new ValidateException('图片不存在');
  10. $font = './public/font/simsunb.ttf';
  11. $info = getimagesize($in_img);
  12. //通过编号获取图像类型
  13. $type = image_type_to_extension($info[2], false);
  14. //在内存中创建和图像类型一样的图像
  15. $fun = "imagecreatefrom" . $type;
  16. //图片复制到内存
  17. $image = $fun($in_img);
  18. //设置字体颜色和透明度
  19. $color = imagecolorallocatealpha($image, 190, 190, 190, 0.3);
  20. $x_length = $info[0];
  21. $y_length = $info[1];
  22. //铺满屏幕
  23. for ($x = 10; $x < $x_length; $x) {
  24. for ($y = 20; $y < $y_length; $y) {
  25. imagettftext($image, $font_size, $angle, $x, $y, $color, $font, $water_text);
  26. $y += $water_h;
  27. }
  28. $x += $water_w;
  29. }
  30. //浏览器输出 保存图片的时候 需要去掉
  31. //header("Content-type:".$info['mime']);
  32. $fun = "image" . $type;
  33. // $fun($image);
  34. //保存图片
  35. $fun($image, $in_img);
  36. //销毁图片
  37. imagedestroy($image);
  38. }
  39. }