C128 PHP 中的条形码脚本



我想在codeigniter框架中添加代码128自动条形码。我为此使用了 http://davidscotttufts.com/2009/03/31/how-to-create-barcodes-in-php/,但它正在生成代码 128B 格式。我也使用过 http://www.barcodephp.com/en/download 但无法在代码点火器框架中正确添加类。任何人都可以帮助我或建议我 code128 自动条形码 php 脚本.

提前致谢

<?php

    // Get thickness parameter from $_GET:
    $thickness = ( isset( $_GET["thickness"] ) ? $_GET[ "thickness" ] : 2 );
    $text = (isset($_GET["text"])?$_GET["text"]:"00000000024");
    //$text=(int)$text;
    $size = (isset($_GET["size"])?$_GET["size"]:"30");
    $orientation = (isset($_GET["orientation"])?$_GET["orientation"]:"horizontal");
    $code_type = (isset($_GET["codetype"])?$_GET["codetype"]:"code128");
    $code_string = "";
    // Translate the $text into barcode the correct $code_type
    if ( strtolower($code_type) == "code128" ) {
        $code_array = array(
                    0       => '11011001100',
                    1       => '11001101100',
                    2       => '11001100110',
                    3       => '10010011000',
                    4       => '10010001100',
                    5       => '10001001100',
                    6       => '10011001000',
                    7       => '10011000100',
                    8       => '10001100100',
                    9       => '11001001000',
                    10      => '11001000100',
                    11      => '11000100100',
                    12      => '10110011100',
                    13      => '10011011100',
                    14      => '10011001110',
                    15      => '10111001100',
                    16      => '10011101100',
                    17      => '10011100110',
                    18      => '11001110010',
                    19      => '11001011100',
                    20      => '11001001110',
                    21      => '11011100100',
                    22      => '11001110100',
                    23      => '11101101110',
                    24      => '11101001100',
                    25      => '11100101100',   
                    26      => '11100100110',
                    27      => '11101100100',
                    28      => '11100110100',
                    29      => '11100110010',
                    30      => '11011011000',
                    31      => '11011000110',
                    32      => '11000110110',
                    33      => '10100011000',
                    34      => '10001011000',
                    35      => '10001000110',
                    36      => '10110001000',
                    37      => '10001101000',
                    38      => '10001100010',
                    39      => '11010001000',
                    40      => '11000101000',
                    41      => '11000100010',
                    42      => '10110111000',
                    43      => '10110001110',
                    44      => '10001101110',
                    45      => '10111011000',
                    46      => '10111000110',
                    47      => '10001110110',
                    48      => '11101110110',
                    49      => '11010001110',
                    50      => '11000101110',
                    51      => '11011101000',
                    52      => '11011100010',
                    53      => '11011101110',
                    54      => '11101011000',
                    55      => '11101000110',
                    56      => '11100010110',
                    57      => '11101101000',
                    58      => '11101100010',
                    59      => '11100011010',
                    60      => '11101111010',
                    61      => '11001000010',
                    62      => '11110001010',
                    63      => '10100110000',
                    64      => '10100001100',
                    65      => '10010110000',
                    66      => '10010000110',
                    67      => '10000101100',
                    68      => '10000100110',
                    69      => '10110010000',
                    70      => '10110000100',
                    71      => '10011010000',
                    72      => '10011000010',
                    73      => '10000110100',
                    74      => '10000110010',
                    75      => '11000010010',
                    76      => '11001010000',
                    77      => '11110111010',
                    78      => '11000010100',
                    79      => '10001111010',
                    80      => '10100111100',
                    81      => '10010111100',
                    82      => '10010011110',
                    83      => '10111100100',
                    84      => '10011110100',
                    85      => '10011110010',
                    86      => '11110100100',
                    87      => '11110010100',
                    88      => '11110010010',
                    89      => '11011011110',
                    90      => '11011110110',
                    91      => '11110110110',
                    92      => '10101111000',
                    93      => '10100011110',
                    94      => '10001011110',
                    95      => '10111101000',
                    96      => '10111100010',
                    97      => '11110101000',
                    98      => '11110100010',
                    99      => '10111011110',    
                    100     => '10111101110',   
                    101     => '11101011110',   
                    102     => '11110101110',    
                    'c'     => '10111011110',
                    'b'     => '10111101110',
                    'a'     => '11101011110',
                    'A'     => '11010000100',
                    'B'     => '11010010000',
                    'C'     => '11010011100',
                    'S'     => '1100011101011'
            );
     $code = array();
     $strCode;

    // settype($code,'string');
        //$this->sourceCode = $code;
        $nbKr = strlen($code);
        $strCode = '';
        for( $i=0; $i<$nbKr; $i++ ) {
            $code[$i] = substr($code, $i, 1);
        }
        $strCode = $code_array['B']; // Start
        $checksum = 104 ;
        $j = 1 ;
        for( $i=0; $i<$nbKr; $i++ ) {
            $tmp = ord($code[$i]) - 32 ;
            $checksum += ( $j++ * $tmp ) ;
            $strCode .= $code_array[$tmp];
        }
        $checksum %= 103 ;
        $strCode .= $code_array[$checksum];
        $strCode .= $code_array['S']; // Stop
        $strCode = $strCode;
        $tmp = strlen($strCode) + 20;
        $width = $tmp;
    } 

    // Pad the edges of the barcode
    $code_length = 20;
    for ( $i=1; $i <= strlen($code_string); $i++ )
        $code_length = $code_length + (integer)(substr($code_string,($i-1),1));
    if ( strtolower($orientation) == "horizontal" ) {
        //$img_width = $code_length;
        $img_width = $code_length * $thickness;
        $img_height = $size;
    } else {
        $img_width = $size;
        $img_height = $code_length;
    }
    $image = imagecreate($img_width, $img_height);
    $black = imagecolorallocate ($image, 0, 0, 0);
    $white = imagecolorallocate ($image, 255, 255, 255);
    imagefill( $image, 0, 0, $white );
    //$location = 10;
    $location = 10 * $thickness;
    for ( $position = 1 ; $position <= strlen($code_string); $position++ ) {
        //$cur_size = $location + ( substr($code_string, ($position-1), 1);
        $cur_size = $location + ( substr($code_string, ($position-1), 1) *$thickness);
        if ( strtolower($orientation) == "horizontal" )
            imagefilledrectangle( $image, $location, 0, $cur_size, $img_height, ($position % 2 == 0 ? $white : $black) );
        else
            imagefilledrectangle( $image, 0, $location, $img_width, $cur_size, ($position % 2 == 0 ? $white : $black) );
        $location = $cur_size;
    }
    // Draw barcode to the screen
    header ('Content-type: image/png');
    imagepng($image);
    imagedestroy($image);
?>

您实际上可以按照 https://github.com/tecnickcom/tc-lib-barcode/blob/develop/example/index.php 中的示例进行操作

例如:

$bobj = $barcode->getBarcodeObj('QRCODE,H', 'hello world', -4, -4, 'black');
echo "<img alt="Embedded Image" src="data:image/png;base64,".base64_encode($bobj->getPngData())."" />";

或者,如果要以其他格式打印条形码:

echo $bobj->getHtmlDiv();

这些示例使用 tc-lib-barcode (https://github.com/tecnickcom/tc-lib-barcode) PHP 软件库来生成线性和二维条形码。

源代码完全符合PSR-2标准,可以使用Composer轻松添加到PHP项目中。

原始代码已经从TCPDF移植和重构,并且已经在数十亿个文档中使用。

相关内容

  • 没有找到相关文章

最新更新