WaterMarkBase.php 4.7 KB

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