|
|
@@ -424,4 +424,70 @@ class BaseService
|
|
|
return $prefix . $timePart . $randomPart . $memberSuffix;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 生成支付二维码
|
|
|
+ * @param {*} $address 支付地址
|
|
|
+ * @return {*}
|
|
|
+ */
|
|
|
+ public static function createPaymentQrCode($address = '')
|
|
|
+ {
|
|
|
+ $content = $address;
|
|
|
+ $qrSize = 300;
|
|
|
+ $font = 4;
|
|
|
+ $textHeight = 20;
|
|
|
+ $padding = 10;
|
|
|
+
|
|
|
+ // 生成二维码图像对象
|
|
|
+ $result = Builder::create()
|
|
|
+ ->writer(new PngWriter())
|
|
|
+ ->data($content)
|
|
|
+ ->size($qrSize)
|
|
|
+ ->margin(0)
|
|
|
+ ->build();
|
|
|
+
|
|
|
+ $qrImage = imagecreatefromstring($result->getString());
|
|
|
+
|
|
|
+ // 创建画布(加上下方文字区和边距)
|
|
|
+ $canvasWidth = $qrSize + $padding * 2;
|
|
|
+ $canvasHeight = $qrSize + $textHeight + $padding * 2;
|
|
|
+ $image = imagecreatetruecolor($canvasWidth, $canvasHeight);
|
|
|
+
|
|
|
+ // 背景白色
|
|
|
+ $white = imagecolorallocate($image, 255, 255, 255);
|
|
|
+ imagefill($image, 0, 0, $white);
|
|
|
+
|
|
|
+ // 黑色字体
|
|
|
+ $black = imagecolorallocate($image, 0, 0, 0);
|
|
|
+
|
|
|
+ // 合并二维码图像
|
|
|
+ imagecopy($image, $qrImage, $padding, $padding, 0, 0, $qrSize, $qrSize);
|
|
|
+
|
|
|
+ // 写文字
|
|
|
+ $textWidth = imagefontwidth($font) * strlen($content);
|
|
|
+ $x = ($canvasWidth - $textWidth) / 2;
|
|
|
+ $y = $qrSize + $padding + 5;
|
|
|
+ imagestring($image, $font, $x, $y, $content, $black);
|
|
|
+
|
|
|
+ $address_name = self::generateRandomString(20).time();
|
|
|
+
|
|
|
+ // 生成文件名
|
|
|
+ $filename = $address_name. '.png';
|
|
|
+ $relativePath = 'payment/' . $filename;
|
|
|
+ $storagePath = storage_path('app/public/' . $relativePath);
|
|
|
+
|
|
|
+ // 确保目录存在
|
|
|
+ @mkdir(dirname($storagePath), 0777, true);
|
|
|
+
|
|
|
+ // 保存图片到文件
|
|
|
+ imagepng($image, $storagePath);
|
|
|
+
|
|
|
+ // 清理
|
|
|
+ imagedestroy($qrImage);
|
|
|
+ imagedestroy($image);
|
|
|
+
|
|
|
+ // 返回 public 存储路径(可用于 URL)
|
|
|
+ return 'storage/'.$relativePath; // 或返回 Storage::url($relativePath);
|
|
|
+ }
|
|
|
+
|
|
|
}
|