效果图
部分代码
<?php
include 'lib/phpqrcode.php';
require_once 'lib/image.php';
$value = "localhost/1?sourcetype=3" . "&tablenum=2";//二维码内容
$errorCorrectionLevel = 'L';//容错级别
$matrixPointSize = 20;//生成图片大小
//生成二维码图片
//开始生成
$color = [
[255, 255, 255],//背景色
[82, 103, 220],//定位角的颜色
[82, 103, 220],//中间内容的颜色
];
//参数 活动模板图片,二维码url,模板内二维码的位置
/**
* $template 背景图
* $url 二维码内容
* $sx,$y 坐标
* $color 二维背景颜色
*/
$template = 'qrimg.png';//背景图
$url = $value;
function getActivityImg($template, $url, $x, $y, $color)
{
//二维码中间添加logo
/******************************固定logo图片大小*******************************/
$logo_raw = "1212.jpg";//logo图
$imgHandle = new Image_process("$logo_raw");
$imgHandle->fixSizeImage(150, 150); //固定logo图片大小值
/******************************固定logo图片大小*******************************/
$logo = "1212.jpg";//固定大小logo图路径
$QR = "base.png";
$last = "last.png";
$errorCorrectionLevel = 'Q'; //防错等级
$matrixPointSize = 8; //二维码大小
//生成二维码
//参数内容:二维码储存内容,生成存储,防错等级,二维码大小,白边大小
\QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1, false, $color);
//合并logo跟二维码-----------------start
$QR = imagecreatefromstring(file_get_contents($QR));
$logo = imagecreatefromstring(file_get_contents($logo));
$QR_width = imagesx($QR);
$logo_width = imagesx($logo);
$logo_height = imagesy($logo);
$logo_qr_width = $QR_width / 5;
$scale = $logo_width / $logo_qr_width;
$logo_qr_height = $logo_height / $scale;
$from_width = ($QR_width - $logo_qr_width) / 2;
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
imagepng($QR, $last); // 生成带log的二维码图片 存储到last
// unlink('base.png');//删除二维底图
//合并logo跟二维码-----------------end
//合成带logo的二维码图片跟 模板图片--------------start
$path_1 = $template;
$path_2 = $last;
//创建一块画布,并从 PNG 文件或 URL 地址载入一副图像
$image_1 = imagecreatefrompng($path_1);
$image_2 = imagecreatefrompng($path_2);
$image_3 = imageCreatetruecolor(imagesx($image_1), imagesy($image_1));
$color = imagecolorallocate($image_3, 255, 255, 255);
imagefill($image_3, 0, 0, $color);
imageColorTransparent($image_3, $color);
imagecopyresampled($image_3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));
imagecopymerge($image_3, $image_2, $x, $y, 0, 0, imagesx($image_2), imagesy($image_2), 100);
//合成带logo的二维码图片跟 模板图片--------------end
//输出到本地文件夹
$fileName = 'qr';
$EchoPath = $fileName . '.png';
imagepng($image_3, $EchoPath);
imagedestroy($image_3);
//画图
createCanvas($EchoPath);
//返回生成的路径
// echo "<img src='" . $EchoPath . "'>";
// echo "<img src='/qrcode/logo/logo.png'>";
unlink('base.png');
unlink('last.png');
die;
}
getActivityImg($template, $url, $x = 43, $y = 44, $color);
//生成画布
function createCanvas($qrImg)
{
//require_once 'lib/font.php';
header('Content-type: text/html; charset=UTF8'); // UTF8不行改成GBK试试,与你保存的格式匹配
//第一:设定标头,告诉浏览器你要生成的MIME 类型
header("Content-type: image/png");
//第二:创建一个画布,以后的操作都将基于此画布区域
$codew = 500;
$codeh = 560;
//最外层画布
$codeimg = imagecreatetruecolor($codew, $codeh);
//底部画布
$im = imagecreatetruecolor(500, 100);
//获取画布颜色
$red = imagecolorallocate($codeimg, 255, 0, 0);
$white = imagecolorallocate($codeimg, 255, 255, 255);
$green = imagecolorallocate($codeimg, 75, 222, 26);
$bule = imagecolorallocate($codeimg, 82, 103, 220);
//第三:填充画布背景颜色
imagefill($codeimg, 0, 0, $white);
//底部颜色
imagefill($im, 0, 0, $bule);
//第四:绘制线条 + 填充文字...
//imageline($codeimg, 0, 00, 30, 60, $white);
//imageline($codeimg, 0, 00, 50, 60, $white);
//imageline($codeimg, 0, 00, 80, 60, $white);
$font_url = 'lib/msyh.ttf';
//填充文字
//imagestring($codeimg, 88, 210, 40, "storename", $bule);
$content = "阿里巴巴!";
imagettftext($codeimg, 12, 0, 210, 47, $bule, $font_url, $content);
//imagettftext($codeimg,15,0,210,40,$bule,$font_url);
//二维码
$im_new = imagecreatefrompng("$qrImg");//返回图像标识符
$im_new_info = getimagesize("$qrImg");//取得图像大小,返回一个数组。该函数不需要用到gd库。
//添加二维码到画布
imagecopy($codeimg, $im_new, 66, 75, 0, 0, $im_new_info[0], $im_new_info[1]);//返回布尔值
//添加一个小画布
imagecopy($codeimg, $im, 0, 490, 0, 0, 500, 160);//返回布尔值
//底部字体
$low_font = '111号桌扫码点餐';
imagettftext($codeimg, 12, 0, 188, 527, $white, $font_url, $low_font);
//第五:输出创建的画布
imagepng($codeimg);
//imagepng($im);//输出到页面。如果有第二个参数[,$filename],则表示保存图像
//第六:销毁画布
imagedestroy($codeimg);
// echo "<img src='qr.png'>";
}
点点赞赏,手留余香
点点赞赏,手留余香
林卧愁春尽,开轩览物华。