ImageWaterMarkService.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use think\exception\ValidateException;
  13. class ImageWaterMarkService
  14. {
  15. public function run($in_img, $water_text = '商城入驻专用其它无效', $font_size = 30, $water_w = 300, $water_h = 450, $angle = -45)
  16. {
  17. if (!is_file($in_img))
  18. throw new ValidateException('图片不存在');
  19. $font = public_path() . 'font/simsunb.ttf';
  20. $info = getimagesize($in_img);
  21. //通过编号获取图像类型
  22. $type = image_type_to_extension($info[2], false);
  23. //在内存中创建和图像类型一样的图像
  24. $fun = "imagecreatefrom" . $type;
  25. //图片复制到内存
  26. $image = $fun($in_img);
  27. //设置字体颜色和透明度
  28. $color = imagecolorallocatealpha($image, 190, 190, 190, 0.3);
  29. $x_length = $info[0];
  30. $y_length = $info[1];
  31. //铺满屏幕
  32. for ($x = 10; $x < $x_length; $x) {
  33. for ($y = 20; $y < $y_length; $y) {
  34. imagettftext($image, $font_size, $angle, $x, $y, $color, $font, $water_text);
  35. $y += $water_h;
  36. }
  37. $x += $water_w;
  38. }
  39. //浏览器输出 保存图片的时候 需要去掉
  40. //header("Content-type:".$info['mime']);
  41. $fun = "image" . $type;
  42. // $fun($image);
  43. //保存图片
  44. $fun($image, $in_img);
  45. //销毁图片
  46. imagedestroy($image);
  47. }
  48. }