php二维码生成

php生成二维码的几种方式,用起来挺方便,实现的几种方法都经过测试通过
1.利用google开放接口
 get方法实现方式一:
 

  1. $urlToEncode="163.com";  
  2. generateQRfromGoogle($urlToEncode);  
  3. function generateQRfromGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0')  {  
  4.      $url = urlencode($url);  
  5.      return  '<img src="http://chart.apis.google.com/chart?chs='.$widhtHeight.'x'.$widhtHeight.'&cht=qr&    chld='.$EC_level.'|'.$margin.'&chl='.$chl.'" alt="QR code" widhtHeight="'.$size.'" widhtHeight="'.$size.'"/>';  
  6. }  


post方法实现方式:

  1. $width = 300;  
  2. $height = 300;  
  3. $string = "163.com";  
  4. function qrcode($width,$height,$string)  
  5. {  
  6.     $post_data = array();  
  7.     $post_data['cht'] = 'qr';  
  8.     $post_data['chs'] = $width."x".$height;  
  9.     $post_data['chl'] = $string;  
  10.     $post_data['choe'] = "UTF-8";  
  11.     $url = "http://chart.apis.google.com/chart";  
  12.     $data_Array = array();  
  13.     foreach($post_data as $key => $value)  
  14.     {  
  15.         $data_Array[] = $key.'='.$value;  
  16.     }  
  17.     $data = implode("&",$data_Array);  
  18.     //echo $data;  
  19.     $ch = curl_init();  
  20.     curl_setopt($ch, CURLOPT_POST, 1);  
  21.     curl_setopt($ch, CURLOPT_HEADER, 0);  
  22.     curl_setopt($ch, CURLOPT_URL, $url);      
  23.     curl_setopt($ch, CURLOPT_POSTFIELDS,$data);  
  24.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  25.     $result = curl_exec($ch);  
  26.     
  27.     //echo "<img src =\"data:image/png;base64,".base64_encode($result)."\" >"; 注意,不写header的写法  
  28.   
  29.      return $result;  
  30. }  
  31.   
  32. header("Content-type:image/png");  
  33. echo qrcode($width,$height,$string);  

2.利用php类库PHP QR Code来实现
首先下载类库包 
地址:http://phpqrcode.sourceforge.net/
下载:http://sourceforge.net/projects/phpqrcode/
 

  1. <?  
  2. include "./phpqrcode/phpqrcode.php";  
  3. $value="http://www.weste.net";  
  4. $errorCorrectionLevel = "L";  
  5. $matrixPointSize = "4";  
  6. QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);  
  7. exit;  
  8. ?>
小广告:) 百度影音资源站-http://v5v6s.com

猜你喜欢

转载自blog.csdn.net/ycl111/article/details/10505271