Image.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\common\service\watermark\image;
  3. use Grafika\Color;
  4. use Grafika\Grafika;
  5. use Grafika\ImageType;
  6. class Image
  7. {
  8. /**
  9. * [waterMark]
  10. *
  11. * @Author: Yeshihao
  12. * @DateTime: 2023/11/14 14:45
  13. * @param $filePath //文件路径
  14. * @param $waterText //水印文字
  15. * @param int $type 0不倾斜 1倾斜
  16. * @return bool
  17. * @throws
  18. */
  19. public function waterMark($filePath, $waterText, $type = 0, $waterText2 = '', $width = 0, $height = 0)
  20. {
  21. // 1 打开海报背景图
  22. $editor = Grafika::createEditor(['Gd']);
  23. $editor->open($backdropImage, $filePath);
  24. // 获取背景图宽高
  25. $size = getimagesize($filePath);
  26. //获取文字类型路劲
  27. $fontPath = Grafika::fontsDir().DS.'st-heiti-light.ttc';
  28. //获取文字数量
  29. $fontNum = mb_strlen($waterText);
  30. // 3.创建文字图片
  31. //判断是否需要添加水印
  32. if ($type) {
  33. //获取斜边长度
  34. $hypo = hypot($size[0], $size[1]);
  35. // 判断文字图片开始的位置
  36. $fontSize = intval($hypo) / $fontNum * 0.5;
  37. // 获取文字大小宽高
  38. $fontSizeArr = $this->get_text_box($waterText, $fontSize, $fontPath);
  39. $fontX = intval(($size[0] * (($hypo - $fontSizeArr[0]) / 2)) / $hypo);
  40. $fontY = $size[1] - intval(($size[1] * (($hypo - $fontSizeArr[0]) / 2)) / $hypo);
  41. //计算倾斜角度
  42. $angle = intval(atan($size[1] / $size[0]) * 180 / pi());
  43. $image = imagecreatetruecolor($size[0], $size[1]);
  44. } else {
  45. // 判断文字图片开始的位置
  46. $fontSize = $size[0] / $fontNum;
  47. // 获取文字大小宽高
  48. $fontSizeArr = $this->get_text_box($waterText, $fontSize, $fontPath);
  49. $fontX = 0;
  50. $fontY = 0;
  51. $angle = 0;
  52. $image = imagecreatetruecolor($fontSizeArr[0], $fontSizeArr[1]);
  53. }
  54. imagesavealpha($image, true);
  55. // 设置透明度
  56. $frontImageSource = imagecolorallocatealpha($image, 0, 0, 0, 127);
  57. imagefill($image, 0, 0, $frontImageSource);
  58. //生成文字透明背景板
  59. $fontImage = new \Grafika\Gd\Image($image, '', imagesx($image), imagesy($image), ImageType::PNG);
  60. //设置文字颜色
  61. $Color = new Color('#cccccc');
  62. $editor->text($fontImage, $waterText, $fontSize, $fontX, $fontY, $Color, $fontPath, $angle);
  63. if ($waterText2) {
  64. $editor->text($fontImage, $waterText2, $fontSize, $fontX + $fontSize, $fontY + $fontSize, $Color, $fontPath, $angle);
  65. }
  66. //合并图片
  67. $editor->blend($backdropImage, $fontImage, 'overlay', 1, 'center');
  68. // 保存图片
  69. $editor->save($backdropImage, $filePath);
  70. return true;
  71. }
  72. /**
  73. * [get_text_box]
  74. * 根据文字获取宽高
  75. *
  76. * @Author: Yeshihao
  77. * @DateTime: 2023/11/14 10:04
  78. * @param $text
  79. * @param $size
  80. * @param $font
  81. * @return array
  82. */
  83. private function get_text_box($text, $size, $font)
  84. {
  85. $point = imagettfbbox($size, 0, $font, $text);
  86. $width = $point[4] - $point[6];
  87. $height = $point[1] - $point[7];
  88. return [$width, $height];
  89. }
  90. }