The example in this article describes how Symfony generates QR codes. Share it with everyone for your reference, the details are as follows:
You can now find many examples on the Internet about using PHP to generate QR codes. There are mainly two methods:
The first type: google open api, as follows:
$urlToEncode="http://blog.it985.com"; generateQRfromGoogle($urlToEncode); function generateQRfromGoogle($chl, $widhtHeight = '150', $EC_level = 'L', $margin = '0') { $url = urlencode($url); echo '<img src="http://chart.apis.google.com/chart?chs='.$widhtHeight.'x'.$widhtHeight.'&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$chl.'" alt="QR code" />'; }
Attachment: API interface address https://developers.google.com/chart/infographics/docs/qr_codes
Second: Use PHP class library PHP QR CODE
Official address: http://phpqrcode.sourceforge.net/
Download address: http://sourceforge.net/projects/phpqrcode/
You can also click here to download from this site.
How to use:
<?php // include这两个文件之一: /* qrlib.php for full version (also you have to provide all library files form package plus cache dir) OR phpqrcode.php for merged version (only one file, but slower and less accurate code because disabled cache and quicker masking configured) */ // 两句话解释: // 包含qrlib.php的话需要同其它文件放到一起:文件、文件夹。 // phpqrcode.php是合并后版本,只需要包含这个文件,但生成的图片速度慢而且不太准确 include('./phpqrcode/phpqrcode.php'); // 以下给出两种用法: // 创建一个二维码文件 QRcode::png('code data text', 'filename.png'); // creates file // 生成图片到浏览器 QRcode::png('some othertext 1234'); ?>
Attached is the official sample code address: http://phpqrcode.sourceforge.net/examples/index.php
Of course, there are other ways to generate QR codes, so I won’t introduce them one by one here.
Let’s talk about using EndroidQrCodeBundle to generate QR codes under Symfony:
1. Use composer to install
Copy code The code is as follows: composer require endroid/qrcode-bundle
2. Register in the kernel
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Endroid\Bundle\QrCodeBundle\EndroidQrCodeBundle(), ); }
3. Define access routes
EndroidQrCodeBundle: resource: "@EndroidQrCodeBundle/Controller/" type: annotation prefix: /qrcode
4. Configuration config.xml
endroid_qr_code: size: 100 padding: 10 extension: gif error_correction_level: high foreground_color: { r: 0, g: 0, b: 0, a: 0 } background_color: { r: 255, g: 255, b: 255, a: 0 } #label: "My label" #labelFontSize: 16
5. Use in twig
Normal text generation method:
<img src="{{ qrcode_url(message) }}" /> <img src="{{ qrcode_url(message, extension='png') }}" /> <img src="{{ qrcode_url(message, size=150) }}" />
Link generation method:
Copy code The code is as follows:
The permanent address of this article: http://blog.it985.com/12340.html
This article comes from IT985 blog. Please indicate the source and corresponding link when reprinting.
Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP graphics and image operation skills", "Summary of excellent PHP development framework", "ThinkPHP introductory tutorial" and "codeigniter introductory tutorial"
I hope this article will be helpful to everyone’s PHP program design based on the Symfony framework.