zoukankan      html  css  js  c++  java
  • php生成条形码

     public function  bar_code(){
    $code = $_GET['code'];
    if ($code != "") {
    if (!is_numeric($code)) die('输入的不是数字');
    if (strlen($code) != 13) die('条码长度不正确');
    // 定义起始付
    $start = '101';
    // 定义中止符
    $end = '101';
    // 定义中间分隔符
    $center = '01010';
    // 定义左资料码
    $Guide = array(0=>'AAAAAA','AABABB','AABBAB','AABBBA','ABAABB','ABBAAB','ABBBAA','ABABAB','ABABBA','ABBABA');

    // 定义左侧码,分为“A”“B”两种
    $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'));
    // 定义右侧码,统一为“C”编码
    $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;

    // 创建方形(×95是因为整个条码共95个单元,+60+30是给条码图片周围留空白边框)
    $img = ImageCreate($width*95+60,$height+30); // 目前这个方形是透明的

    // “1”的颜色(黑)与“0”的颜色(白)
    $fg = ImageColorAllocate($img, 0, 0, 0);
    $bg = ImageColorAllocate($img, 255, 255, 255);

    // “0”的颜色(白色),填充整个方形
    ImageFilledRectangle($img, 0, 0, $width*95+60, $height+30, $bg);

    // 循环编码后的每一个单元,输出条码图形
    for ($x=0; $x<strlen($barcode); $x++) {
    // ($x<4) 为起始符,($x>=92)为中止符,($x>=45 && $x<50)为中间分隔符
    // 3个需要将高度增加
    if (($x<4) || ($x>=45 && $x<50) || ($x>=92)) {
    $increment = 10;
    } else {
    $increment = 0;
    }
    // 当编码值为“1”时,输出黑色;当编码值为“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);
    }

    $filename= $code .".jpg"; //一维码(条形码)存放地址
    // var_dump($filename);die;


    header("Content-Type: image/jpeg");
    $aa=ImageJPEG($img, $filename, 9);
    if($aa){
    return $filename;
    }else{
    return false;
    }
    //return $img;
    }

    }
  • 相关阅读:
    MySQL命名、设计及使用规范--------来自标点符的《MySQL命名、设计及使用规范》
    Mybatis 在 insert 之后想获取自增的主键 id
    MyBatis动态SQL foreach标签实现批量插入
    PowerDesigner设置表字段唯一约束
    Hadoop基本介绍
    oracle的行级触发器使用
    RFID-RC522、FM1702SL、M1卡初探
    PHP处理海量样本相似度聚类算法
    RFID(Radio Frequency Identification 射频识别)基本概念
    USB Keyboard Recorder
  • 原文地址:https://www.cnblogs.com/xiaoqiangjun/p/11888429.html
Copyright © 2011-2022 走看看