本文實(shí)例講述了Symfony生成二維碼的方法。分享給大家供大家參考,具體如下:
現(xiàn)在網(wǎng)上能搜到很多關(guān)于使用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這兩個(gè)文件之一:/*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是合并后版本,只需要包含這個(gè)文件,但生成的圖片速度慢而且不太準(zhǔn)確include('./phpqrcode/phpqrcode.php');// 以下給出兩種用法:// 創(chuàng)建一個(gè)二維碼文件QRcode::png('code data text', 'filename.png');// creates file// 生成圖片到瀏覽器QRcode::png('some othertext 1234');?> 附官方示例代碼地址:http://phpqrcode.sourceforge.net/examples/index.php
當(dāng)然,還有其他方法生成二維碼,這里就不一一介紹了。
下面我們說(shuō)一下在Symfony下使用EndroidQrCodeBundle生成二維碼:
1、使用composer安裝
復(fù)制代碼代碼如下: composer require endroid/qrcode-bundle
2、在kernel中注冊(cè)
<?php// app/AppKernel.phppublic function registerBundles(){ $bundles = array( // ... new Endroid/Bundle/QrCodeBundle/EndroidQrCodeBundle(), );} 3、定義訪問(wèn)路由
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) }}" /> 鏈接生成方式:
復(fù)制代碼代碼如下: <img src="{{ qrcode_data_uri(message, size=200, padding=10) }}" />
本文永久地址:http://blog.it985.com/12340.html
本文出自 IT985博客 ,轉(zhuǎn)載時(shí)請(qǐng)注明出處及相應(yīng)鏈接。注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。

















