温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Symfony如何生成二维码

发布时间:2021-08-30 16:05:00 来源:亿速云 阅读:148 作者:小新 栏目:开发技术

这篇文章主要介绍了Symfony如何生成二维码,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

具体如下:

现在网上能搜到很多关于使用PHP生成二维码的例子,主要是两种方法:

第一种:google开放api,如下:

$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" />';
}

附:API接口地址 https://developers.google.com/chart/infographics/docs/qr_codes

第二种:使用PHP类库 PHP QR CODE

官方地址:http://phpqrcode.sourceforge.net/

下载地址:http://sourceforge.net/projects/phpqrcode/

也可点击此处本站下载。

使用方法:

<?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');
?>

附官方示例代码地址:http://phpqrcode.sourceforge.net/examples/index.php

当然,还有其他方法生成二维码,这里就不一一介绍了。

下面我们说一下在Symfony下使用EndroidQrCodeBundle生成二维码:

1、使用composer安装

composer require endroid/qrcode-bundle

2、在kernel中注册

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

3、定义访问路由

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

4、配置 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、在twig中使用

普通文本生成方式:

<img src="{{ qrcode_url(message) }}" />
<img src="{{ qrcode_url(message, extension='png') }}" />
<img src="{{ qrcode_url(message, size=150) }}" />

链接生成方式:

<img src="{{ qrcode_data_uri(message, size=200, padding=10) }}" />

感谢你能够认真阅读完这篇文章,希望小编分享的“Symfony如何生成二维码”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI