Image.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 * 0.5;
  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. // //设置文字颜色
  62. // $Color = new Color('#CCCCCC');
  63. // $editor->text($fontImage, $waterText, $fontSize, $fontX, $fontY, $Color, $fontPath, $angle);
  64. // if ($waterText2) {
  65. // $editor->text($fontImage, $waterText2, $fontSize, $fontX + $fontSize, $fontY + $fontSize, $Color, $fontPath, $angle);
  66. // }
  67. //
  68. // //合并图片
  69. // $editor->blend($backdropImage, $fontImage, 'overlay', 1, 'center');
  70. // // 保存图片
  71. // $editor->save($backdropImage, $filePath);
  72. // return true;
  73. // }
  74. /**
  75. * [get_text_box]
  76. * 根据文字获取宽高
  77. *
  78. * @Author: Yeshihao
  79. * @DateTime: 2023/11/14 10:04
  80. * @param $text
  81. * @param $size
  82. * @param $font
  83. * @return array
  84. */
  85. private function get_text_box($text, $size, $font)
  86. {
  87. $point = imagettfbbox($size, 0, $font, $text);
  88. $width = $point[4] - $point[6];
  89. $height = $point[1] - $point[7];
  90. return [$width, $height];
  91. }
  92. /**
  93. * [waterMark]
  94. *
  95. * @Author: Yeshihao
  96. * @DateTime: 2023/11/14 14:45
  97. * @param $filePath //文件路径
  98. * @param $waterText //水印文字
  99. * @param int $type 0不倾斜 1倾斜
  100. * @return bool
  101. * @throws
  102. */
  103. public function waterMark($filePath, $waterText, $alpha = 30, $type = 0, $waterText2 = '', $width = 0, $height = 0)
  104. {
  105. // 创建图片资源
  106. $image = imagecreatefromjpeg($filePath);
  107. if (!$image) {
  108. return false;
  109. }
  110. // 获取图片尺寸
  111. $width = imagesx($image);
  112. $height = imagesy($image);
  113. $color = '#CCCCCC';
  114. // 设置水印文字的颜色
  115. [$r, $g, $b] = sscanf($color, "#%02x%02x%02x");
  116. $alpha = number_format($alpha / 100 * 127, 2);
  117. $color = imagecolorallocatealpha($image, $r, $g, $b, $alpha);
  118. // //获取文字数量
  119. $fontNum = mb_strlen($waterText);
  120. $fontPath = Grafika::fontsDir().DS.'st-heiti-light.ttc';
  121. // 3.创建文字图片
  122. //判断是否需要添加水印
  123. if ($type) {
  124. //获取斜边长度
  125. $hypo = hypot($width, $height);
  126. // 判断文字图片开始的位置
  127. $fontSize = intval($hypo) / $fontNum * 0.5;
  128. // 获取文字大小宽高
  129. $fontSizeArr = $this->get_text_box($waterText, $fontSize, $fontPath);
  130. $fontX = intval(($width * (($hypo - $fontSizeArr[0]) / 2)) / $hypo);
  131. $fontY = $height - intval(($height * (($hypo - $fontSizeArr[0]) / 2)) / $hypo);
  132. //计算倾斜角度
  133. $angle = intval(atan($height / $width) * 180 / pi());
  134. } else {
  135. // 判断文字图片开始的位置
  136. $fontSize = $width / $fontNum * 0.5;
  137. $fontX = 0;
  138. $fontY = 0;
  139. $angle = 0;
  140. }
  141. // 添加水印文字 // imagettftext() 的参数依次是:图片资源, 字体大小, 旋转角度, X坐标, Y坐标, 颜色, 字体文件, 文本内容
  142. imagettftext($image, $fontSize, $angle, $fontX, $fontY, $color, $fontPath, $waterText);
  143. imagettftext($image, $fontSize, $angle, $fontX + $fontSize, $fontY + $fontSize, $color, $fontPath, $waterText2);
  144. // 保存带有水印的图片
  145. imagejpeg($image, $filePath, 90);
  146. // 释放图片资源
  147. imagedestroy($image);
  148. return true;
  149. }
  150. }