Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #23 from mape2k/master

Add rotate and showtext option
  • Loading branch information...
commit 54c6cd721c1e62a7fcd88e217a052406759f4144 2 parents bd7e48e + 45ce63b
Daniel O'Connor CloCkWeRX authored
20 Image/Barcode2.php
@@ -60,6 +60,14 @@ class Image_Barcode2
60 60 const BARCODE_EAN8 = 'ean8';
61 61 const BARCODE_POSTNET = 'postnet';
62 62
  63 + /**
  64 + * Rotation type
  65 + */
  66 + const ROTATE_NONE = '0';
  67 + const ROTATE_RIGHT = '90';
  68 + const ROTATE_UTURN = '180';
  69 + const ROTATE_LEFT = '270';
  70 +
63 71
64 72 /**
65 73 * Draws a image barcode
@@ -80,6 +88,8 @@ class Image_Barcode2
80 88 * browser, or be returned.
81 89 * @param integer $height The image height
82 90 * @param integer $width The image width
  91 + * @param boolean $showText The text should be placed under barcode
  92 + * @param integer $rotation The rotation angle
83 93 *
84 94 * @return resource The corresponding gd image resource
85 95 *
@@ -94,7 +104,9 @@ public static function draw($text,
94 104 $imgtype = Image_Barcode2::IMAGE_PNG,
95 105 $bSendToBrowser = true,
96 106 $height = 60,
97   - $width = 1
  107 + $width = 1,
  108 + $showText = true,
  109 + $rotation = Image_Barcode2::ROTATE_NONE
98 110 ) {
99 111 //Make sure no bad files are included
100 112 if (!preg_match('/^[a-zA-Z0-9]+$/', $type)) {
@@ -124,10 +136,16 @@ public static function draw($text,
124 136 }
125 137
126 138 $obj->setBarcode($text);
  139 + $obj->setShowText($showText);
127 140
128 141 $obj->validate();
129 142 $img = $obj->draw();
130 143
  144 + // Rotate image on demand
  145 + if ($rotation !== self::ROTATE_NONE) {
  146 + $img = imagerotate($img, $rotation, 0);
  147 + }
  148 +
131 149 if ($bSendToBrowser) {
132 150 // Send image to browser
133 151 switch ($imgtype) {
23 Image/Barcode2/Common.php
@@ -41,6 +41,7 @@ class Image_Barcode2_Common
41 41 protected $barcodethinwidth;
42 42 protected $barcodethickwidth;
43 43 protected $fontsize = 2;
  44 + protected $showText;
44 45
45 46 /**
46 47 * @var Image_Barcode2_Writer
@@ -107,6 +108,28 @@ public function getBarcode()
107 108 return $this->barcode;
108 109 }
109 110
  111 + /**
  112 + * Set if text will be placed under the barcode
  113 + *
  114 + * @param boolean $showText The text should be placed under barcode
  115 + *
  116 + * @return void
  117 + */
  118 + public function setShowText($showText)
  119 + {
  120 + $this->showText = $showText;
  121 + }
  122 +
  123 + /**
  124 + * Get if text will be placed under the barcode
  125 + *
  126 + * @return boolean
  127 + */
  128 + public function getShowText()
  129 + {
  130 + return $this->showText;
  131 + }
  132 +
110 133 public function setFontSize($size)
111 134 {
112 135 $this->fontsize = $size;
18 Image/Barcode2/Driver/Code128.php
@@ -294,14 +294,16 @@ public function draw()
294 294
295 295
296 296 // First, print the image, centered across the bottom.
297   - $writer->imagestring(
298   - $img,
299   - $fontsize,
300   - $barcodewidth / 2 - strlen($text) / 2 * ($writer->imagefontwidth($fontsize)),
301   - $this->getBarcodeHeight() + $writer->imagefontheight($fontsize) / 2,
302   - $text,
303   - $black
304   - );
  297 + if ($this->showText) {
  298 + $writer->imagestring(
  299 + $img,
  300 + $fontsize,
  301 + $barcodewidth / 2 - strlen($text) / 2 * ($writer->imagefontwidth($fontsize)),
  302 + $this->getBarcodeHeight() + $writer->imagefontheight($fontsize) / 2,
  303 + $text,
  304 + $black
  305 + );
  306 + }
305 307
306 308 // We set $xpos to 10 so we start bar printing after
307 309 // position 10 to simulate the 10 pixel "Quiet Zone"
18 Image/Barcode2/Driver/Code39.php
@@ -188,14 +188,16 @@ public function draw()
188 188 }
189 189
190 190 // draw text under barcode
191   - $writer->imagestring(
192   - $img,
193   - $fontsize,
194   - ($barcode_len - $font_width * strlen($text)) / 2,
195   - $this->getBarcodeHeight() - $font_height,
196   - $text,
197   - $black
198   - );
  191 + if ($this->showText) {
  192 + $writer->imagestring(
  193 + $img,
  194 + $fontsize,
  195 + ($barcode_len - $font_width * strlen($text)) / 2,
  196 + $this->getBarcodeHeight() - $font_height,
  197 + $text,
  198 + $black
  199 + );
  200 + }
199 201
200 202
201 203 return $img;
57 Image/Barcode2/Driver/Ean13.php
@@ -190,16 +190,19 @@ public function draw()
190 190 $xpos = 0;
191 191
192 192 // print first digit
193   - $writer->imagestring(
194   - $img,
195   - $fontsize,
196   - $xpos,
197   - $this->getBarcodeHeight(),
198   - $key,
199   - $black
200   - );
  193 + if ($this->showText) {
  194 + $writer->imagestring(
  195 + $img,
  196 + $fontsize,
  197 + $xpos,
  198 + $this->getBarcodeHeight(),
  199 + $key,
  200 + $black
  201 + );
  202 +
  203 + $xpos = $writer->imagefontwidth($fontsize) + 1;
  204 + };
201 205
202   - $xpos = $writer->imagefontwidth($fontsize) + 1;
203 206
204 207 // Draws the left guard pattern (bar-space-bar)
205 208 // bar
@@ -230,14 +233,16 @@ public function draw()
230 233 for ($idx = 1; $idx < 7; $idx ++) {
231 234 $value = substr($text, $idx, 1);
232 235
233   - $writer->imagestring(
234   - $img,
235   - $fontsize,
236   - $xpos + 1,
237   - $this->getBarcodeHeight(),
238   - $value,
239   - $black
240   - );
  236 + if ($this->showText) {
  237 + $writer->imagestring(
  238 + $img,
  239 + $fontsize,
  240 + $xpos + 1,
  241 + $this->getBarcodeHeight(),
  242 + $value,
  243 + $black
  244 + );
  245 + }
241 246
242 247 foreach ($this->_codingmap[$value][$set_array[$idx - 1]] as $bar) {
243 248 if ($bar) {
@@ -287,14 +292,16 @@ public function draw()
287 292 for ($idx = 7; $idx < 13; $idx ++) {
288 293 $value = substr($text, $idx, 1);
289 294
290   - $writer->imagestring(
291   - $img,
292   - $fontsize,
293   - $xpos + 1,
294   - $this->getBarcodeHeight(),
295   - $value,
296   - $black
297   - );
  295 + if ($this->showText) {
  296 + $writer->imagestring(
  297 + $img,
  298 + $fontsize,
  299 + $xpos + 1,
  300 + $this->getBarcodeHeight(),
  301 + $value,
  302 + $black
  303 + );
  304 + }
298 305
299 306 foreach ($this->_codingmap[$value]['C'] as $bar) {
300 307 if ($bar) {
38 Image/Barcode2/Driver/Ean8.php
@@ -186,14 +186,18 @@ public function draw()
186 186
187 187 for ($idx = 0; $idx < 4; $idx ++) {
188 188 $value = substr($text, $idx, 1);
189   - $writer->imagestring(
190   - $img,
191   - $fontsize,
192   - $xpos + 1,
193   - $this->getBarcodeHeight(),
194   - $value,
195   - $black
196   - );
  189 +
  190 + if ($this->showText) {
  191 + $writer->imagestring(
  192 + $img,
  193 + $fontsize,
  194 + $xpos + 1,
  195 + $this->getBarcodeHeight(),
  196 + $value,
  197 + $black
  198 + );
  199 + }
  200 +
197 201 foreach ($this->_codingmap[$value]['A'] as $bar) {
198 202 if ($bar) {
199 203 $writer->imagefilledrectangle(
@@ -244,14 +248,16 @@ public function draw()
244 248 for ($idx = 4; $idx < 8; $idx ++) {
245 249 $value = substr($text, $idx, 1);
246 250
247   - $writer->imagestring(
248   - $img,
249   - $fontsize,
250   - $xpos + 1,
251   - $this->getBarcodeHeight(),
252   - $value,
253   - $black
254   - );
  251 + if ($this->showText) {
  252 + $writer->imagestring(
  253 + $img,
  254 + $fontsize,
  255 + $xpos + 1,
  256 + $this->getBarcodeHeight(),
  257 + $value,
  258 + $black
  259 + );
  260 + }
255 261
256 262 foreach ($this->_codingmap[$value]['C'] as $bar) {
257 263 if ($bar) {
75 Image/Barcode2/Driver/Upca.php
@@ -172,14 +172,16 @@ public function draw()
172 172 $xpos = 0;
173 173
174 174 // print first digit
175   - $writer->imagestring(
176   - $img,
177   - $fontsize,
178   - $xpos,
179   - $this->getBarcodeHeight(),
180   - $key,
181   - $black
182   - );
  175 + if ($this->showText) {
  176 + $writer->imagestring(
  177 + $img,
  178 + $fontsize,
  179 + $xpos,
  180 + $this->getBarcodeHeight(),
  181 + $key,
  182 + $black
  183 + );
  184 + }
183 185 $xpos = $writer->imagefontwidth($fontsize) + 1;
184 186
185 187
@@ -229,14 +231,17 @@ public function draw()
229 231 // Draw left $text contents
230 232 for ($idx = 1; $idx < 6; $idx ++) {
231 233 $value = substr($text, $idx, 1);
232   - $writer->imagestring(
233   - $img,
234   - $fontsize,
235   - $xpos + 1,
236   - $this->getBarcodeHeight(),
237   - $value,
238   - $black
239   - );
  234 +
  235 + if ($this->showText) {
  236 + $writer->imagestring(
  237 + $img,
  238 + $fontsize,
  239 + $xpos + 1,
  240 + $this->getBarcodeHeight(),
  241 + $value,
  242 + $black
  243 + );
  244 + }
240 245
241 246 foreach ($this->_codingmap[$value]['L'] as $bar) {
242 247 if ($bar) {
@@ -286,14 +291,18 @@ public function draw()
286 291 // Draw right $text contents
287 292 for ($idx = 6; $idx < 11; $idx ++) {
288 293 $value = substr($text, $idx, 1);
289   - $writer->imagestring(
290   - $img,
291   - $fontsize,
292   - $xpos + 1,
293   - $this->getBarcodeHeight(),
294   - $value,
295   - $black
296   - );
  294 +
  295 + if ($this->showText) {
  296 + $writer->imagestring(
  297 + $img,
  298 + $fontsize,
  299 + $xpos + 1,
  300 + $this->getBarcodeHeight(),
  301 + $value,
  302 + $black
  303 + );
  304 + }
  305 +
297 306 foreach ($this->_codingmap[$value]['R'] as $bar) {
298 307 if ($bar) {
299 308 $writer->imagefilledrectangle(
@@ -357,14 +366,16 @@ public function draw()
357 366
358 367
359 368 // Print Check Digit
360   - $writer->imagestring(
361   - $img,
362   - $fontsize,
363   - $xpos + 1,
364   - $this->getBarcodeHeight(),
365   - $value,
366   - $black
367   - );
  369 + if ($this->showText) {
  370 + $writer->imagestring(
  371 + $img,
  372 + $fontsize,
  373 + $xpos + 1,
  374 + $this->getBarcodeHeight(),
  375 + $value,
  376 + $black
  377 + );
  378 + }
368 379
369 380 return $img;
370 381 }
57 Image/Barcode2/Driver/Upce.php
@@ -178,14 +178,16 @@ public function draw()
178 178 $xpos = 0;
179 179
180 180 // print first digit
181   - $writer->imagestring(
182   - $img,
183   - $fontsize,
184   - $xpos,
185   - $this->getBarcodeHeight(),
186   - $key,
187   - $black
188   - );
  181 + if ($this->showText) {
  182 + $writer->imagestring(
  183 + $img,
  184 + $fontsize,
  185 + $xpos,
  186 + $this->getBarcodeHeight(),
  187 + $key,
  188 + $black
  189 + );
  190 + }
189 191 $xpos = $writer->imagefontwidth($fontsize) + 1;
190 192
191 193
@@ -220,15 +222,18 @@ public function draw()
220 222 $checkdigit = substr($text, 7, 1);
221 223 for ($idx = 1; $idx < 7; $idx ++) {
222 224 $value = substr($text, $idx, 1);
223   - $writer->imagestring(
224   - $img,
225   - $fontsize,
226   - $xpos + 1,
227   - $this->getBarcodeHeight(),
228   - $value,
229   - $black
230   - );
231   -
  225 +
  226 + if ($this->showText) {
  227 + $writer->imagestring(
  228 + $img,
  229 + $fontsize,
  230 + $xpos + 1,
  231 + $this->getBarcodeHeight(),
  232 + $value,
  233 + $black
  234 + );
  235 + }
  236 +
232 237 if ($this->_paritypattern[$checkdigit][$idx-1] == 1) {
233 238 foreach ($this->_codingmap[$value]['E'] as $bar) {
234 239 if ($bar) {
@@ -304,14 +309,16 @@ public function draw()
304 309
305 310
306 311 // Print Check Digit
307   - $writer->imagestring(
308   - $img,
309   - $fontsize,
310   - $xpos + 1,
311   - $this->getBarcodeHeight(),
312   - $checkdigit,
313   - $black
314   - );
  312 + if ($this->showText) {
  313 + $writer->imagestring(
  314 + $img,
  315 + $fontsize,
  316 + $xpos + 1,
  317 + $this->getBarcodeHeight(),
  318 + $checkdigit,
  319 + $black
  320 + );
  321 + }
315 322
316 323 return $img;
317 324 }

0 comments on commit 54c6cd7

Please sign in to comment.
Something went wrong with that request. Please try again.