laravel 在图片中 生成二维码和添加文字

一、生成二维码

1. 安装 安装了 simplesoftwareio/simple-qrcode扩展包,配置文件
composer require simplesoftwareio/simple-qrcode
2、配置laravel 在config/app.php 注册服务
// 在config/app.php 注册服务提供者:Service Provider
  SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class
// 在 config/app.php 添加 QrCode 门面:添加 Aliases
'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class
3、生成二维码
		1. errorCorrection 容错级别设置
	 		L	7% 的字节码恢复率.
			M	15% 的字节码恢复率.
			Q	25% 的字节码恢复率.
			H	30% 的字节码恢复率.
		2.margin    边距设置  
		3.merge    二维码中添加图片,方法只支持 PNG,参数(图像路径或 URL,图像宽度和高度)
		4.backgroundColor(255, 0, 0)  设置二维码背景色
		5.color(255,0,255)   颜色
		6.size      字体设置
		7.generate  设置二维码参数:  
			$QrCodeText  二维码内容
			$imagePath   二维码文件
		// 代码实例
		 QrCode::format('png')
		 	->errorCorrection('L')
		 	->merge('path/img.png',0.5,true)
		 	->backgroundColor(255, 0, 0)
		 	->margin(0.5)
		 	->size(190)
		 	->generate($shareCodeurl, $imagePath);

4、图片合成

php 处理图像函数 及 实例

1、imagecreatefromjpeg($filename) 从给定的 JPG 文件创建一个新的图像资源
		$image = imagecreatefromjpeg('path/to/image.jpg');
		
2、imagecreatefrompng($filename)从给定的 PNG 文件创建一个新的图像资源。
		$image = imagecreatefrompng('path/to/image.png');		
		
3、imagecreatefromgif($filename)从给定的 GIF 文件创建一个新的图像资源。
		$image = imagecreatefromgif('path/to/image.gif');
		
4、imagecreate($width, $height):创建一个新的空白图像资源,指定宽度和高度。
		$image = imagecreate(500, 500);
		
5、imagecolorallocate($image, $red, $green, $blue):为给定的图像分配一个颜色
		$color = imagecolorallocate($image, 255, 0, 0); // 分配红色
		
6、imageline($image, $x1, $y1, $x2, $y2, $color):在给定的图像上绘制一条线。
		imageline($image, 0, 0, 100, 100, $color); // 从坐标 (0,0) 到 (100,100) 绘制一条红色线
		
7、imagefilledrectangle($image, $x1, $y1, $x2, $y2, $color):在给定的图像上绘制一个填充的矩形。
		imagefilledrectangle($image, 50, 50, 150, 150, $color); // 在图像上绘制一个红色的填充矩形
		
8、imagecopy($sourceImage, $destinationImage, $sourceX, $sourceY, $destinationX, $destinationY, $width, $height):将一个图像的一部分复制到另一个图像上
		$sourceImage = imagecreatefrompng('path/to/source.png'); // 源图像  
		$destinationImage = imagecreate(200, 200); // 目标图像  
		imagecopy($sourceImage, $destinationImage, 0, 0, 0, 0, imagesx($sourceImage), imagesy($sourceImage)); // 将源图像复制到目标图像上

图片合并函数 imagecopyresampled用于将一个图像复制并重新采样到另一个图像上

imagecopyresampled 函数的语法:

bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

参数说明:

  • $dst_image:目标图像的资源标识符。
  • $src_image:源图像的资源标识符。
  • $dst_x:目标图像上复制的起始 x 坐标。
  • $dst_y:目标图像上复制的起始 y 坐标。
  • $src_x:源图像上开始复制的 x 坐标。
  • $src_y:源图像上开始复制的 y 坐标。
  • $dst_w:目标图像上复制的宽度。
  • $dst_h:目标图像上复制的高度。
  • $src_w:源图像上复制的宽度。
  • $src_h:源图像上复制的高度。

简单的示例。imagecopyresampled函数将一个图像缩放到指定大小,并复制到另一个图像上

		// 提前准备好的海报图  必须是PNG格式
       $bg = imagecreatefrompng($backgroundImagePath);
       $destinationImage = imagecreatetruecolor(800, 600);  
       //二维码
       $qrcodes = imagecreatefrompng(public_path($imagePath)); //二维码
       
       $x = imagesx($bg); //二维码开始位置的x坐标
       $y = imagesy($bg); //二维码开始位置的x坐标

       imagecopyresampled($bg, $qrcodes, 104, 274, 0, 0, imagesx($qrcodes), imagesx($qrcodes), imagesx($qrcodes), imagesx($qrcodes));
       imagepng($bg, $imagePath); //合并图片
       // 保存目标图像为 PNG 文件  
   	   imagepng($destinationImage, 'path/to/destination.png');  
   		// 释放图像资源  
   	   imagedestroy($sourceImage);  
   	   imagedestroy($destinationImage);

5、图片中添加文字,直接上完整代码

public function addTextToBackgroundImage($backgroundImagePath, $text, $outputPath='path/img.png',$textAlignment='center')
    {
        // 打开背景图片
        $backgroundImage = imagecreatefrompng(public_path($backgroundImagePath));

        // 获取背景图片的宽度和高度
        $backgroundWidth = imagesx($backgroundImage);
        $backgroundHeight = imagesy($backgroundImage);
        // 设置文字样式
        $font = //'path/to/your/font.ttf'; // 替换为你的字体文件路径
        $fontSize = 18; // 字体大小
        $color = imagecolorallocate($backgroundImage, 26, 11, 1); // 文字颜色,这里设置为白色
        $positionX = 135; // 文字起始位置的 X 坐标
        $positionY = 242; // 文字起始位置的 Y 坐标
        // 获取文字的宽度和高度
        $textWidth = imagettfbbox($fontSize, 0, $font, $text);
        $textHeight = imagettfbbox($fontSize, 0, $font, $text);
        $positionX = ($backgroundWidth - $textWidth[2]) / 2;
        // 根据对齐方式计算文字的位置
        switch ($textAlignment) {
            case 'center':
                $positionX = ($backgroundWidth - $textWidth[2]) / 2;
                $positionY = ($backgroundHeight - $textHeight[1]) / 2;
                break;
            case 'top':
                $positionY = 0;
                break;
            case 'bottom':
                $positionY = $backgroundHeight - $textHeight[1];
                break;
            default:
                // 默认居中
                $positionX = ($backgroundWidth - $textWidth[2]) / 2;
                $positionY = ($backgroundHeight - $textHeight[1]) / 2;
                break;
        } 
        // 在背景图片上添加文字
        imagettftext($backgroundImage, $fontSize, 0, $positionX, $positionY, $color, $font, $text);

        // 输出最终图像到文件或浏览器
        if ($outputPath !== null) {
            imagepng($backgroundImage, $outputPath); // 输出为 PNG 格式的文件
        } else {
            header('Content-Type: image/png'); // 输出到浏览器时设置正确的图像类型头信息
			//imagepng($backgroundImage); // 输出为 PNG 格式的图像到浏览器
        	//保存至本地文件
        	imagepng($backgroundImage, $outputPath);
        }
        // 清理内存中的图像资源
        imagedestroy($backgroundImage);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,这里是一个 Vue 组件,可以生成带有文字图片的二维码: ```html <template> <div> <canvas ref="qrcodeCanvas"></canvas> </div> </template> <script> import QRCode from "qrcode"; export default { name: "QrcodeWithImageAndText", props: { value: { type: String, required: true }, size: { type: Number, default: 100 }, text: { type: String, default: "" }, textColor: { type: String, default: "#000" }, textSize: { type: Number, default: 16 }, image: { type: String, default: "" }, imageWidth: { type: Number, default: 20 }, imageHeight: { type: Number, default: 20 } }, mounted() { this.generate(); }, methods: { async generate() { try { const canvas = this.$refs.qrcodeCanvas; const url = await QRCode.toDataURL(this.value, { width: this.size }); const img = new Image(); img.src = url; img.onload = () => { canvas.width = img.width; canvas.height = img.height + this.textSize + this.imageHeight; const ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0); ctx.font = `${this.textSize}px sans-serif`; ctx.fillStyle = this.textColor; ctx.textAlign = "center"; ctx.fillText( this.text, img.width / 2, img.height + this.textSize + this.imageHeight ); const image = new Image(); image.src = this.image; image.onload = () => { ctx.drawImage( image, img.width / 2 - this.imageWidth / 2, img.height + this.textSize, this.imageWidth, this.imageHeight ); }; }; } catch (err) { console.error(err); } } } }; </script> ``` 这个组件和上一个组件也很相似,但是多了几个属性: - `image`:要添加到二维码下面的图片 URL。 - `imageWidth`:图片的宽度。 - `imageHeight`:图片的高度。 当组件挂载到页面上时,会自动调用 `generate` 方法生成带有文本和图片的二维码,并将其绘制到一个 `canvas` 元素上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jyvan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值