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.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed Dec 26, 2014
2 parents 20c8754 + ec60307 commit 75524be
Show file tree
Hide file tree
Showing 10 changed files with 368 additions and 101 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
PDF417 Changelog
================

0.1.1 (2014-12-26)
------------------

* Added validation of options in renderers.
* Fixed a bug in ImageRenderer where padding was not in bgColor, but white.
* Upgraded Intrevention/Image to v2.
* Added 'quality' option to ImageRenderer (used for JPG only).

0.1.0 (2014-12-24)
------------------

Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
PDF 417 barcode generator
=========================

[![Build Status](https://travis-ci.org/ihabunek/pdf417-php.svg?branch=master)](https://travis-ci.org/ihabunek/pdf417-php) [![Latest Stable Version](https://poser.pugx.org/bigfish/pdf417-php/v/stable.svg)](https://packagist.org/packages/bigfish/pdf417-php) [![Total Downloads](https://poser.pugx.org/bigfish/pdf417-php/downloads.svg)](https://packagist.org/packages/bigfish/pdf417-php) [![Latest Unstable Version](https://poser.pugx.org/bigfish/pdf417-php/v/unstable.svg)](https://packagist.org/packages/bigfish/pdf417-php) [![License](https://poser.pugx.org/bigfish/pdf417-php/license.svg)](https://packagist.org/packages/bigfish/pdf417-php)
[![Build Status](https://travis-ci.org/ihabunek/pdf417-php.svg?branch=master)](https://travis-ci.org/ihabunek/pdf417-php) [![Latest Stable Version](https://poser.pugx.org/bigfish/pdf417/v/stable.svg)](https://packagist.org/packages/bigfish/pdf417) [![Total Downloads](https://poser.pugx.org/bigfish/pdf417/downloads.svg)](https://packagist.org/packages/bigfish/pdf417) [![License](https://poser.pugx.org/bigfish/pdf417/license.svg)](https://packagist.org/packages/bigfish/pdf417)

Requirements
------------

Requires the following components:

* PHP >= 5.4
* Fileinfo extension
* GD extension

Installation
------------
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
}
},
"require": {
"intervention/image": "~1.6"
"php": ">=5.4",
"ext-gd": "*",
"intervention/image": "~2.0"
},
"require-dev": {
"mockery/mockery": "@stable"
Expand Down
81 changes: 20 additions & 61 deletions composer.lock

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

55 changes: 55 additions & 0 deletions src/Renderers/AbstractRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace BigFish\PDF417\Renderers;

use BigFish\PDF417\BarcodeData;
use BigFish\PDF417\RendererInterface;

abstract class AbstractRenderer implements RendererInterface
{
/** Default options array. */
protected $options = [];

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])) {
$this->options[$key] = $value;
}
}

$errors = $this->validateOptions();
if (!empty($errors)) {
$errors = implode("\n", $errors);
throw new \InvalidArgumentException($errors);
}
}

/**
* Validates the options, throws an Exception on failure.
*
* @param array $options
* @return array An array of errors, empty if no errors.
*/
public function validateOptions()
{
return [];
}

/**
* Returns the MIME content type of the barcode generated by this renderer.
*
* @return string
*/
public abstract function getContentType();

/**
* Renders the barcode from the given data set.
*
* @param BarcodeData $data The barcode data.
* @return mixed Output format depends on the renderer.
*/
public abstract function render(BarcodeData $data);
}
102 changes: 78 additions & 24 deletions src/Renderers/ImageRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,78 +5,132 @@
use BigFish\PDF417\BarcodeData;
use BigFish\PDF417\RendererInterface;

use Intervention\Image\Image;
use Intervention\Image\ImageManager;
use Intervention\Image\Gd\Color;

class ImageRenderer implements RendererInterface
class ImageRenderer extends AbstractRenderer
{
/** Supported image formats and corresponding MIME types. */
private $formats = [
protected $formats = [
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
];

private $options = [
protected $options = [
'format' => 'png',
'quality' => 90,
'scale' => 3,
'ratio' => 3,
'padding' => 20,
'color' => "#000",
'bgColor' => "#fff",
'color' => "#000000",
'bgColor' => "#ffffff",
];

public function __construct(array $options)
/**
* {@inheritdoc}
*/
public function validateOptions()
{
// Merge given options with defaults
foreach ($options as $key => $value) {
if (isset($this->options[$key])) {
$this->options[$key] = $value;
}
}
$errors = [];

// Validate options
$format = $this->options['format'];
if (!isset($this->formats[$format])) {
throw new \InvalidArgumentException("Invalid image format: \"$format\".");
$formats = implode(", ", array_keys($this->formats));
$errors[] = "Invalid option \"format\": \"$format\". Expected one of: $formats.";
}

$scale = $this->options['scale'];
if (!is_numeric($scale) || $scale < 1 || $scale > 20) {
$errors[] = "Invalid option \"scale\": \"$scale\". Expected an integer between 1 and 20.";
}

$ratio = $this->options['ratio'];
if (!is_numeric($ratio) || $ratio < 1 || $ratio > 10) {
$errors[] = "Invalid option \"ratio\": \"$ratio\". Expected an integer between 1 and 10.";
}

$padding = $this->options['padding'];
if (!is_numeric($padding) || $padding < 0 || $padding > 50) {
$errors[] = "Invalid option \"padding\": \"$padding\". Expected an integer between 0 and 50.";
}

$quality = $this->options['quality'];
if (!is_numeric($quality) || $quality < 0 || $quality > 100) {
$errors[] = "Invalid option \"quality\": \"$quality\". Expected an integer between 0 and 50.";
}

// Check colors by trying to parse them
$color = $this->options['color'];
$bgColor = $this->options['bgColor'];

$gdColor = new Color();

try {
$gdColor->parse($color);
} catch (\Exception $ex) {
$errors[] = "Invalid option \"color\": \"$color\". Supported color formats: \"#000000\", \"rgb(0,0,0)\", or \"rgba(0,0,0,0)\"";
}

try {
$gdColor->parse($bgColor);
} catch (\Exception $ex) {
$errors[] = "Invalid option \"bgColor\": \"$bgColor\". Supported color formats: \"#000000\", \"rgb(0,0,0)\", or \"rgba(0,0,0,0)\"";
}

return $errors;
}

/**
* {@inheritdoc}
*/
public function getContentType()
{
$format = $this->options['format'];
return $this->formats[$format];
}

/**
* {@inheritdoc}
*/
public function render(BarcodeData $data)
{
$pixelGrid = $data->getPixelGrid();
$height = count($pixelGrid);
$width = count($pixelGrid[0]);

$options = $this->options;
// Extract options
$bgColor = $this->options['bgColor'];
$color = $this->options['color'];
$format = $this->options['format'];
$padding = $this->options['padding'];
$quality = $this->options['quality'];
$ratio = $this->options['ratio'];
$scale = $this->options['scale'];

$img = Image::canvas($width, $height, $options['bgColor']);
// Create a new image
$manager = new ImageManager();
$img = $manager->canvas($width, $height, $bgColor);

// Render the barcode
foreach ($pixelGrid as $y => $row) {
foreach ($row as $x => $value) {
if ($value) {
$img->pixel($options['color'], $x, $y);
$img->pixel($color, $x, $y);
}
}
}

// Apply scaling & aspect ratio
$width *= $options['scale'];
$height *= $options['scale'] * $options['ratio'];
$width *= $scale;
$height *= $scale * $ratio;
$img->resize($width, $height);

// Add padding
$width += 2 * $options['padding'];
$height += 2 * $options['padding'];
$img->resizeCanvas($width, $height, 'center', false, '#fff');
$width += 2 * $padding;
$height += 2 * $padding;
$img->resizeCanvas($width, $height, 'center', false, $bgColor);

return $img->encode($options['format']);
return $img->encode($format, $quality);
}
}
Loading

0 comments on commit 75524be

Please sign in to comment.