url = $url; $this->water = $water; $this->water2 = $water2; $this->wxapp_id = $wxapp_id; $this->type = $type; } /** * [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, 1, $this->water2); break; case 'pdf': $pdf = new Pdf(); $pdf->waterMark($filePath, $this->water, 1, $this->water2); 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; } }