WaterMarkBase.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace app\common\service\watermark;
  3. use app\common\service\watermark\image\Image;
  4. use app\common\service\watermark\pdf\Pdf;
  5. /**
  6. * Class WaterMarkBase
  7. * 生成水印
  8. *
  9. * @Author: Yeshihao
  10. * @DateTime: 2024/11/27 14:03
  11. * @package app\common\service\watermark
  12. */
  13. class WaterMarkBase
  14. {
  15. /**
  16. * @var string
  17. */
  18. public $error = '';
  19. /**
  20. * @var
  21. */
  22. public $url;
  23. public $water;
  24. public $wxapp_id;
  25. public $type;
  26. public $tempDownloadFileName;
  27. /**
  28. * WaterMarkBase constructor.
  29. *
  30. * @param $url //图片路径
  31. * @param $water //水印内容
  32. * @param $wxapp_id //wxappid
  33. */
  34. public function __construct($url, $water, $wxapp_id, $type = 'image')
  35. {
  36. $this->url = $url;
  37. $this->water = $water;
  38. $this->wxapp_id = $wxapp_id;
  39. $this->type = $type;
  40. }
  41. /**
  42. * [waterMark]
  43. * 添加水印
  44. *
  45. * @Author: Yeshihao
  46. * @DateTime: 2024/11/27 14:03
  47. * @return string
  48. */
  49. public function waterMark()
  50. {
  51. //获取文件路径查看是否存在本地
  52. $filePath = $this->saveTempImage($this->wxapp_id, $this->url);
  53. //根据类型调用水印方法
  54. switch ($this->type) {
  55. case 'image':
  56. $image = new Image();
  57. $image->waterMark($filePath, $this->water, 1);
  58. break;
  59. case 'pdf':
  60. $pdf = new Pdf();
  61. $pdf->waterMark($filePath, $this->water);
  62. break;
  63. }
  64. return $filePath;
  65. }
  66. /**
  67. * [saveTempImage]
  68. * 保存临时文件
  69. *
  70. * @Author: Yeshihao
  71. * @DateTime: 2024/11/25 14:28
  72. * @param $wxapp_id
  73. * @param $url
  74. * @param string $mark
  75. * @return string
  76. */
  77. private
  78. function saveTempImage($wxapp_id, $url, $mark = 'temp')
  79. {
  80. $savePath = $this->getTempSavePath($wxapp_id, $url, $mark);
  81. if (file_exists($savePath)) {
  82. return $savePath;
  83. }
  84. $ch = curl_init($url);
  85. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  86. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  87. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  88. $img = curl_exec($ch);
  89. curl_close($ch);
  90. $fp = fopen($savePath, 'w');
  91. fwrite($fp, $img);
  92. fclose($fp);
  93. return $savePath;
  94. }
  95. /**
  96. * 获取保存路径
  97. *
  98. * @param int $wxapp_id
  99. * @param string $url
  100. * @param string $mark
  101. * @return string
  102. * @Author Mark
  103. * @DateTime 2024-08-14
  104. */
  105. private function getTempSavePath($wxapp_id, $url, $mark = 'temp'): string
  106. {
  107. $dirPath = WEB_PATH.'temp/image'.'/'.$wxapp_id;
  108. !is_dir($dirPath) && mkdir($dirPath, 0755, true);
  109. if ($this->type == 'pdf') {
  110. $fileName = $mark.'_'.date('YmdHis').'_'.md5($url).'.pdf';
  111. } else {
  112. $fileName = $mark.'_'.date('YmdHis').'_'.md5($url).'.png';
  113. }
  114. $savePath = $dirPath.'/'.$fileName;
  115. $this->tempDownloadFileName = $fileName;
  116. return $savePath;
  117. }
  118. /**
  119. * gif转png
  120. *
  121. * @param string $path
  122. * @return void
  123. * @Author Mark
  124. * @DateTime 2024-06-12
  125. */
  126. private function git2png($path)
  127. {
  128. $image = imagecreatefromgif($path);
  129. if ($image === false) {
  130. return true;
  131. }
  132. // 设置PNG的背景颜色(透明度)
  133. $background = imagecolorallocatealpha($image, 0, 0, 0, 127);
  134. // 创建一个全新的真彩色图像
  135. $png_image = imagecreatetruecolor(imagesx($image), imagesy($image));
  136. // 设置透明度,以便将GIF的透明区域变成半透明的黑色
  137. imagefill($png_image, 0, 0, $background);
  138. // 如果GIF有透明区域,将其合并到新图片中
  139. if (imagecolortransparent($image) >= 0) {
  140. // 获取GIF图片的透明颜色索引
  141. $transparent_index = imagecolortransparent($image);
  142. // 获取透明颜色
  143. $transparent_color = imagecolorsforindex($image, $transparent_index);
  144. // 设置新图片的透明颜色
  145. $background = imagecolorallocatealpha($png_image, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue'], $transparent_color['alpha']);
  146. // 设置新图片的透明区域
  147. imagefill($png_image, 0, 0, $background);
  148. imagecolortransparent($png_image, $background);
  149. }
  150. // 将原始图片拷贝到新图片上
  151. imagecopy($png_image, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
  152. imagepng($png_image, $path);
  153. // 释放内存
  154. imagedestroy($image);
  155. imagedestroy($png_image);
  156. }
  157. /**
  158. * 设置错误信息
  159. *
  160. * @param string $msg
  161. * @return false
  162. * @Author Mark
  163. * @DateTime 2024-08-23
  164. */
  165. public function setError(string $msg): bool
  166. {
  167. $this->error = $msg;
  168. return false;
  169. }
  170. /**
  171. * 获取错误信息
  172. *
  173. * @return string
  174. * @Author Mark
  175. * @DateTime 2024-08-23
  176. */
  177. public function getError(): string
  178. {
  179. return $this->error;
  180. }
  181. }