Skip to content

Commit fd18d04

Browse files
authored
Merge pull request #91 from picqer/v2
Version 2
2 parents 7a4c853 + 981b1ae commit fd18d04

File tree

89 files changed

+5852
-2963
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+5852
-2963
lines changed

.github/workflows/phpunit.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: PHP Composer
1+
name: phpunit
22

33
on: [push]
44

@@ -18,7 +18,7 @@ jobs:
1818
uses: shivammathur/setup-php@v1
1919
with:
2020
php-version: ${{ matrix.php-versions }}
21-
extensions: mbstring, gd
21+
extensions: mbstring, gd, bcmath, imagick
2222

2323
- name: Validate composer.json
2424
run: composer validate

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
.idea
2-
.DS_Store
31
vendor
42
composer.lock
5-
composer.phar
3+
composer.phar

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ php:
77

88
sudo: false
99

10+
before_script: yes | pecl install imagick
11+
1012
install: composer install --no-interaction --prefer-source
1113

1214
script: vendor/bin/phpunit

CHANGELOG.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## [Unreleased]
8+
9+
## [2.0.0] - 2020-01-11
10+
### Added
11+
- Introduced Barcode and BarcodeBar classes to standardise generator output.
12+
- Introduced methods to force use of GD or Imagick, see readme.
13+
- Loads of new tests added, including tests on Github Actions.
14+
15+
### Changed
16+
- Splitted all barcode types to different files.
17+
- Refactored a lot of code for better readability, stricter checking, and to be more efficient.
18+
- Merged JPG and PNG generators, because of duplicate code.
19+
20+
### Fixed
21+
- Fixed a bug in Codabar generation 2d1128f5222d9368fc6151d2b51801ea29ba1052
22+
- Do not draw multiple bars on the same position #74
23+
- Do not try to draw barcodes for empty strings #42
24+
- Fixed possible casting issue in Codabar #92
25+
26+
## [0.4.0] - 2019-12-31
27+
### Added
28+
- Added support for PHP 7.4, thanks to @pilif #80
29+
30+
## [0.3.0] - 2019-01-12
31+
### Added
32+
- SVG: Add viewBox attribute to allow svg scaling #68 by @cuchac
33+
- Adjust CODE_128 to handle odd number of digits #55 by @richayles
34+
35+
### Fixed
36+
- Bugfix update imagick function #51 by @Keinbockwurst
37+
38+
## [0.2.2] - 2017-09-28
39+
### Added
40+
- Raising exceptions if we cannot generate JPG or PNG because of missing libraries. Thanks @OskarStark
41+
42+
## [0.2.1] - 2016-10-24
43+
### Fixed
44+
- Bugfixes for wrong constant values.
45+
46+
## [0.2.0] - 2016-05-14
47+
### Added
48+
- This release adds exceptions to this package. Now it is easier to detect if the generated barcode is correct or not.
49+
50+
## [0.1.0] - 2015-08-13
51+
### Added
52+
- Everything. First release of this package.

Readme.md

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
# PHP Barcode Generator [![Build Status](https://travis-ci.org/picqer/php-barcode-generator.svg?branch=master)](https://travis-ci.org/picqer/php-barcode-generator) [![Total Downloads](https://poser.pugx.org/picqer/php-barcode-generator/downloads)](https://packagist.org/packages/picqer/php-barcode-generator)
1+
# PHP Barcode Generator
2+
[![Build Status](https://travis-ci.org/picqer/php-barcode-generator.svg?branch=master)](https://travis-ci.org/picqer/php-barcode-generator) [![Github Actions](https://github.com/picqer/php-barcode-generator/workflows/phpunit/badge.svg)](https://travis-ci.org/picqer/php-barcode-generator) [![Total Downloads](https://poser.pugx.org/picqer/php-barcode-generator/downloads)](https://packagist.org/packages/picqer/php-barcode-generator)
3+
24
This is an easy to use, non-bloated, framework independent, barcode generator in PHP.
35

46
It creates SVG, PNG, JPG and HTML images, from the most used 1D barcode standards.
57

6-
*The codebase is largely from the [TCPDF barcode generator](https://github.com/tecnickcom/TCPDF) by Nicola Asuni. This code is therefor licensed under LGPLv3. It is still a bit of a mess, bit I will clean it in the future. I do not expect the interface of this class will change during the clean ups.*
8+
*The codebase is based on the [TCPDF barcode generator](https://github.com/tecnickcom/TCPDF) by Nicola Asuni. This code is therefor licensed under LGPLv3.*
9+
10+
## No support for...
11+
We do not support any 2D barcodes, like QR codes. We also only generate the 'bars' part of a barcode. If you want text of the code below the barcode, you could add it later to the output of this package.
712

813
## Installation
914
Install through [composer](https://getcomposer.org/doc/00-intro.md):
@@ -18,16 +23,33 @@ If you want to generate PNG or JPG images, you need the GD library or Imagick in
1823
Initiate the barcode generator for the output you want, then call the ->getBarcode() routine as many times as you want.
1924

2025
```php
26+
<?php
27+
require 'vendor/autoload.php';
28+
29+
// This will output the barcode as HTML output to display in the browser
2130
$generator = new Picqer\Barcode\BarcodeGeneratorHTML();
2231
echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128);
2332
```
2433

25-
The ->getBarcode() routine accepts the following:
26-
- $code Data for the barcode
27-
- $type Type of barcode, use the constants defined in the class
28-
- $widthFactor Width is based on the length of the data, with this factor you can make the barcode bars wider than default
29-
- $totalHeight The total height of the barcode
30-
- $color Hex code of the foreground color
34+
The `getBarcode()` method accepts the following parameters:
35+
- `$barcode` String needed to encode in the barcode
36+
- `$type` Type of barcode, use the constants defined in the class
37+
- `$widthFactor` Width is based on the length of the data, with this factor you can make the barcode bars wider than default
38+
- `$height` The total height of the barcode in pixels
39+
- `$foregroundColor` Hex code as string, or array of RGB, of the colors of the bars (the foreground color)
40+
41+
Example of usage of all parameters:
42+
43+
```php
44+
<?php
45+
46+
require 'vendor/autoload.php';
47+
48+
$redColor = [255, 0, 0];
49+
50+
$generator = new Picqer\Barcode\BarcodeGeneratorPNG();
51+
file_put_contents('barcode.png', $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 3, 50, $redColor));
52+
```
3153

3254
## Image types
3355
```php
@@ -37,7 +59,11 @@ $generatorJPG = new Picqer\Barcode\BarcodeGeneratorJPG();
3759
$generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML();
3860
```
3961

40-
## Accepted types
62+
## Accepted barcode types
63+
These barcode types are supported. All types support different character sets or have mandatory lengths. Please see wikipedia for supported chars and lengths per type.
64+
65+
Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner support, variable length and most chars supported.
66+
4167
- TYPE_CODE_39
4268
- TYPE_CODE_39_CHECKSUM
4369
- TYPE_CODE_39E
@@ -69,10 +95,26 @@ $generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML();
6995
- TYPE_PHARMA_CODE
7096
- TYPE_PHARMA_CODE_TWO_TRACKS
7197

98+
[See example images for all supported barcode types](examples.md)
99+
100+
## A note about PNG and JPG images
101+
If you want to use PNG or JPG images, you need to install [Imagick](https://www.php.net/manual/en/intro.imagick.php) or the [GD library](https://www.php.net/manual/en/intro.image.php). This package will use Imagick if that is installed, or fall back to GD. If you have both installed but you want a specific method, you can use `$generator->useGd()` or `$generator->useImagick()` to force your preference.
102+
72103
## Examples
73-
Embedded PNG image in HTML:
74104

105+
### Embedded PNG image in HTML
75106
```php
76-
$generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
107+
$generator = new Picqer\Barcode\BarcodeGeneratorPNG();
77108
echo '<img src="data:image/png;base64,' . base64_encode($generator->getBarcode('081231723897', $generator::TYPE_CODE_128)) . '">';
78109
```
110+
111+
### Save JPG barcode to disk
112+
```php
113+
$generator = new Picqer\Barcode\BarcodeGeneratorJPG();
114+
file_put_contents('barcode.jpg', $generator->getBarcode('081231723897', $generator::TYPE_CODABAR));
115+
```
116+
117+
### Oneliner SVG output to disk
118+
```php
119+
file_put_contents('barcode.svg', (new Picqer\Barcode\BarcodeGeneratorSVG())->getBarcode('6825ME601', Picqer\Barcode\BarcodeGeneratorSVG::TYPE_KIX));
120+
```

composer.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@
1313
},
1414
{
1515
"name": "Casper Bakker",
16-
"email": "[email protected]"
16+
"email": "[email protected]",
17+
"homepage": "https://picqer.com"
1718
}
1819
],
1920
"require": {
20-
"php": ">=7.2.0"
21+
"php": ">=7.2.0",
22+
"ext-mbstring": "*"
2123
},
2224
"require-dev": {
2325
"phpunit/phpunit": "^8.5"
2426
},
27+
"suggest": {
28+
"ext-bcmath": "Barcode IMB (Intelligent Mail Barcode) needs bcmath extension",
29+
"ext-gd": "For JPG and PNG generators, GD or Imagick is required",
30+
"ext-imagick": "For JPG and PNG generators, GD or Imagick is required"
31+
},
2532
"autoload": {
2633
"psr-4": {
2734
"Picqer\\Barcode\\": "src"

examples.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Examples of supported barcodes
2+
3+
These are examples of supported barcodes with this library.
4+
5+
6+
7+
### C39
8+
9+
![Barcode 1234567890abcABC as C39](tests/verified-files/C39-1234567890abcABC.svg)
10+
11+
### C39+
12+
13+
![Barcode 1234567890abcABC as C39+](tests/verified-files/C39+-1234567890abcABC.svg)
14+
15+
### C39E+
16+
17+
![Barcode 1234567890abcABC as C39E+](tests/verified-files/C39E+-1234567890abcABC.svg)
18+
19+
### C93
20+
21+
![Barcode 1234567890abcABC as C93](tests/verified-files/C93-1234567890abcABC.svg)
22+
23+
### S25
24+
25+
![Barcode 1234567890 as S25](tests/verified-files/S25-1234567890.svg)
26+
27+
### S25+
28+
29+
![Barcode 1234567890 as S25+](tests/verified-files/S25+-1234567890.svg)
30+
31+
### I25
32+
33+
![Barcode 1234567890 as I25](tests/verified-files/I25-1234567890.svg)
34+
35+
### I25+
36+
37+
![Barcode 1234567890 as I25+](tests/verified-files/I25+-1234567890.svg)
38+
39+
### EAN13
40+
41+
![Barcode 081231723897 as EAN13](tests/verified-files/EAN13-081231723897.svg)
42+
43+
![Barcode 0049000004632 as EAN13](tests/verified-files/EAN13-0049000004632.svg)
44+
45+
![Barcode 004900000463 as EAN13](tests/verified-files/EAN13-004900000463.svg)
46+
47+
### C128
48+
49+
![Barcode 081231723897 as C128](tests/verified-files/C128-081231723897.svg)
50+
51+
![Barcode 1234567890abcABC-283*33 as C128](tests/verified-files/C128-1234567890abcABC-283*33.svg)
52+
53+
### C128A
54+
55+
![Barcode 1234567890 as C128A](tests/verified-files/C128A-1234567890.svg)
56+
57+
### C128B
58+
59+
![Barcode 081231723897 as C128B](tests/verified-files/C128B-081231723897.svg)
60+
61+
![Barcode 1234567890abcABC-283*33 as C128B](tests/verified-files/C128B-1234567890abcABC-283*33.svg)
62+
63+
### EAN2
64+
65+
![Barcode 22 as EAN2](tests/verified-files/EAN2-22.svg)
66+
67+
### EAN5
68+
69+
![Barcode 1234567890abcABC-283*33 as EAN5](tests/verified-files/EAN5-1234567890abcABC-283*33.svg)
70+
71+
### EAN8
72+
73+
![Barcode 1234568 as EAN8](tests/verified-files/EAN8-1234568.svg)
74+
75+
### UPCA
76+
77+
![Barcode 123456789 as UPCA](tests/verified-files/UPCA-123456789.svg)
78+
79+
### UPCE
80+
81+
![Barcode 123456789 as UPCE](tests/verified-files/UPCE-123456789.svg)
82+
83+
### MSI
84+
85+
![Barcode 123456789 as MSI](tests/verified-files/MSI-123456789.svg)
86+
87+
### MSI+
88+
89+
![Barcode 123456789 as MSI+](tests/verified-files/MSI+-123456789.svg)
90+
91+
### POSTNET
92+
93+
![Barcode 123456789 as POSTNET](tests/verified-files/POSTNET-123456789.svg)
94+
95+
### PLANET
96+
97+
![Barcode 123456789 as PLANET](tests/verified-files/PLANET-123456789.svg)
98+
99+
### RMS4CC
100+
101+
![Barcode 123456789 as RMS4CC](tests/verified-files/RMS4CC-123456789.svg)
102+
103+
### KIX
104+
105+
![Barcode 123456789 as KIX](tests/verified-files/KIX-123456789.svg)
106+
107+
### IMB
108+
109+
![Barcode 123456789 as IMB](tests/verified-files/IMB-123456789.svg)
110+
111+
### CODABAR
112+
113+
![Barcode 123456789 as CODABAR](tests/verified-files/CODABAR-123456789.svg)
114+
115+
### CODE11
116+
117+
![Barcode 123456789 as CODE11](tests/verified-files/CODE11-123456789.svg)
118+
119+
### PHARMA
120+
121+
![Barcode 123456789 as PHARMA](tests/verified-files/PHARMA-123456789.svg)
122+
123+
### PHARMA2T
124+
125+
![Barcode 123456789 as PHARMA2T](tests/verified-files/PHARMA2T-123456789.svg)
126+
127+
128+
129+
*This file is generated by generate-examples.php*

generate-examples.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
require('vendor/autoload.php');
4+
require(__DIR__ . '/tests/VerifiedBarcodeTest.php');
5+
$verifiedFiles = VerifiedBarcodeTest::$supportedBarcodes;
6+
7+
$result = [];
8+
$result[] = '# Examples of supported barcodes';
9+
$result[] = 'These are examples of supported barcodes with this library.';
10+
$result[] = '';
11+
12+
foreach ($verifiedFiles as $verifiedFile) {
13+
$result[] = '### ' . $verifiedFile['type'];
14+
foreach ($verifiedFile['barcodes'] as $barcode) {
15+
$result[] = sprintf('![Barcode %s as %s](tests/verified-files/%s-%s.svg)', $barcode, $verifiedFile['type'], $verifiedFile['type'], $barcode);
16+
}
17+
}
18+
19+
$result[] = '';
20+
$result[] = '*This file is generated by generate-examples.php*';
21+
22+
file_put_contents('examples.md', implode(PHP_EOL . PHP_EOL, $result));

generate-verified-files.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
<?php
22

3-
include('src/BarcodeGenerator.php');
4-
include('src/BarcodeGeneratorPNG.php');
5-
include('src/BarcodeGeneratorSVG.php');
6-
include('src/BarcodeGeneratorJPG.php');
7-
include('src/BarcodeGeneratorHTML.php');
3+
require('vendor/autoload.php');
84

95
$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG();
106
file_put_contents('tests/verified-files/081231723897-ean13.svg', $generatorSVG->getBarcode('081231723897', $generatorSVG::TYPE_EAN_13));
@@ -14,3 +10,15 @@
1410

1511
$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG();
1612
file_put_contents('tests/verified-files/0049000004632-ean13.svg', $generatorSVG->getBarcode('0049000004632', $generatorSVG::TYPE_EAN_13));
13+
14+
15+
// New style of verified files
16+
require(__DIR__ . '/tests/VerifiedBarcodeTest.php');
17+
$verifiedFiles = VerifiedBarcodeTest::$supportedBarcodes;
18+
19+
$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG();
20+
foreach ($verifiedFiles as $verifiedFile) {
21+
foreach ($verifiedFile['barcodes'] as $barcode) {
22+
file_put_contents('tests/verified-files/' . $verifiedFile['type'] . '-' . $barcode . '.svg', $generatorSVG->getBarcode($barcode, $verifiedFile['type']));
23+
}
24+
}

0 commit comments

Comments
 (0)