| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- namespace app\common\service\watermark;
- use app\common\service\watermark\image\Image;
- use app\common\service\watermark\pdf\Pdf;
- /**
- * Class WaterMarkBase
- * 生成水印
- *
- * @Author: Yeshihao
- * @DateTime: 2024/11/27 14:03
- * @package app\common\service\watermark
- */
- class WaterMarkBase
- {
- /**
- * @var string
- */
- public $error = '';
- /**
- * @var
- */
- public $url;
- public $water;
- public $water2;
- public $wxapp_id;
- public $type;
- public $width;
- public $height;
- public $alpha;
- public $tempDownloadFileName;
- /**
- * WaterMarkBase constructor.
- *
- * @param $url
- * @param $water
- * @param $wxapp_id
- * @param int $alpha
- * @param string $type
- * @param string $water2
- * @param int $width
- * @param int $height
- */
- public function __construct($url, $water, $wxapp_id, $alpha = 30, $type = 'image', $water2 = '', $width = 0, $height = 0)
- {
- $this->url = $url;
- $this->water = $water;
- $this->water2 = $water2;
- $this->wxapp_id = $wxapp_id;
- $this->alpha = $alpha;
- $this->type = $type;
- $this->width = $width;
- $this->height = $height;
- }
- /**
- * [waterMark]
- *
- * @Author: Yeshihao
- * @DateTime: 2024/11/28 17:19
- * @return array
- * @throws \Mpdf\MpdfException
- * @throws \setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException
- * @throws \setasign\Fpdi\PdfParser\PdfParserException
- * @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
- */
- public function waterMark()
- {
- //获取文件路径查看是否存在本地
- $filePath = $this->saveTempImage($this->wxapp_id, $this->url);
- //根据类型调用水印方法
- switch ($this->type) {
- case 'image':
- $image = new Image();
- $image->waterMark($filePath, $this->water, $this->alpha, 1, $this->water2, $this->width, $this->height);
- break;
- case 'pdf':
- $pdf = new Pdf();
- $pdf->waterMark($filePath, $this->water, $this->alpha, 1, $this->water2, $this->width, $this->height);
- break;
- }
- return ['file_path' => $filePath, 'file_name' => $this->tempDownloadFileName];
- }
- /**
- * [saveTempImage]
- * 保存临时文件
- *
- * @Author: Yeshihao
- * @DateTime: 2024/11/25 14:28
- * @param $wxapp_id
- * @param $url
- * @param string $mark
- * @return string
- */
- private function saveTempImage($wxapp_id, $url, $mark = 'temp')
- {
- $savePath = $this->getTempSavePath($wxapp_id, $url, $mark);
- if (file_exists($savePath)) {
- return $savePath;
- }
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
- $img = curl_exec($ch);
- curl_close($ch);
- $fp = fopen($savePath, 'w');
- fwrite($fp, $img);
- fclose($fp);
- return $savePath;
- }
- /**
- * 获取保存路径
- *
- * @param int $wxapp_id
- * @param string $url
- * @param string $mark
- * @return string
- * @Author Mark
- * @DateTime 2024-08-14
- */
- private function getTempSavePath($wxapp_id, $url, $mark = 'temp'): string
- {
- $dirPath = WEB_PATH.'temp/image'.'/'.$wxapp_id;
- !is_dir($dirPath) && mkdir($dirPath, 0755, true);
- if ($this->type == 'pdf') {
- $fileName = $mark.'_'.date('YmdHis').'_'.md5($url).'.pdf';
- } else {
- $fileName = $mark.'_'.date('YmdHis').'_'.md5($url).'.png';
- }
- $savePath = $dirPath.'/'.$fileName;
- $this->tempDownloadFileName = $fileName;
- return $savePath;
- }
- /**
- * gif转png
- *
- * @param string $path
- * @return void
- * @Author Mark
- * @DateTime 2024-06-12
- */
- private function git2png($path)
- {
- $image = imagecreatefromgif($path);
- if ($image === false) {
- return true;
- }
- // 设置PNG的背景颜色(透明度)
- $background = imagecolorallocatealpha($image, 0, 0, 0, 127);
- // 创建一个全新的真彩色图像
- $png_image = imagecreatetruecolor(imagesx($image), imagesy($image));
- // 设置透明度,以便将GIF的透明区域变成半透明的黑色
- imagefill($png_image, 0, 0, $background);
- // 如果GIF有透明区域,将其合并到新图片中
- if (imagecolortransparent($image) >= 0) {
- // 获取GIF图片的透明颜色索引
- $transparent_index = imagecolortransparent($image);
- // 获取透明颜色
- $transparent_color = imagecolorsforindex($image, $transparent_index);
- // 设置新图片的透明颜色
- $background = imagecolorallocatealpha($png_image, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue'], $transparent_color['alpha']);
- // 设置新图片的透明区域
- imagefill($png_image, 0, 0, $background);
- imagecolortransparent($png_image, $background);
- }
- // 将原始图片拷贝到新图片上
- imagecopy($png_image, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
- imagepng($png_image, $path);
- // 释放内存
- imagedestroy($image);
- imagedestroy($png_image);
- }
- /**
- * 设置错误信息
- *
- * @param string $msg
- * @return false
- * @Author Mark
- * @DateTime 2024-08-23
- */
- public function setError(string $msg): bool
- {
- $this->error = $msg;
- return false;
- }
- /**
- * 获取错误信息
- *
- * @return string
- * @Author Mark
- * @DateTime 2024-08-23
- */
- public function getError(): string
- {
- return $this->error;
- }
- }
|