/var/www/hkosl.com/oneMotorChunHing_delete20240201/html/bcclass/BCGisbn.barcode.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/**
 * BCGisbn.barcode.php
 *--------------------------------------------------------------------
 *
 * Sub-Class - ISBN-10 and ISBN-13
 *
 * You can provide an ISBN with 10 digits with or without the checksum.
 * You can provide an ISBN with 13 digits with or without the checksum.
 * Calculate the ISBN based on the EAN-13 encoding.
 *
 * The checksum is always displayed.
 *
 *--------------------------------------------------------------------
 * Revision History
 * v2.0.0    23 apr    2008    Jean-Sébastien Goupil    New Version Update
 * v1.2.5    13 apr    2007    Jean-Sébastien Goupil
 *--------------------------------------------------------------------
 * $Id: BCGisbn.barcode.php,v 1.7 2010/02/14 00:24:07 jsgoupil Exp $
 *--------------------------------------------------------------------
 * Copyright (C) Jean-Sebastien Goupil
 * http://www.barcodephp.com
 */
include_once('BCGean13.barcode.php');

class 
BCGisbn extends BCGean13 {
    const 
GS1_AUTO 0;
    const 
GS1_PREFIX978 1;
    const 
GS1_PREFIX979 2;

    private 
$gs1;
    private 
$isbn_created;
    private 
$isbn_text;
    private 
$isbn_textfont;

    private 
$forceOffsetY;

    
/**
     * Constructor
     *
     * @param int $gs1
     * @param string $isbn_text
     * @param mixed $textfont2 BCGFont or int
     */
    
public function __construct($gs1 self::GS1_AUTO$isbn_text BCGBarcode1D::AUTO_LABEL$isbn_font null) {
        
parent::__construct();

        
$this->forceOffsetY false;

        
$this->setISBNFont($isbn_font);
        
$this->setISBNText($isbn_text);
        
$this->setGS1($gs1);
    }

    
/**
     * Saves Text
     *
     * @param string $text
     */
    
public function parse($text) {
        
BCGBarcode1D::parse(str_replace(array('-'' '), ''$text));

        
$this->createISBNText();
        
$this->setLabelOffset();
    }

    
/**
     * Sets the first numbers of the barcode.
     *  - GS1_AUTO: Adds 978 before the code
     *  - GS1_PREFIX978: Adds 978 before the code
     *  - GS1_PREFIX979: Adds 979 before the code
     *
     * @param int $gs1
     */
    
public function setGS1($gs1) {
        
$gs1 = (int)$gs1;
        if(
$gs1 !== self::GS1_AUTO && $gs1 !== self::GS1_PREFIX978 && $gs1 !== self::GS1_PREFIX979) {
            
$gs1 self::GS1_AUTO;
        }
        
$this->gs1 $gs1;
    }

    
/**
     * Sets the font to write the ISBN text on the top of the barcode.
     *
     * @param mixed $font
     */
    
public function setISBNFont($font) {
        if(
$font instanceof BCGFont) {
            
$this->isbn_textfont = clone $font;
        } else if(
$font === null) {
            
$this->isbn_textfont null;
        } else {
            
$this->isbn_textfont intval($font);
        }

        
$this->setLabelOffset();
    }

    
/**
     * Sets the text for the ISBN value.
     *
     * @param string $isbn_text
     */
    
public function setISBNText($text) {
        
$this->isbn_text $text;

        
$this->createISBNText();
        
$this->setLabelOffset();
    }

    public function 
setOffsetY($offsetY) {
        
parent::setOffsetY($offsetY);

        
// We force the offsetY, so we won't position based on the label position
        
$this->forceOffsetY true;
    }

    public function 
getMaxSize() {
        
// We must compute the first digit calculating the width
        
$null null;
        
$this->isLengthCorrect($null);

        return 
parent::getMaxSize();
    }

    protected function 
setLabelOffset() {
        
parent::setLabelOffset();

        if(!empty(
$this->isbn_created) && !$this->forceOffsetY) {
            if(
$this->getISBNFont() instanceof BCGFont) {
                
$f = clone $this->getISBNFont();
                
$f->setText($this->isbn_created);

                
$val = ($f->getHeight() - $f->getUnderBaseline()) / $this->scale BCGBarcode1D::SIZE_SPACING_FONT;
                
$this->offsetY $val;
            } elseif(
$this->getISBNFont() !== 0) {
                
$val = (imagefontheight($this->getISBNFont()) + 2) / $this->scale;
                
$this->offsetY $val;
            }
        }
    }

    protected function 
isCharsAllowed(&$im) {
        
$c strlen($this->text);
        
// Special case, if we have 10 digits, the last one can be X
        
if($c === 10) {
            if(
array_search($this->text[9], $this->keys) === false && $this->text[9] !== 'X') {
                
$this->drawError($im'Char \'' $this->text[9] . '\' not allowed.');
                return 
false;
            }
            
// Drop the last char
            
$this->text substr($this->text09);
        }

        return 
parent::isCharsAllowed($im);
    }

    protected function 
isLengthCorrect(&$im) {
        
$c strlen($this->text);

        
// If we have 13 chars just flush the last one
        
if($c === 13) {
            
$this->text substr($this->text012);
            return 
true;
        } elseif(
$c === 12) {
            return 
true;
        } elseif(
$c === || $c === 10) {
            if(
$c === 10) {
                
// Before dropping it, we check if it's legal
                
if(array_search($this->text[9], $this->keys) === false && $this->text[9] !== 'X') {
                    return 
false;
                }
                
$this->text substr($this->text09);
            }
            if(
$this->gs1 === self::GS1_AUTO || $this->gs1 === self::GS1_PREFIX978) {
                
$this->text '978' $this->text;
            } elseif(
$this->gs1 === self::GS1_PREFIX979) {
                
$this->text '979' $this->text;
            }
            
// We changed the start, recalculate the offset label
            
parent::setLabelOffset();

            return 
true;
        } else {
            if(
$im !== null) {
                
$this->drawError($im'Must provide 9, 10, 12 or 13 digits.');
            }
            return 
false;
        }
    }

    
/**
     * Overloaded method for drawing special label
     *
     * @param resource $im
     */
    
protected function drawText($im) {
        
parent::drawText($im);

        if(
strlen($this->isbn_created) > 0) {
            
$pA $this->getMaxSize();
            
$pB BCGBarcode1D::getMaxSize();
            
$w =  $pA[0] - $pB[0];

            if(
$this->getISBNFont() instanceof BCGFont) {
                
$textfont = clone $this->getISBNFont();
                
$textfont->setText($this->isbn_created);
                
$xPosition = ($w 2) - ($textfont->getWidth() / 2) + $this->offsetX $this->scale;
                
$yPosition $this->offsetY $this->scale BCGBarcode1D::SIZE_SPACING_FONT;
                
$textfont->draw($im$this->colorFg->allocate($im), $xPosition$yPosition);
            } elseif(
$this->getISBNFont() !== 0) {
                
$xPosition = ($w 2) - (strlen($this->isbn_created) / 2) * imagefontwidth($this->getISBNFont()) + $this->offsetX $this->scale;
                
$yPosition $this->offsetY $this->scale BCGBarcode1D::SIZE_SPACING_FONT imagefontheight($this->getISBNFont());
                
imagestring($im$this->getISBNFont(), $xPosition$yPosition$this->isbn_created$this->colorFg->allocate($im));
            }
        }
    }

    private function 
getISBNFont() {
        if(
$this->isbn_textfont === null) {
            return 
$this->textfont;
        } else {
            return 
$this->isbn_textfont;
        }
    }

    private function 
createISBNText() {
        if(
$this->isbn_text === BCGBarcode1D::AUTO_LABEL && !empty($this->text)) { 
            
// We try to create the ISBN Text... the hyphen really depends the ISBN agency.
            // We just put one before the checksum and one after the GS1 if present.
            
$c strlen($this->text);
            if(
$c === 12 || $c === 13) {
                
// If we have 13 characters now, just transform it temporarily to find the checksum...
                // Further in the code we take care of that anyway.
                
$lastCharacter '';
                if(
$c === 13) {
                    
$lastCharacter $this->text[12];
                    
$this->text substr($this->text012);
                }
                
                
$checksum $this->processChecksum();
                
$this->isbn_created 'ISBN ' substr($this->text03) . '-' substr($this->text39) . '-' $checksum;
                
                
// Put the last character back
                
if($c === 13) {
                    
$this->text .= $lastCharacter;
                }
            } elseif(
$c === || $c === 10) {
                
$checksum 0;
                for(
$i 10$i >= 2$i--) {
                    
$checksum += $this->text[10 $i] * $i;
                }
                
$checksum 11 $checksum 11;
                if(
$checksum === 10) {
                    
$checksum 'X';
                }
                
$this->isbn_created 'ISBN ' substr($this->text09) . '-' $checksum;
            } else {
                
$this->isbn_created '';
            }
        } else {
            
$this->isbn_created $this->isbn_text;
        }
    }
};
?>