WaterMarkBase.php 5.2 KB

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