PHP生成一维码、二维码方法
来源:互联网 发布:钢结构软件 编辑:程序博客网 时间:2024/06/13 20:36
- <?php
- $code = $_GET['code'];
- if ($code != "") {
- if (!is_numeric($code)) die('输入的不是数字');
- if (strlen($code) < 12 || strlen($code) > 13) die('条码长度不正确');
- if (strlen($code) == 12) {
-
- $lsum = 0;
- $rsum = 0;
- for($i=1; $i<=strlen($code); $i++) {
- if($i % 2) {
- $lsum += (int)$code[$i-1];
- }else{
- $rsum += (int)$code[$i-1];
- }
- }
- $tsum = $lsum + $rsum * 3;
- $chkdig = 10 - ($tsum % 10);
- if ($chkdig == 10) $chkdig = 0;
- $code .= $chkdig;
- }
-
-
- $start = '101';
-
- $end = '101';
-
- $center = '01010';
-
- $Guide = array(0=>'AAAAAA','AABABB','AABBAB','AABBBA','ABAABB','ABBAAB','ABBBAA','ABABAB','ABABBA','ABBABA');
-
-
- $Lencode = array("A" => array('0001101','0011001','0010011','0111101','0100011','0110001','0101111','0111011','0110111','0001011'),
- "B" => array('0100111','0110011','0011011','0100001','0011101','0111001','0000101','0010001','0001001','0010111'));
-
- $Rencode = array('1110010','1100110','1101100','1000010','1011100','1001110','1010000','1000100','1001000','1110100');
-
-
-
-
- $barcode = $start;
-
-
- for($i=1; $i<=6; $i++) {
- $barcode .= $Lencode[$Guide[$code[0]][($i-1)]][$code[$i]];
- }
-
-
- $barcode .= $center;
-
-
- for($i=7; $i<13; $i++) {
- $barcode .= $Rencode[$code[($i)]];
- }
-
-
- $barcode .= $end;
-
-
- $width = 2;
- $height = 40;
-
-
- $increment = 10;
-
-
- $img = ImageCreate($width*95+60,$height+30);
-
-
- $fg = ImageColorAllocate($img, 0, 0, 0);
- $bg = ImageColorAllocate($img, 255, 255, 255);
-
-
- ImageFilledRectangle($img, 0, 0, $width*95+60, $height+30, $bg);
-
-
- for ($x=0; $x<strlen($barcode); $x++) {
-
-
- if (($x<4) || ($x>=45 && $x<50) || ($x>=92)) {
- $increment = 10;
- } else {
- $increment = 0;
- }
-
- if ($barcode[$x] == '1') {
- $color = $fg;
- } else {
- $color = $bg;
- }
- ImageFilledRectangle($img, ($x*$width)+30,5,($x+1)*$width+29,$height+$increment,$color);
- }
-
- ImageString($img, 5, 20, $height+5, $code[0], $fg);
- for ($x=0; $x<6; $x++) {
-
- ImageString($img, 5, $width*(8+$x*6)+30, $height+5, $code[$x+1], $fg);
-
- ImageString($img, 5, $width*(53+$x*6)+30, $height+5, $code[$x+7], $fg);
- }
-
- header("Content-Type: image/jpeg");
- ImageJPEG($img, "", 100);
- }
- ?>
-
- <form action="?">
- 输入EAN-13条形码(如果输入12位长度,将自动计算校验位)<input type="text" name="code"> <input type="submit" value="生成条码图片">
- </form>
![](http://static.blog.csdn.net/images/save_snippets.png)
二维码:[ Google API ]1、网址:
- <?php
- $urlToEncode="http://www.ioco.cn";
- 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.'" alt="QR code" widhtHeight="'.$size.'" widhtHeight="'.$size.'"/>';
- }
- ?>
2、名片:- <?php
- $vname = 'chenfan';
- $vtel = '13506330633';
- generateQRfromGoogle($vname,$vtel);
-
- function generateQRfromGoogle($vname,$vtel,$widhtHeight ='150',$EC_level='L',$margin='0')
- {
- if($vname&&$vtel){
- $chl = "BEGIN:VCARD\nVERSION:3.0".
- "\nFN:$vname".
- "\nTEL:$vtel".
- "\nEND:VCARD";
- echo '<img src="http://chart.apis.google.com/chart?chs='.$widhtHeight.'x'.$widhtHeight.'&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.urlencode($chl).'" alt="QR code" widhtHeight="'.$size.'" widhtHeight="'.$size.'"/>';
- }
- }
- ?>
0 0