Subversion Repositories Web Services

Rev

Rev 56957 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
56957 fvs 1
<?
2
/*
3
 
4
 * Built-In Encoders
56961 fvs 5
 * Part of PHP-Barcode 0.4
56957 fvs 6
 
56961 fvs 7
 * (C) 2001,2002,2003,2004,2011 by Folke Ashberg <folke@ashberg.de>
56957 fvs 8
 
56961 fvs 9
 * The newest version can be found at http://www.ashberg.de/php-barcode
56957 fvs 10
 
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
 
25
 */
26
 
27
function barcode_gen_ean_sum($ean){
28
  $even=true; $esum=0; $osum=0;
29
  for ($i=strlen($ean)-1;$i>=0;$i--){
30
        if ($even) $esum+=$ean[$i];     else $osum+=$ean[$i];
31
        $even=!$even;
32
  }
33
  return (10-((3*$esum+$osum)%10))%10;
34
}
35
 
36
/* barcode_encode_ean(code [, encoding])
37
 *   encodes $ean with EAN-13 using builtin functions
38
 *
39
 *   return:
40
 *    array[encoding] : the encoding which has been used (EAN-13)
41
 *    array[bars]     : the bars
42
 *    array[text]     : text-positioning info
43
 */
44
function barcode_encode_ean($ean, $encoding = "EAN-13"){
45
    $digits=array(3211,2221,2122,1411,1132,1231,1114,1312,1213,3112);
46
    $mirror=array("000000","001011","001101","001110","010011","011001","011100","010101","010110","011010");
47
    $guards=array("9a1a","1a1a1","a1a");
48
 
49
    $ean=trim($ean);
56961 fvs 50
    if (preg_match("#[^0-9]#i",$ean)){
56957 fvs 51
        return array("text"=>"Invalid EAN-Code");
52
    }
53
    $encoding=strtoupper($encoding);
54
    if ($encoding=="ISBN"){
56961 fvs 55
        if (!preg_match("#^978#", $ean)) $ean="978".$ean;
56957 fvs 56
    }
56961 fvs 57
    if (preg_match("#^978#", $ean)) $encoding="ISBN";
56957 fvs 58
    if (strlen($ean)<12 || strlen($ean)>13){
59
        return array("text"=>"Invalid $encoding Code (must have 12/13 numbers)");
60
    }
61
 
62
    $ean=substr($ean,0,12);
63
    $eansum=barcode_gen_ean_sum($ean);
64
    $ean.=$eansum;
65
    $line=$guards[0];
66
    for ($i=1;$i<13;$i++){
67
        $str=$digits[$ean[$i]];
68
        if ($i<7 && $mirror[$ean[0]][$i-1]==1) $line.=strrev($str); else $line.=$str;
69
        if ($i==6) $line.=$guards[1];
70
    }
71
    $line.=$guards[2];
72
 
73
    /* create text */
74
    $pos=0;
75
    $text="";
76
    for ($a=0;$a<13;$a++){
77
        if ($a>0) $text.=" ";
78
        $text.="$pos:12:{$ean[$a]}";
79
        if ($a==0) $pos+=12;
80
        else if ($a==6) $pos+=12;
81
        else $pos+=7;
82
    }
83
 
84
    return array(
85
                "encoding" => $encoding,
86
                "bars" => $line,
87
                "text" => $text
88
                );
89
}
90
 
91
?>