一、生成二维码
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);
}