萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> 利用phpqrcode生成二維碼實例代碼

利用phpqrcode生成二維碼實例代碼

   QR碼詳細原理可以參考QR維基百科,中文版介紹比較少,英文版介紹很全面,推薦看英文版的。

  Description

  PHP QR Code is open source (LGPL) library for generating QR Code, 2-dimensional barcode. Based on libqrencode C library, provides API for creating QR Code barcode images (PNG, JPEG thanks to GD2). Implemented purely in PHP.

  Features

  •Supports QR Code versions (size) 1-40

  •Numeric, Alphanumeric, 8-bit and Kanji encoding.

  •Implemented purely in PHP, no external dependencies except GD2

  •Exports to PNG, JPEG images, also exports as bit-table

  •TCPDF 2-D barcode API integration

  •Easy to configure

  •Data cache for calculation speed-up

  •Debug data dump, error logging, time benchmarking

  •Provided merge tool helps deploy library as a one file

  •new! API documentation

  •new! Over 30 detailed examples

  •100% Open Source, LGPL Licensed

  Demo

  項目地址:http://sourceforge.net/projects/phpqrcode/

  Generate fixed size images

  * phpqrcode本身未提供固定大小二維碼的設置,請參考 http://sourceforge.net/p/phpqrcode/discussion/1111884/thread/f739531a/

 代碼如下  

--- original/phpqrcode/qrimage.php       2010-08-18 07:06:24.000000000 -0700
+++ qrimage.php 2012-07-13 00:51:57.000000000 -0700
@@ -86,10 +86,15 @@
                 }
             }
-            $target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
-            ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
+
+            $targetW = (defined('IMAGE_WIDTH') ? IMAGE_WIDTH : $imgW * $pixelPerPoint );
+            $targetH = (defined('IMAGE_HEIGHT') ? IMAGE_HEIGHT : $imgH * $pixelPerPoint );
+
+            $target_image =ImageCreate($targetW, $targetH);
+
+            ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $targetW, $targetH, $imgW, $imgH);
             ImageDestroy($base_image);
             return $target_image;
         }
-    }
No newline at end of file
+    }

用法,設置生成的二維碼寬高為 500px

define('IMAGE_WIDTH', 500);
define('IMAGE_HEIGHT', 500);
QRcode::png('http://www.111cn.net/)');

copyright © 萬盛學電腦網 all rights reserved