// 设置响应头,输出图片
// header('Content-Type: image/png');
$domain = $this->request->domain();
// $public =public_path();
// echo $domain.'/mask.png';die;
// $res =file_get_contents("./background/mask.png");
// var_dump($res);die;
// 加载背景图片
$background = imagecreatefrompng('./background/mask.png');
// 分配颜色
$textColor = imagecolorallocate($background, 255, 255, 255); // 白色
// 设置字体路径和大小
$fontPath =realpath('./background/pingfangsc-regular.otf');
$fontSize = 40; // 字体大小
$text = "巡检点名称:".$site_name;
// 设置文本的位置
$x = 370; // X 位置
$y = 300; // Y 位置
// 添加文字到图像
imagettftext($background, $fontSize, 0, $x, $y, $textColor, $fontPath, $text);
$fontSize1 = 40; // 字体大小
$text1 = "编号:".$site_code;
// 设置文本的位置
$x1 = 360; // X 位置
$y1 = 1570; // Y 位置
$textColors = imagecolorallocate($background, 51, 51, 51); //字体颜色 黑色
// 添加文字到图像
imagettftext($background, $fontSize1, 0, $x1, $y1, $textColors, $fontPath, $text1);
// 生成二维码(使用phpqrcode库)
$qr_path = 'uploads/inspection/';
if (!file_exists($qr_path)) {
@mkdir($qr_path, 0777, true);
}
$qrcodeFile = $qr_path . 'qrcode.png';
QRcode::png($site_url, $qrcodeFile, QR_ECLEVEL_L, 22,0);
// 加载生成的二维码
$qrcode = imagecreatefrompng($qrcodeFile);
// 合并二维码到背景图
$qrcodeX = 230; // 调整二维码的X位置
$qrcodeY = 630; // 调整二维码的Y位置
imagecopy($background, $qrcode, $qrcodeX, $qrcodeY, 0, 0, imagesx($qrcode), imagesy($qrcode));
// 输出最终图像
imagepng($background,$qr_path.$site_code_name.'.png');
// 清理内存
imagedestroy($background);
imagedestroy($qrcode);
return $domain."/".$qr_path.$site_code_name.'.png';
![]()