WaterMarkBase.php 4.9 KB

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