当前位置: 首页 > 科技观察

PHP生成中间有LOGO图片的二维码

时间:2023-03-12 19:42:22 科技观察

这是一个二维码生成器,可以帮你简单生成二维码,可以在二维码中间设置一个小logo图标.它非常易于使用,并且作为Symfony包提供,可以快速集成到您的项目中。查看demo下载源码查看原文:https://www.helloweba.com/view-blog-455.html,可以下载源码。安装使用Composer安装这个库。composerrequireendroid/qrcode的基本用法只需要几行代码就可以生成一个普通的二维码:require_once('vendor/autoload.php');useEndroid\QrCode\QrCode;$qrCode=newQrCode('Hellowebawelcomesyou!');header('Content-Type:'.$qrCode->getContentType());echo$qrCode->writeString();进阶使用QrCode提供多种方法参数设置,满足各种需求场景。比如你需要生成一个中间有logo的二维码,还需要保存一张二维码的图片等。请看下面代码:require_once('vendor/autoload.php');useEndroid\QrCode\ErrorCorrectionLevel;useEndroid\QrCode\LabelAlignment;useEndroid\QrCode\QrCode;useSymfony\Component\HttpFoundation\Response;//CreateabasicQRcode$qrCode=newQrCode('LifeistooshorttobegeneratingQRcodes');$qrCode->setSize(300);//设置高级选项$qrCode->setWriterByName('png')->setMargin(10)->setSize(200)->setEncoding('UTF-8')->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH)->setForegroundColor(['r'=>0,'g'=>0,'b'=>0])->setBackgroundColor(['r'=>255,'g'=>255,'b'=>255])->setLabel('手机扫一扫',12)->setLogoPath('logo.png')->setLogoWidth(64)->setValidateResult(false);//直接输出二维码header('Content-Type:'.$qrCode->getContentType());echo$qrCode->writeString();//Saveittoafile//$qrCode->writeFile(__DIR__.'/qrcode.png');//Createaresponseobject$response=newResponse($qrCode->writeString(),Response::HTTP_OK,['Content-Type'=>$qrCode->getContentType()]);运行上面的代码,会发现中间生成了一个,上面代码中:setSize():设置二维码图片的大小,数字整数,默认长宽都是300px的图片setErrorCorrectionLevel():设置错误校正级别,可以是LOW、MEDIUM、QUARTILE、HIGHsetForegroundColor():设置正面颜色,rgb,默认黑色:设置中间logo路径地址setLogoWidth():设置logo宽度,数量