Heim  >  Artikel  >  Backend-Entwicklung  >  php生成二维码实现代码

php生成二维码实现代码

巴扎黑
巴扎黑Original
2016-11-21 13:30:051109Durchsuche

二维码是二维条形码的一种,可以将网址、文字、照片等信息通过相应的编码算法编译成为一个方块形条码图案,手机用户可以通过摄像头和解码软件将相关信息重新解码并查看内容 
php类库PHP QR Code 
# 两句话解释: 
# 包含qrlib.php的话需要同其它文件放到一起:文件、文件夹。 
# phpqrcode.php是合并后版本,只需要包含这个文件,但生成的图片速度慢而且不太准确 
# 以下给出两种用法: 

<?php 
   include(&#39;./phpqrcode/phpqrcode.php&#39;); 
   // 二维码数据 
   $data = &#39;http://gz.altmi.com&#39;; 
   // 生成的文件名 
   $filename = $errorCorrectionLevel.&#39;|&#39;.$matrixPointSize.&#39;.png&#39;; 
   // 纠错级别:L、M、Q、H 
   $errorCorrectionLevel = &#39;L&#39;;  
   // 点的大小:1到10 
   $matrixPointSize = 4;  
   QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
?>
  
# 创建一个二维码文件 
QRcode::png(&#39;code data text&#39;, &#39;filename.png&#39;); 
// creates file 
  
# 生成图片到浏览器 
QRcode::png(&#39;some othertext 1234&#39;); 
// creates code image and outputs it directly into browser

如果生成有色彩的需要自己动手 
修改phpqrcode.php中QRimage类的image方法中颜色设置部分的代码,1.1.4版本的代码在第987、988行,一个前景色,一个背景色: 

$col[0] = ImageColorAllocate($base_image,255,255,255);
$col[1] = ImageColorAllocate($base_image,0,0,0);

即可 
利用 google开放api生成 

$urlToEncode="http://gz.altmi.com"; 
generateQRfromGoogle($urlToEncode); 
function generateQRfromGoogle($chl,$widhtHeight =&#39;150&#39;,$EC_level=&#39;L&#39;,$margin=&#39;0&#39;) 
{ 
    $url = urlencode($url);  
    echo &#39;<img src="http://chart.apis.google.com/chart?chs=&#39;.$widhtHeight.&#39;x&#39;.$widhtHeight.&#39;&cht=qr&chld=&#39;.$EC_level.&#39;|&#39;.$margin.&#39;&chl=&#39;.$chl.&#39;" alt="QR code" widhtHeight="&#39;.$size.&#39;" widhtHeight="&#39;.$size.&#39;"/>&#39;; 
}


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn