Skip to content
This repository was archived by the owner on Apr 11, 2023. It is now read-only.

Commit b8bf76a

Browse files
committed
Merge branch 'release/0.2.0'
2 parents df65072 + 47ee229 commit b8bf76a

File tree

12 files changed

+168
-18
lines changed

12 files changed

+168
-18
lines changed

.travis.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
language: php
22

33
php:
4-
- 5.4
5-
- 5.5
6-
- 5.6
4+
- '5.4'
5+
- '5.5'
6+
- '5.6'
7+
- '7.0'
78
- hhvm
89

910
install:

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
PDF417 Changelog
22
================
33

4+
0.2.0 (2016-05-05)
5+
------------------
6+
7+
* Added support for new formats in ImageRenderer (tif, bmp, data-url) (#1)
8+
* Fixed bug when encoded text started with numbers or bytes (#2)
9+
410
0.1.2 (2014-12-26)
511
------------------
612

README.md

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@ Requirements
1313
Requires the following components:
1414

1515
* PHP >= 5.4
16-
* Fileinfo extension
17-
* GD extension
16+
* PHP extensions: bcmath, fileinfo, gd
1817

1918
Installation
2019
------------
2120

2221
Add it to your `composer.json` file:
2322

2423
```
25-
composer require bigfish/pdf417 ~0.1
24+
composer require bigfish/pdf417 ^1.0.0
2625
```
2726

2827
Usage overview
@@ -59,6 +58,68 @@ $renderer = new SvgRenderer([
5958
$svg = $renderer->render($data);
6059
```
6160

61+
ImageRenderer
62+
-------------
63+
64+
Renders the barcode to an image using [Intervention Image](http://image.intervention.io/)
65+
66+
Render function returns an instance of `Intervention\Image\Image`.
67+
68+
#### Options
69+
70+
Option | Default | Description
71+
------- | ------- | -----------
72+
format | png | Output format, one of: `jpg`, `png`, `gif`, `tif`, `bmp`, `data-url`
73+
quality | 90 | Jpeg encode quality (1-10)
74+
scale | 3 | Scale of barcode elements (1-20)
75+
ratio | 3 | Height to width ration of barcode elements (1-10)
76+
padding | 20 | Padding in pixels (0-50)
77+
color | #000000 | Foreground color as a hex code
78+
bgColor | #ffffff | Background color as a hex code
79+
80+
#### Examples
81+
82+
```php
83+
$pdf417 = new PDF417();
84+
$data = $pdf417->encode("My hovercraft is full of eels");
85+
86+
// Create a PNG image, red on green background, extra big
87+
$renderer = new ImageRenderer([
88+
'format' => 'png',
89+
'color' => '#FF0000',
90+
'color' => '#00FF00',
91+
'scale' => 10,
92+
]);
93+
94+
$image = $renderer->render($data);
95+
$image->save('hovercraft.png');
96+
```
97+
98+
The `data-url` format is not like the others, it returns a base64 encoded PNG
99+
image which can be used in an image `src` or in CSS. When you create an image
100+
using this format:
101+
102+
```php
103+
$pdf417 = new PDF417();
104+
$data = $pdf417->encode('My nipples explode with delight');
105+
106+
$renderer = new ImageRenderer([
107+
'format' => 'data-url'
108+
]);
109+
$img = $renderer->render($data);
110+
```
111+
112+
You can use it directly in your HTML or CSS code:
113+
114+
```html
115+
<img src="<?= $img->encoded ?>" />
116+
```
117+
118+
And this will be rendered as:
119+
```html
120+
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA.... " />
121+
```
122+
62123
Thanks
63124
------
64125

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"require": {
1616
"php": ">=5.4",
1717
"ext-gd": "*",
18+
"ext-bcmath": "*",
1819
"intervention/image": "~2.0"
1920
},
2021
"require-dev": {

composer.lock

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DataEncoder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ public function __construct()
4242
public function encode($data)
4343
{
4444
$chains = $this->splitToChains($data);
45-
$addSwitchCode = false;
45+
46+
// Add a switch code at the beginning if the first encoder to be used
47+
// is not the text encoder. Decoders by default start decoding as text.
48+
$firstEncoder = $chains[0][1];
49+
$addSwitchCode = (!($firstEncoder instanceof Encoders\TextEncoder));
4650

4751
$codes = [];
4852
foreach ($chains as $chEnc) {

src/Renderers/AbstractRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __construct(array $options = [])
1515
// Merge options with defaults, ignore options not specified in
1616
// defaults.
1717
foreach ($options as $key => $value) {
18-
if (isset($this->options[$key])) {
18+
if (array_key_exists($key, $this->options)) {
1919
$this->options[$key] = $value;
2020
}
2121
}

src/Renderers/ImageRenderer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class ImageRenderer extends AbstractRenderer
1515
'jpg' => 'image/jpeg',
1616
'png' => 'image/png',
1717
'gif' => 'image/gif',
18+
'tif' => 'image/tiff',
19+
'bmp' => 'image/bmp',
20+
'data-url' => null,
1821
];
1922

2023
protected $options = [
@@ -35,7 +38,7 @@ public function validateOptions()
3538
$errors = [];
3639

3740
$format = $this->options['format'];
38-
if (!isset($this->formats[$format])) {
41+
if (!array_key_exists($format, $this->formats)) {
3942
$formats = implode(", ", array_keys($this->formats));
4043
$errors[] = "Invalid option \"format\": \"$format\". Expected one of: $formats.";
4144
}
@@ -92,6 +95,8 @@ public function getContentType()
9295

9396
/**
9497
* {@inheritdoc}
98+
*
99+
* @return \Intervention\Image\Image
95100
*/
96101
public function render(BarcodeData $data)
97102
{

src/Renderers/SvgRenderer.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class SvgRenderer extends AbstractRenderer
1515
protected $options = [
1616
'scale' => 3,
1717
'ratio' => 3,
18-
'color' => "#000"
18+
'color' => "#000",
19+
'description' => null,
1920
];
2021

2122
/**
@@ -73,10 +74,13 @@ public function render(BarcodeData $data)
7374
$svg->setAttribute("version", "1.1");
7475
$svg->setAttribute("xmlns", "http://www.w3.org/2000/svg");
7576

76-
// Add description node
77-
$text = "HUB3 barcode generated by http://hub3.bigfish.software/";
78-
$description = $doc->createElement("description", $text);
79-
$svg->appendChild($description);
77+
// Add description node if defined
78+
$desc = $options['description'];
79+
if (!empty($desc)) {
80+
$svg->appendChild(
81+
$doc->createElement("description", $desc)
82+
);
83+
}
8084

8185
// Create the group
8286
$group = $doc->createElement("g");

tests/DataEncoderTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace BigFish\PDF417\Tests;
4+
5+
use BigFish\PDF417\DataEncoder;
6+
use BigFish\PDF417\Encoders\TextEncoder;
7+
use BigFish\PDF417\Encoders\NumberEncoder;
8+
use BigFish\PDF417\Encoders\ByteEncoder;
9+
10+
class DataEncoderTest extends \PHPUnit_Framework_TestCase
11+
{
12+
public function testStartingSwitchCodeWordIsAddedOnlyForText()
13+
{
14+
$encoder = new DataEncoder();
15+
16+
// When starting with text, the first code word does not need to be the switch
17+
$cw1 = $encoder->encode("ABC123")[0];
18+
$this->assertNotEquals($cw1, TextEncoder::SWITCH_CODE_WORD);
19+
20+
// When starting with numbers, we do need to switch
21+
$cw1 = $encoder->encode("123ABC")[0];
22+
$this->assertEquals($cw1, NumberEncoder::SWITCH_CODE_WORD);
23+
24+
// Also with bytes
25+
$cw1 = $encoder->encode("\x0B")[0];
26+
$this->assertEquals($cw1, ByteEncoder::SWITCH_CODE_WORD);
27+
28+
// Alternate bytes switch code when number of bytes is divisble by 6
29+
$cw1 = $encoder->encode("\x0B\x0B\x0B\x0B\x0B\x0B")[0];
30+
$this->assertEquals($cw1, ByteEncoder::SWITCH_CODE_WORD_ALT);
31+
}
32+
}

0 commit comments

Comments
 (0)