WaterMarkBase.php 4.8 KB

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