picqer / php-barcode-generator

Barcode generator in PHP that is easy to use, non-bloated and framework independent.
GNU Lesser General Public License v3.0
1.67k stars 402 forks source link

Is ITF-14 supported? #79

Closed marvelidea closed 4 years ago

marvelidea commented 5 years ago

Is ITF-14 supported?

joeybab3 commented 5 years ago

Just in case this question comes up again, did you find an answer?

marvelidea commented 5 years ago

Yes. I ported code from another ITF gen and built my own barcode_itf method

`protected function barcode_itf($code) {

    $chr = array();
    $chr['0'] = '11221';
    $chr['1'] = '21112';
    $chr['2'] = '12112';
    $chr['3'] = '22111';
    $chr['4'] = '11212';
    $chr['5'] = '21211';
    $chr['6'] = '12211';
    $chr['7'] = '11122';
    $chr['8'] = '21121';
    $chr['9'] = '12121';

    if(strlen($code) == 13){
        $total = 0;

        for($i=0; $i <= strlen($code)-1; $i++){
            $temp = intval($code.substr($i, 1));
            $total += $temp * (($i == 0 || $i % 2 == 0) ? 3 : 1);
        }

        $cs = $total % 10;
        $cs = 10 - $cs;
        if ($cs == 10)
            $cs = 0;

        $code += (string)$cs;
    }

    if(strlen($code) > 14 || strlen($code) < 13){
        throw new InvalidLengthException();
    }

    $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
    $k = 0;

    $pbegin = "1010";
    $pbeginarr = str_split($pbegin);

    foreach ($pbeginarr as $x){
        $t = ($x == '1') ? true : false;
        $w = 1;

        $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
        $bararray['maxw'] += $w;
        ++$k;
    }

    for($i=0; $i < strlen($code); $i+=2){

        if ( ! isset($chr[$code{$i}]) || ! isset($chr[$code{$i + 1}])) {
            throw new InvalidCharacterException();
        }

        $bars = true;
        $pbars = $chr[$code[$i]];
        $pspaces = $chr[$code[$i + 1]];
        $pmixed = "";

        while (strlen($pbars) > 0) {
            $pmixed .= $pbars[0].$pspaces[0];
            $pbars = substr($pbars, 1);
            $pspaces = substr($pspaces, 1);
        }

        $pmixedarr = str_split($pmixed);

        foreach ($pmixedarr as $x){
            if ($bars){
                $t = true;
                $w = ($x == '1') ? '1' : '2';
            }
            else
            {
                $t = false;
                $w = ($x == '1') ? '1' : '2';
            }

            $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
            $bararray['maxw'] += $w;
            $bars = !$bars;
            ++$k;
        }

    }

    $pend = "1101";
    $pendarr = str_split($pend);

    foreach ($pendarr as $x){
        $t = ($x == '1') ? true : false;
        $w = 1;

        $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
        $bararray['maxw'] += $w;
        ++$k;
    }

    return $bararray;

}`
joeybab3 commented 5 years ago

To get this to run I ended up placing the function in BarcodeGenerator.php and adding an ITF_14 constant.