前几天测试老大提了一个需求,说我们的包厢的二维码太丑,商家自己去找设计,需要花费不少钱,能不能直接生成带底图的二维码,效果如下
先贴下有底图的效果的代码
$qucodepath="png/qrode.png";
$content ="http://192.168.97.178:3020/?b=20110319212222222277";
$backgroundpath="png/chest_card1.png";
$resultPngPic="png/result.png";
if (file_exists($resultPngPic)){
unlink($resultPngPic);
}
include('phpqrcode.php');
$margin = 2;
$errorCorrectionLevel = 'M';
$size =9;
// 二维码
QRcode::png($content,$qucodepath,QR_ECLEVEL_M,$size,$margin);
$qrcode = imagecreatefrompng($qucodepath);
list($qrcode_w,$qrcode_h) =getimagesize($qucodepath);
$dst_x = 69.5;
$dst_y = 277;
$src_x = 0;
$src_y = 0;
$dst_w = 160;
$dst_h = 160;
$backgroundImg = imagecreatefrompng($backgroundpath);
// 底图和二维码整合
imagecopyresampled($backgroundImg,$qrcode, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $qrcode_w,$qrcode_h);
imagepng($backgroundImg,$resultPngPic);
imagedestroy($backgroundImg);
unlink($qucodepath);
效果
二维码已经有背景图片了,咱们继续,再加上头像的代码,也是类似的
list($src_w,$src_h) = getimagesize($headPath);
$dst_x = 105;
$dst_y = 47;
$src_x = 0;
$src_y = 0;
$dst_w = 90;
$dst_h = 90;
imagecopyresampled($backgroundImg,$header, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w,$src_h);
难得是图片上的文字,最关键要水平居中。
// 下面是生成文字,并水平居中
$companyname="测试031";
$boxname="V11";
// 开源苹方字体
$fontpath='PingFangSCM.ttf';
$font_size = 15;
$r =255;$g =110;$b = 0;
$fontColor = imagecolorallocate ( $backgroundImg, $r, $g, $b );//字的RGB颜色
$x =73;$y=220;
$width = imagesx ( $backgroundImg );
$fontBox = imagettfbbox($font_size, 0, $fontpath, $companyname);//文字水平居中实质
$x = ceil(($width - $fontBox[2]) / 2);
imagettftext($backgroundImg,$font_size,0,$x,$y,$fontColor,$fontpath, $companyname);
$r=$g=$b =255;
$fontColor = imagecolorallocate ( $backgroundImg, $r, $g, $b );//字的RGB颜色
$y = 270;
$fontBox = imagettfbbox($font_size, 0, $fontpath, $boxname);//文字水平居中实质
$x = ceil(($width - $fontBox[2]) / 2);
imagettftext($backgroundImg,$font_size,0,$x,$y,$fontColor,$fontpath, $boxname);
效果
最后简单说一下,生成二维码是使用了qrcode库,网上很好找