yeshihao 1 год назад
Родитель
Сommit
4caeb79e13

+ 6 - 3
dyu/application/common/service/watermark/WaterMarkBase.php

@@ -30,6 +30,8 @@ class WaterMarkBase
 
 	public $water2;
 
+	public $water3;
+
 	public $wxapp_id;
 
 	public $type;
@@ -51,7 +53,7 @@ class WaterMarkBase
 	 * @param int    $width
 	 * @param int    $height
 	 */
-	public function __construct($url, $water, $wxapp_id, $alpha = 30, $type = 'image', $water2 = '', $width = 0, $height = 0)
+	public function __construct($url, $water, $wxapp_id, $alpha = 30, $type = 'image', $water2 = '', $width = 0, $height = 0, $water3 = 0)
 	{
 		$this->url      = $url;
 		$this->water    = $water;
@@ -61,6 +63,7 @@ class WaterMarkBase
 		$this->type     = $type;
 		$this->width    = $width;
 		$this->height   = $height;
+		$this->water3   = $water3;
 	}
 
 	/**
@@ -82,11 +85,11 @@ class WaterMarkBase
 		switch ($this->type) {
 			case 'image':
 				$image = new Image();
-				$image->waterMark($filePath, $this->water, $this->alpha, 1, $this->water2, $this->width, $this->height);
+				$image->waterMark($filePath, $this->water, $this->alpha, 1, $this->water2, $this->width, $this->height, $this->water3);
 				break;
 			case 'pdf':
 				$pdf = new Pdf();
-				$pdf->waterMark($filePath, $this->water, $this->alpha, 1, $this->water2, $this->width, $this->height);
+				$pdf->waterMark($filePath, $this->water, $this->alpha, 1, $this->water2, $this->width, $this->height, $this->water3);
 				break;
 		}
 

+ 13 - 2
dyu/application/common/service/watermark/image/Image.php

@@ -106,7 +106,7 @@ class Image
 	 * @return bool
 	 * @throws
 	 */
-	public function waterMark($filePath, $waterText, $alpha = 30, $type = 0, $waterText2 = '', $width = 0, $height = 0)
+	public function waterMark($filePath, $waterText, $alpha = 30, $type = 0, $waterText2 = '', $width = 0, $height = 0, $waterText3 = '')
 	{
 		// 创建图片资源
 		$image_info = getimagesize($filePath);
@@ -140,6 +140,12 @@ class Image
 				$fontNum = $fontNum2;
 			}
 		}
+		if ($waterText3) {
+			$fontNum3 = mb_strlen($waterText3);
+			if ($fontNum3 > $fontNum) {
+				$fontNum = $fontNum3;
+			}
+		}
 		$fontPath = Grafika::fontsDir().DS.'st-heiti-light.ttc';
 		// 3.创建文字图片
 		//判断是否需要添加水印
@@ -164,7 +170,12 @@ class Image
 
 		// 添加水印文字  // imagettftext() 的参数依次是:图片资源, 字体大小, 旋转角度, X坐标, Y坐标, 颜色, 字体文件, 文本内容
 		imagettftext($image, $fontSize, $angle, $fontX, $fontY, $color, $fontPath, $waterText);
-		imagettftext($image, $fontSize, $angle, $fontX + $fontSize, $fontY + $fontSize, $color, $fontPath, $waterText2);
+		if ($waterText2) {
+			imagettftext($image, $fontSize, $angle, $fontX + 1.5 * $fontSize, $fontY + 0.5 * $fontSize, $color, $fontPath, $waterText2);
+		}
+		if ($waterText3) {
+			imagettftext($image, $fontSize, $angle, $fontX - 0.5 * $fontSize, $fontY - 1.5 * $fontSize, $color, $fontPath, $waterText3);
+		}
 
 		// 保存带有水印的图片
 		imagejpeg($image, $filePath, 90);

+ 116 - 27
dyu/application/common/service/watermark/pdf/Pdf.php

@@ -2,10 +2,12 @@
 
 namespace app\common\service\watermark\pdf;
 
+use Grafika\Grafika;
 use Mpdf\Mpdf;
 use Mpdf\Config\ConfigVariables;
 use Mpdf\Config\FontVariables;
 use TCPDF;
+use setasign\Fpdi\Tcpdf\Fpdi;
 
 /**
  * Class Pdf
@@ -58,36 +60,123 @@ class Pdf
 	 * @throws \setasign\Fpdi\PdfParser\PdfParserException
 	 * @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
 	 */
-	public function waterMark($filePath, $waterText, $alpha = 30, $type = 0, $waterText2 = '', $pageWidth = 210, $pageHeight = 297)
+//	public function waterMark($filePath, $waterText, $alpha = 30, $type = 0, $waterText2 = '', $pageWidth = 210, $pageHeight = 297)
+//	{
+//		$defaultConfig          = (new ConfigVariables())->getDefaults();
+//		$fontDirs               = $defaultConfig['fontDir'];
+//		$defaultFontConfig      = (new FontVariables())->getDefaults();
+//		$fontData               = $defaultFontConfig['fontdata'];
+//		$mpdf                   = new Mpdf(['mode'         => '+aCJK',
+//		                                    'fontDir'      => array_merge($fontDirs, [__DIR__.'/fonts']),
+//		                                    'fontdata'     => $fontData + ['msyh' => ['R' => 'msyh.ttf',],],
+//		                                    'default_font' => 'msyh',
+//		                                    'format'       => [$pageWidth ?: 210, $pageHeight ?: 297],
+//		]);
+//		$mpdf->autoScriptToLang = true;
+//		$mpdf->autoLangToFont   = true;
+//
+//		$text = $waterText.$waterText2;
+//		//文字水印
+//		$alpha         = number_format((100 - $alpha) / 100, 2);
+//		$watermarkText = new \Mpdf\WatermarkText($text, 30, 30, '#cccccc', $alpha);
+//		$mpdf->SetWatermarkText($watermarkText, $alpha);
+//		$mpdf->showWatermarkText = true;
+//		$mpdf->watermarkAngle    = '45';
+//		$pageCount               = $mpdf->SetSourceFile($filePath); //读取原始文件页数
+//		for ($i = 1; $i <= $pageCount; $i++) {
+//			$import_page = $mpdf->ImportPage($i);
+//			$mpdf->UseTemplate($import_page);
+//			if ($i < $pageCount)
+//				$mpdf->AddPage();
+//		}
+//		$mpdf->Output($filePath, 'F');// 下载修改后的PDF
+//		return true;
+//	}
+
+	/**
+	 * [waterMark]
+	 *
+	 * @Author: Yeshihao
+	 * @DateTime: 2025/4/27 16:48
+	 * @param        $filePath
+	 * @param        $waterText
+	 * @param int    $alpha
+	 * @param int    $type
+	 * @param string $waterText2
+	 * @param int    $pageWidth
+	 * @param int    $pageHeight
+	 * @param string $waterText3
+	 * @return bool
+	 * @throws \setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException
+	 * @throws \setasign\Fpdi\PdfParser\Filter\FilterException
+	 * @throws \setasign\Fpdi\PdfParser\PdfParserException
+	 * @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
+	 * @throws \setasign\Fpdi\PdfReader\PdfReaderException
+	 */
+	public function waterMark($filePath, $waterText, $alpha = 30, $type = 0, $waterText2 = '', $pageWidth = 210, $pageHeight = 297, $waterText3 = '')
 	{
-		$defaultConfig          = (new ConfigVariables())->getDefaults();
-		$fontDirs               = $defaultConfig['fontDir'];
-		$defaultFontConfig      = (new FontVariables())->getDefaults();
-		$fontData               = $defaultFontConfig['fontdata'];
-		$mpdf                   = new Mpdf(['mode'         => '+aCJK',
-		                                    'fontDir'      => array_merge($fontDirs, [__DIR__.'/fonts']),
-		                                    'fontdata'     => $fontData + ['msyh' => ['R' => 'msyh.ttf',],],
-		                                    'default_font' => 'msyh',
-		                                    'format'       => [$pageWidth ?: 210, $pageHeight ?: 297],
-		]);
-		$mpdf->autoScriptToLang = true;
-		$mpdf->autoLangToFont   = true;
-
-		$text = $waterText.$waterText2;
-		//文字水印
-		$alpha         = number_format((100 - $alpha) / 100, 2);
-		$watermarkText = new \Mpdf\WatermarkText($text, 30, 30, '#cccccc', $alpha);
-		$mpdf->SetWatermarkText($watermarkText, $alpha);
-		$mpdf->showWatermarkText = true;
-		$mpdf->watermarkAngle    = '45';
-		$pageCount               = $mpdf->SetSourceFile($filePath); //读取原始文件页数
+		// 初始化 FPDI
+		$pdf = new Fpdi();
+
+//		$fontRegular = WEB_PATH . "static/common/fonts/msyh.ttf";
+//		$pdf->AddFont('msyh', '', $fontRegular, true);    // 常规
+//
+//// 设置默认字体
+//		$pdf->SetFont('msyh', '', 40);  // 使用常规字体
+
+// 确保 PHP 文件编码为 UTF-8(无 BOM)
+//		$pdf->setHeaderData('', 0, '', '', [0, 0, 0], [255, 255, 255]);
+//		$pdf->setDocInfo('Creator', 'Your App', 'Title', 'Subject', 'Keywords', 'UTF-8');
+
+		$pdf->SetFont('cid0cs', '', 30); // 使用内置中文字体
+		$pdf->setLanguageArray(['a_meta_charset' => 'UTF-8']);
+
+		// 加载已有 PDF
+		$pageCount = $pdf->setSourceFile($filePath);
+
+		//透明度
+		$alpha = number_format((100 - $alpha) / 100, 2);
+
+		// 遍历每一页
 		for ($i = 1; $i <= $pageCount; $i++) {
-			$import_page = $mpdf->ImportPage($i);
-			$mpdf->UseTemplate($import_page);
-			if ($i < $pageCount)
-				$mpdf->AddPage();
+			// 导入当前页
+			$templateId = $pdf->importPage($i);
+			$size       = $pdf->getTemplateSize($templateId);
+			$pdf->AddPage($size['orientation'], [$size['width'], $size['height']]);
+			$pdf->useTemplate($templateId);
+
+			$pdf->SetTextColor(204, 204, 204); // 透明度
+			$pdf->SetAlpha($alpha); // 整体透明度
+
+			// 计算居中位置
+			$x = $size['width'] / 2;
+			$y = $size['height'] / 2;
+
+			// 第一行水印
+			$pdf->StartTransform();
+			$pdf->Rotate(45, $x, $y);
+			$pdf->Text($x - 80, $y, $waterText);
+			$pdf->StopTransform();
+
+			if ($waterText2) {
+				// 第二行水印
+				$pdf->StartTransform();
+				$pdf->Rotate(45, $x, $y);
+				$pdf->Text($x - 80, $y + 15, $waterText2); // 调整 Y 坐标控制行间距
+				$pdf->StopTransform();
+			}
+
+			if ($waterText3) {
+				// 第三行水印
+				$pdf->StartTransform();
+				$pdf->Rotate(45, $x, $y);
+				$pdf->Text($x - 80, $y - 15, $waterText3); // 调整 Y 坐标控制行间距
+				$pdf->StopTransform();
+			}
 		}
-		$mpdf->Output($filePath, 'F');// 下载修改后的PDF
+
+// 输出 PDF
+		$pdf->Output($filePath, 'F');
 		return true;
 	}
 }

+ 21 - 5
dyu/application/store/model/CertificateApply.php

@@ -14,6 +14,8 @@ use app\common\enum\dtalk\RobotSence;
 use app\common\model\CertificateApply as CertificateApplyModel;
 use app\common\service\dtalk\robot\BaseRobot;
 use app\common\service\watermark\WaterMarkBase;
+use TCPDF;
+use setasign\Fpdi\Tcpdf\Fpdi;
 
 /**
  * 拼团商品分类模型
@@ -115,10 +117,15 @@ class CertificateApply extends CertificateApplyModel
 				return false;
 			}
 			foreach ($certificateList as $certificate) {
-				$water  = $this->watermark ?: '审批号:'.$this->order_no;
+				$water  = '审批号:'.$this->order_no;
 				$water2 = '';
 				if ($this['indate']) {
-					$water2 = $data['use_data'] ? '有效期至'.$this['indate'] : '';
+					$water2 = $data['use_date'] ? '有效期至'.$this->indate : '';
+				}
+				$water3 = '';
+				if ($this->watermark) {
+					$water3 = $water;
+					$water  = $this->watermark;
 				}
 				$service       = new WaterMarkBase($certificate['file']['file_path'],
 					$water,
@@ -127,7 +134,9 @@ class CertificateApply extends CertificateApplyModel
 					$certificate['file_type'],
 					$water2,
 					$certificate['width'],
-					$certificate['height']);
+					$certificate['height'],
+					$water3
+				);
 				$result        = $service->waterMark();
 				$result_data[] = [
 					'name'           => $certificate['name'],
@@ -181,11 +190,16 @@ class CertificateApply extends CertificateApplyModel
 			return false;
 		}
 		foreach ($certificateList as $certificate) {
-			$water  = $this->watermark ?: '审批号:'.$this->order_no;
+			$water  = '审批号:'.$this->order_no;
 			$water2 = '';
 			if ($this['indate']) {
 				$water2 = $this->use_date ? '有效期至'.$this->indate : '';
 			}
+			$water3 = '';
+			if ($this->watermark) {
+				$water3 = $water;
+				$water  = $this->watermark;
+			}
 			$service       = new WaterMarkBase($certificate['file']['file_path'],
 				$water,
 				self::$wxapp_id,
@@ -193,7 +207,9 @@ class CertificateApply extends CertificateApplyModel
 				$certificate['file_type'],
 				$water2,
 				$certificate['width'],
-				$certificate['height']);
+				$certificate['height'],
+				$water3
+			);
 			$result        = $service->waterMark();
 			$result_data[] = [
 				'name'           => $certificate['name'],

+ 4 - 4
dyu/application/store/view/content/certificate/watermark/detail.php

@@ -77,13 +77,13 @@
                                 <label class="am-u-sm-3 am-u-lg-2 am-form-label ">打印有效日期 </label>
                                 <div class="am-u-sm-9 am-u-end">
                                     <label class="am-radio-inline">
-                                        <input type="radio" name="use_data" value="1"
-                                               data-am-ucheck <?= ['use_date'] ? '' : 'checked' ?> disabled>
+                                        <input type="radio" name="use_date" value="1"
+                                               data-am-ucheck <?= ['use_date'] ? 'checked' : '' ?> disabled>
                                     </label>
                                     <label class="am-radio-inline">
-                                        <input type="radio" name="use_data" value="0"
-                                               data-am-ucheck <?= ['use_date'] ? 'checked' : '' ?> disabled>
+                                        <input type="radio" name="use_date" value="0"
+                                               data-am-ucheck <?= ['use_date'] ? '' : 'checked' ?> disabled>
                                     </label>
                                 </div>

+ 4 - 4
dyu/application/store/view/content/certificate/watermark/edit.php

@@ -65,13 +65,13 @@
                                 <label class="am-u-sm-3 am-u-lg-2 am-form-label ">打印有效日期 </label>
                                 <div class="am-u-sm-9 am-u-end">
                                     <label class="am-radio-inline">
-                                        <input type="radio" name="use_data" value="1"
-                                               data-am-ucheck <?= ['indate'] ? '' : 'checked' ?> <?= empty($model['indate']) ? 'disabled' : '' ?>>
+                                        <input type="radio" name="use_date" value="1"
+                                               data-am-ucheck <?= ['indate'] ? 'checked' : '' ?> <?= empty($model['indate']) ? 'disabled' : '' ?>>
                                     </label>
                                     <label class="am-radio-inline">
-                                        <input type="radio" name="use_data" value="0"
-                                               data-am-ucheck <?= ['indate'] ? 'checked' : '' ?> <?= empty($model['indate']) ? 'disabled' : '' ?>>
+                                        <input type="radio" name="use_date" value="0"
+                                               data-am-ucheck <?= ['indate'] ? '' : 'checked' ?> <?= empty($model['indate']) ? 'disabled' : '' ?>>
                                     </label>
                                 </div>