WaterMarkBase.php 5.1 KB

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