Home > Backend Development > PHP Tutorial > How to generate QR code with Symfony, symfony generation_PHP tutorial

How to generate QR code with Symfony, symfony generation_PHP tutorial

WBOY
Release: 2016-07-12 08:59:22
Original
896 people have browsed it

How Symfony generates QR codes, symfony generates

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&#63;chs='.$widhtHeight.'x'.$widhtHeight.'&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$chl.'" alt="QR code" />';
}

Copy after login

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:

<&#63;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');
&#63;>

Copy after login

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

<&#63;php
// app/AppKernel.php
public function registerBundles()
{
  $bundles = array(
    // ...
    new Endroid\Bundle\QrCodeBundle\EndroidQrCodeBundle(),
  );
}

Copy after login

3. Define access routes

EndroidQrCodeBundle:
  resource:  "@EndroidQrCodeBundle/Controller/"
  type:    annotation
  prefix:   /qrcode

Copy after login

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

Copy after login

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) }}" />

Copy after login

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.

Articles you may be interested in:

  • Symfony2 Detailed explanation of installing third-party Bundles instances
  • Symfony2 Detailed explanation of using third-party library Upload to create image upload instances
  • Symfony2 Graphical tutorial on the configuration method under Nginx
  • Symfony2 installation method (2 methods)
  • Symfony2 session usage example analysis
  • Symfony learning ten-minute entry classic tutorial
  • Nginx rewrite rule configuration for PHP’s Symfony and CodeIgniter framework
  • Symfony data verification method example analysis
  • Symfony form and page implementation skills
  • Symfony page Detailed explanation of basic creation examples
  • Classic introductory tutorial for high-performance PHP framework Symfony2
  • How to export data in CSV files in symfony
  • Symfony2 implements the method of built-in data in doctrine

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1098966.htmlTechArticleSymfony's method of generating QR codes, symfony generation This article describes how Symfony generates QR codes. I share it with you for your reference. The details are as follows: Nowadays, you can find a lot of information on the Internet...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template