Image.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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, $waterText3 = '')
  104. {
  105. // 创建图片资源
  106. $image_info = getimagesize($filePath);
  107. $image_type = $image_info[2];
  108. if ($image_type == IMAGETYPE_JPEG) {
  109. $image = imagecreatefromjpeg($filePath);
  110. // 处理JPEG图片的逻辑
  111. } elseif ($image_type == IMAGETYPE_PNG) {
  112. $image = imagecreatefrompng($filePath);
  113. // 处理PNG图片的逻辑
  114. }
  115. if (!$image) {
  116. return false;
  117. }
  118. // 获取图片尺寸
  119. $width = imagesx($image);
  120. $height = imagesy($image);
  121. $color = '#CCCCCC';
  122. // 设置水印文字的颜色
  123. [$r, $g, $b] = sscanf($color, "#%02x%02x%02x");
  124. $alpha = number_format($alpha / 100 * 127, 2);
  125. $color = imagecolorallocatealpha($image, $r, $g, $b, $alpha);
  126. // //获取文字数量
  127. $fontNum = mb_strlen($waterText);
  128. if ($waterText2) {
  129. $fontNum2 = mb_strlen($waterText2);
  130. if ($fontNum2 > $fontNum) {
  131. $fontNum = $fontNum2;
  132. }
  133. }
  134. if ($waterText3) {
  135. $fontNum3 = mb_strlen($waterText3);
  136. if ($fontNum3 > $fontNum) {
  137. $fontNum = $fontNum3;
  138. }
  139. }
  140. $fontPath = Grafika::fontsDir().DS.'st-heiti-light.ttc';
  141. // 3.创建文字图片
  142. //判断是否需要添加水印
  143. if ($type) {
  144. //获取斜边长度
  145. $hypo = hypot($width, $height);
  146. // 判断文字图片开始的位置
  147. $fontSize = intval($hypo) / $fontNum * 0.5;
  148. // 获取文字大小宽高
  149. $fontSizeArr = $this->get_text_box($waterText, $fontSize, $fontPath);
  150. $fontX = intval(($width * (($hypo - $fontSizeArr[0]) / 2)) / $hypo);
  151. $fontY = $height - intval(($height * (($hypo - $fontSizeArr[0]) / 2)) / $hypo);
  152. //计算倾斜角度
  153. $angle = intval(atan($height / $width) * 180 / pi());
  154. } else {
  155. // 判断文字图片开始的位置
  156. $fontSize = $width / $fontNum * 0.5;
  157. $fontX = 0;
  158. $fontY = 0;
  159. $angle = 0;
  160. }
  161. // 添加水印文字 // imagettftext() 的参数依次是:图片资源, 字体大小, 旋转角度, X坐标, Y坐标, 颜色, 字体文件, 文本内容
  162. imagettftext($image, $fontSize, $angle, $fontX, $fontY, $color, $fontPath, $waterText);
  163. if ($waterText2) {
  164. imagettftext($image, $fontSize, $angle, $fontX + 1 * $fontSize, $fontY + 1.5 * $fontSize, $color, $fontPath, $waterText2);
  165. }
  166. if ($waterText3) {
  167. imagettftext($image, $fontSize, $angle, $fontX - 1 * $fontSize, $fontY - 1.5 * $fontSize, $color, $fontPath, $waterText3);
  168. }
  169. // 保存带有水印的图片
  170. imagejpeg($image, $filePath, 90);
  171. // 释放图片资源
  172. imagedestroy($image);
  173. return true;
  174. }
  175. }