Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
3
 
4
/**
5
 * Image_Barcode_postnet class
6
 *
7
 * Renders PostNet barcodes
8
 *
9
 * PHP versions 4
10
 *
11
 * LICENSE: This source file is subject to version 3.0 of the PHP license
12
 * that is available through the world-wide-web at the following URI:
13
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
14
 * the PHP License and are unable to obtain it through the web, please
15
 * send a note to license@php.net so we can mail you a copy immediately.
16
 *
17
 * @category   Image
18
 * @package    Image_Barcode
19
 * @author     Josef "Jeff" Sipek <jeffpc@optonline.net>
20
 * @copyright  2005 Josef "Jeff" Sipek
21
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
22
 * @version    CVS: $Id: postnet.php 304741 2010-10-25 09:14:17Z clockwerx $
23
 * @link       http://pear.php.net/package/Image_Barcode
24
 */
25
 
26
 /*
27
  * Note:
28
  *
29
  * The generated barcode must fit the following criteria to be useable
30
  * by the USPS scanners:
31
  *
32
  * When printed, the dimensions should be:
33
  *
34
  *     tall bar:       1/10 inches     = 2.54 mm
35
  *  short bar:      1/20 inches     = 1.27 mm
36
  *  density:        22 bars/inch    = 8.66 bars/cm
37
  */
38
 
39
require_once 'Image/Barcode.php';
40
 
41
 
42
/**
43
 * Image_Barcode_postnet class
44
 *
45
 * Package which provides a method to create PostNet barcode using GD library.
46
 *
47
 * @category   Image
48
 * @package    Image_Barcode
49
 * @author     Josef "Jeff" Sipek <jeffpc@optonline.net>
50
 * @copyright  2005 Josef "Jeff" Sipek
51
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
52
 * @version    CVS: $Id: postnet.php 304741 2010-10-25 09:14:17Z clockwerx $
53
 * @link       http://pear.php.net/package/Image_Barcode
54
 */
55
class Image_Barcode_postnet extends Image_Barcode
56
{
57
    /**
58
     * Barcode type
59
     * @var string
60
     */
61
    var $_type = 'postnet';
62
 
63
    /**
64
     * Bar short height
65
     *
66
     * @var integer
67
     */
68
    var $_barshortheight = 7;
69
 
70
    /**
71
     * Bar tall height
72
     *
73
     * @var integer
74
     */
75
    var $_bartallheight = 15;
76
 
77
    /**
78
     * Bar width / scaling factor
79
     *
80
     * @var integer
81
     */
82
    var $_barwidth = 2;
83
 
84
    /**
85
     * Coding map
86
     * @var array
87
     */
88
    var $_coding_map = array(
89
           '0' => '11000',
90
           '1' => '00011',
91
           '2' => '00101',
92
           '3' => '00110',
93
           '4' => '01001',
94
           '5' => '01010',
95
           '6' => '01100',
96
           '7' => '10001',
97
           '8' => '10010',
98
           '9' => '10100'
99
        );
100
 
101
    /**
102
     * Draws a PostNet image barcode
103
     *
104
     * @param  string $text     A text that should be in the image barcode
105
     * @param  string $imgtype  The image type that will be generated
106
     *
107
     * @return image            The corresponding Interleaved 2 of 5 image barcode
108
     *
109
     * @access public
110
     *
111
     * @author Josef "Jeff" Sipek <jeffpc@optonline.net>
112
     * @since  Image_Barcode 0.3
113
     */
114
 
115
    function draw($text, $imgtype = 'png')
116
    {
117
        $text = trim($text);
118
 
119
        if (!preg_match('/[0-9]/', $text)) {
120
            return;
121
        }
122
 
123
        // Calculate the barcode width
124
        $barcodewidth = (strlen($text)) * 2 * 5 * $this->_barwidth + $this->_barwidth*3;
125
 
126
        // Create the image
127
        $img = ImageCreate($barcodewidth, $this->_bartallheight);
128
 
129
        // Alocate the black and white colors
130
        $black = ImageColorAllocate($img, 0, 0, 0);
131
        $white = ImageColorAllocate($img, 255, 255, 255);
132
 
133
        // Fill image with white color
134
        imagefill($img, 0, 0, $white);
135
 
136
        // Initiate x position
137
        $xpos = 0;
138
 
139
        // Draws the leader
140
        imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $this->_bartallheight, $black);
141
        $xpos += 2*$this->_barwidth;
142
 
143
        // Draw $text contents
144
        for ($idx = 0; $idx < strlen($text); $idx++) {
145
            $char  = substr($text, $idx, 1);
146
 
147
            for ($baridx = 0; $baridx < 5; $baridx++) {
148
                $elementheight = (substr($this->_coding_map[$char], $baridx, 1)) ?  0 : $this->_barshortheight;
149
                imagefilledrectangle($img, $xpos, $elementheight, $xpos + $this->_barwidth - 1, $this->_bartallheight, $black);
150
                $xpos += 2*$this->_barwidth;
151
            }
152
        }
153
 
154
        // Draws the trailer
155
        imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $this->_bartallheight, $black);
156
        $xpos += 2*$this->_barwidth;
157
 
158
        return $img;
159
    } // function create
160
 
161
} // class
162
?>