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

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed May 5, 2016
2 parents df65072 + 47ee229 commit b8bf76a
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 18 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- '5.4'
- '5.5'
- '5.6'
- '7.0'
- hhvm

install:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
PDF417 Changelog
================

0.2.0 (2016-05-05)
------------------

* Added support for new formats in ImageRenderer (tif, bmp, data-url) (#1)
* Fixed bug when encoded text started with numbers or bytes (#2)

0.1.2 (2014-12-26)
------------------

Expand Down
67 changes: 64 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ Requirements
Requires the following components:

* PHP >= 5.4
* Fileinfo extension
* GD extension
* PHP extensions: bcmath, fileinfo, gd

Installation
------------

Add it to your `composer.json` file:

```
composer require bigfish/pdf417 ~0.1
composer require bigfish/pdf417 ^1.0.0
```

Usage overview
Expand Down Expand Up @@ -59,6 +58,68 @@ $renderer = new SvgRenderer([
$svg = $renderer->render($data);
```

ImageRenderer
-------------

Renders the barcode to an image using [Intervention Image](http://image.intervention.io/)

Render function returns an instance of `Intervention\Image\Image`.

#### Options

Option | Default | Description
------- | ------- | -----------
format | png | Output format, one of: `jpg`, `png`, `gif`, `tif`, `bmp`, `data-url`
quality | 90 | Jpeg encode quality (1-10)
scale | 3 | Scale of barcode elements (1-20)
ratio | 3 | Height to width ration of barcode elements (1-10)
padding | 20 | Padding in pixels (0-50)
color | #000000 | Foreground color as a hex code
bgColor | #ffffff | Background color as a hex code

#### Examples

```php
$pdf417 = new PDF417();
$data = $pdf417->encode("My hovercraft is full of eels");

// Create a PNG image, red on green background, extra big
$renderer = new ImageRenderer([
'format' => 'png',
'color' => '#FF0000',
'color' => '#00FF00',
'scale' => 10,
]);

$image = $renderer->render($data);
$image->save('hovercraft.png');
```

The `data-url` format is not like the others, it returns a base64 encoded PNG
image which can be used in an image `src` or in CSS. When you create an image
using this format:

```php
$pdf417 = new PDF417();
$data = $pdf417->encode('My nipples explode with delight');

$renderer = new ImageRenderer([
'format' => 'data-url'
]);
$img = $renderer->render($data);
```

You can use it directly in your HTML or CSS code:

```html
<img src="<?= $img->encoded ?>" />
```

And this will be rendered as:
```html
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA.... " />
```

Thanks
------

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"require": {
"php": ">=5.4",
"ext-gd": "*",
"ext-bcmath": "*",
"intervention/image": "~2.0"
},
"require-dev": {
Expand Down
8 changes: 5 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/DataEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public function __construct()
public function encode($data)
{
$chains = $this->splitToChains($data);
$addSwitchCode = false;

// Add a switch code at the beginning if the first encoder to be used
// is not the text encoder. Decoders by default start decoding as text.
$firstEncoder = $chains[0][1];
$addSwitchCode = (!($firstEncoder instanceof Encoders\TextEncoder));

$codes = [];
foreach ($chains as $chEnc) {
Expand Down
2 changes: 1 addition & 1 deletion src/Renderers/AbstractRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(array $options = [])
// Merge options with defaults, ignore options not specified in
// defaults.
foreach ($options as $key => $value) {
if (isset($this->options[$key])) {
if (array_key_exists($key, $this->options)) {
$this->options[$key] = $value;
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Renderers/ImageRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class ImageRenderer extends AbstractRenderer
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'tif' => 'image/tiff',
'bmp' => 'image/bmp',
'data-url' => null,
];

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

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

/**
* {@inheritdoc}
*
* @return \Intervention\Image\Image
*/
public function render(BarcodeData $data)
{
Expand Down
14 changes: 9 additions & 5 deletions src/Renderers/SvgRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class SvgRenderer extends AbstractRenderer
protected $options = [
'scale' => 3,
'ratio' => 3,
'color' => "#000"
'color' => "#000",
'description' => null,
];

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

// Add description node
$text = "HUB3 barcode generated by http://hub3.bigfish.software/";
$description = $doc->createElement("description", $text);
$svg->appendChild($description);
// Add description node if defined
$desc = $options['description'];
if (!empty($desc)) {
$svg->appendChild(
$doc->createElement("description", $desc)
);
}

// Create the group
$group = $doc->createElement("g");
Expand Down
32 changes: 32 additions & 0 deletions tests/DataEncoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace BigFish\PDF417\Tests;

use BigFish\PDF417\DataEncoder;
use BigFish\PDF417\Encoders\TextEncoder;
use BigFish\PDF417\Encoders\NumberEncoder;
use BigFish\PDF417\Encoders\ByteEncoder;

class DataEncoderTest extends \PHPUnit_Framework_TestCase
{
public function testStartingSwitchCodeWordIsAddedOnlyForText()
{
$encoder = new DataEncoder();

// When starting with text, the first code word does not need to be the switch
$cw1 = $encoder->encode("ABC123")[0];
$this->assertNotEquals($cw1, TextEncoder::SWITCH_CODE_WORD);

// When starting with numbers, we do need to switch
$cw1 = $encoder->encode("123ABC")[0];
$this->assertEquals($cw1, NumberEncoder::SWITCH_CODE_WORD);

// Also with bytes
$cw1 = $encoder->encode("\x0B")[0];
$this->assertEquals($cw1, ByteEncoder::SWITCH_CODE_WORD);

// Alternate bytes switch code when number of bytes is divisble by 6
$cw1 = $encoder->encode("\x0B\x0B\x0B\x0B\x0B\x0B")[0];
$this->assertEquals($cw1, ByteEncoder::SWITCH_CODE_WORD_ALT);
}
}
15 changes: 15 additions & 0 deletions tests/Renderers/ImageRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ public function testContentType()
$actual = $renderer->getContentType();
$expected = "image/gif";
$this->assertSame($expected, $actual);

$renderer = new ImageRenderer(["format" => "bmp"]);
$actual = $renderer->getContentType();
$expected = "image/bmp";
$this->assertSame($expected, $actual);

$renderer = new ImageRenderer(["format" => "tif"]);
$actual = $renderer->getContentType();
$expected = "image/tiff";
$this->assertSame($expected, $actual);

// data-url format does not have a mime type
$renderer = new ImageRenderer(["format" => "data-url"]);
$actual = $renderer->getContentType();
$this->assertNull($actual);
}

/**
Expand Down
21 changes: 20 additions & 1 deletion tests/Renderers/SvgRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testRender()

// Check document structure
$xml = simplexml_load_string($string);
$this->assertTrue(isset($xml->description));
$this->assertFalse(isset($xml->description));
$this->assertTrue(isset($xml->g));

foreach($xml->g as $group) {
Expand All @@ -48,4 +48,23 @@ public function testRender()
}
}
}

public function testRenderWithDescription()
{
$data = new BarcodeData();
$data->codes = [[true, false],[false, true]];

$desc = "today is a good day to generate barcodes";

$renderer = new SvgRenderer([
'description' => $desc
]);

$string = $renderer->render($data);

// Check description exists
$xml = simplexml_load_string($string);
$this->assertTrue(isset($xml->description));
$this->assertSame($desc, strval($xml->description));
}
}

0 comments on commit b8bf76a

Please sign in to comment.