ImageWaterMarkService.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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
  16. = -10)
  17. {
  18. if (!is_file($in_img))
  19. throw new ValidateException('图片不存在');
  20. $font = public_path() . 'font/simsunb.ttf';
  21. $info = getimagesize($in_img);
  22. //通过编号获取图像类型
  23. $type = image_type_to_extension($info[2], false);
  24. //在内存中创建和图像类型一样的图像
  25. $fun = "imagecreatefrom" . $type;
  26. //图片复制到内存
  27. $image = $fun($in_img);
  28. //设置字体颜色和透明度
  29. $color = imagecolorallocatealpha($image, 190, 190, 190, 50);
  30. $x_length = $info[0];
  31. $y_length = $info[1];
  32. //铺满屏幕
  33. //for ($x = 10; $x < $x_length; $x) {
  34. // for ($y = 20; $y < $y_length; $y) {
  35. imagettftext($image, $font_size, $angle, $x_length/3, $y_length/2, $color, $font, $water_text);
  36. // $y += $water_h;
  37. //}
  38. //$x += $water_w;
  39. //}
  40. //浏览器输出 保存图片的时候 需要去掉
  41. //header("Content-type:".$info['mime']);
  42. $fun = "image" . $type;
  43. // $fun($image);
  44. //保存图片
  45. $fun($image, $in_img);
  46. //销毁图片
  47. imagedestroy($image);
  48. }
  49. }