本文實例講述了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.'" style="color: rgb(0, 102, 153); text-decoration: none;" target="_blank">本站下載。使用方法:
<?php// include這兩個文件之一:/*qrlib.php for full version (also you have to provide all library filesform package plus cache dir)OR phpqrcode.php for merged version (only one file,but slower and less accurate code because disabled cacheand 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.phppublic function registerBundles(){ $bundles = array( // ... new Endroid/Bundle/QrCodeBundle/EndroidQrCodeBundle(), );}3、定義訪問路由
EndroidQrCodeBundle: resource: "@EndroidQrCodeBundle/Controller/" type: annotation prefix: /qrcode4、配置 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: 165、在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) }}" />
本文永久地址:http://blog.it985.com/12340.html
本文出自 IT985博客 ,轉載時請注明出處及相應鏈接。
注:相關教程知識閱讀請移步到PHP教程頻道。