Skip to content

Commit 90535c5

Browse files
authored
Merge branch 'main' into patch-1
2 parents 599945e + 829fb1f commit 90535c5

File tree

9 files changed

+209
-28
lines changed

9 files changed

+209
-28
lines changed

Readme.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# PHP Barcode Generator
2-
[![Build Status](https://travis-ci.org/picqer/php-barcode-generator.svg?branch=main)](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)
2+
<a href="https://github.com/picqer/php-barcode-generator/actions"><img src="https://github.com/picqer/php-barcode-generator/workflows/phpunit/badge.svg" alt="Build Status"></a>
3+
<a href="https://packagist.org/packages/picqer/php-barcode-generator"><img src="https://img.shields.io/packagist/dt/picqer/php-barcode-generator" alt="Total Downloads"></a>
4+
<a href="https://packagist.org/packages/picqer/php-barcode-generator"><img src="https://img.shields.io/packagist/v/picqer/php-barcode-generator" alt="Latest Stable Version"></a>
35

4-
This is an easy to use, non-bloated, framework independent, barcode generator in PHP.
6+
This is an easy to use, non-bloated, framework independent, barcode generator in PHP. It uses zero(!) composer dependencies and is only a handful of files. Probably the reason that this is the most downloaded barcode generator for PHP on Packagist. ;)
57

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

@@ -84,6 +86,7 @@ Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner
8486
- TYPE_EAN_5
8587
- TYPE_EAN_8
8688
- TYPE_EAN_13
89+
- TYPE_ITF14 (Also known as GTIN-14)
8790
- TYPE_UPC_A
8891
- TYPE_UPC_E
8992
- TYPE_MSI

phpunit.xml

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
colors="true"
5-
convertErrorsToExceptions="true"
6-
convertNoticesToExceptions="true"
7-
convertWarningsToExceptions="true"
8-
processIsolation="false"
9-
stopOnError="false"
10-
stopOnFailure="false"
11-
verbose="true"
12-
>
13-
<testsuites>
14-
<testsuite name="Barcode Test Suite">
15-
<directory>./tests/</directory>
16-
</testsuite>
17-
</testsuites>
18-
<filter>
19-
<whitelist>
20-
<directory>./src</directory>
21-
</whitelist>
22-
</filter>
23-
</phpunit>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnError="false" stopOnFailure="false" verbose="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory>./src</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Barcode Test Suite">
10+
<directory>./tests/</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

src/BarcodeGenerator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
use Picqer\Barcode\Types\TypeIntelligentMailBarcode;
4848
use Picqer\Barcode\Types\TypeInterleaved25;
4949
use Picqer\Barcode\Types\TypeInterleaved25Checksum;
50+
use Picqer\Barcode\Types\TypeITF14;
5051
use Picqer\Barcode\Types\TypeKix;
5152
use Picqer\Barcode\Types\TypeMsi;
5253
use Picqer\Barcode\Types\TypeMsiChecksum;
@@ -74,6 +75,7 @@ abstract class BarcodeGenerator
7475
const TYPE_STANDARD_2_5_CHECKSUM = 'S25+';
7576
const TYPE_INTERLEAVED_2_5 = 'I25';
7677
const TYPE_INTERLEAVED_2_5_CHECKSUM = 'I25+';
78+
const TYPE_ITF_14 = 'ITF14';
7779
const TYPE_CODE_128 = 'C128';
7880
const TYPE_CODE_128_A = 'C128A';
7981
const TYPE_CODE_128_B = 'C128B';
@@ -136,6 +138,9 @@ protected function createDataBuilderForType(string $type)
136138
case self::TYPE_INTERLEAVED_2_5_CHECKSUM:
137139
return new TypeInterleaved25Checksum();
138140

141+
case self::TYPE_ITF_14:
142+
return new TypeITF14();
143+
139144
case self::TYPE_CODE_128:
140145
return new TypeCode128();
141146

src/BarcodeGeneratorSVG.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ class BarcodeGeneratorSVG extends BarcodeGenerator
88
* Return a SVG string representation of barcode.
99
*
1010
* @param $barcode (string) code to print
11-
* @param $type (const) type of barcode
11+
* @param BarcodeGenerator::Type_* $type (string) type of barcode
1212
* @param $widthFactor (float) Minimum width of a single bar in user units.
1313
* @param $height (int) Height of barcode in user units.
1414
* @param $foregroundColor (string) Foreground color (in SVG format) for bar elements (background is transparent).
1515
* @return string SVG code.
1616
* @public
1717
*/
18-
public function getBarcode($barcode, $type, float $widthFactor = 2, int $height = 30, string $foregroundColor = 'black')
18+
public function getBarcode(string $barcode, $type, float $widthFactor = 2, int $height = 30, string $foregroundColor = 'black')
1919
{
2020
$barcodeData = $this->getBarcodeData($barcode, $type);
2121

src/Types/TypeITF14.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Picqer\Barcode\Types;
4+
5+
use Picqer\Barcode\Barcode;
6+
use Picqer\Barcode\BarcodeBar;
7+
use Picqer\Barcode\Exceptions\InvalidCharacterException;
8+
use Picqer\Barcode\Exceptions\InvalidLengthException;
9+
10+
class TypeITF14 implements TypeInterface
11+
{
12+
/**
13+
* @throws InvalidLengthException
14+
* @throws InvalidCharacterException
15+
*/
16+
public function getBarcodeData(string $code): Barcode
17+
{
18+
$chr = [];
19+
$chr['0'] = '11221';
20+
$chr['1'] = '21112';
21+
$chr['2'] = '12112';
22+
$chr['3'] = '22111';
23+
$chr['4'] = '11212';
24+
$chr['5'] = '21211';
25+
$chr['6'] = '12211';
26+
$chr['7'] = '11122';
27+
$chr['8'] = '21121';
28+
$chr['9'] = '12121';
29+
$chr['A'] = '11';
30+
$chr['Z'] = '21';
31+
32+
if (strlen($code) < 13 || strlen($code) > 14) {
33+
throw new InvalidLengthException();
34+
}
35+
36+
if (strlen($code) === 13) {
37+
$code .= $this->getChecksum($code);
38+
}
39+
40+
$barcode = new Barcode($code);
41+
42+
// Add start and stop codes
43+
$code = 'AA' . strtolower($code) . 'ZA';
44+
45+
// Loop through 2 chars at once
46+
for ($charIndex = 0; $charIndex < strlen($code); $charIndex += 2) {
47+
if (! isset($chr[$code[$charIndex]]) || ! isset($chr[$code[$charIndex + 1]])) {
48+
throw new InvalidCharacterException();
49+
}
50+
51+
$drawBar = true;
52+
$pbars = $chr[$code[$charIndex]];
53+
$pspaces = $chr[$code[$charIndex + 1]];
54+
$pmixed = '';
55+
56+
while (strlen($pbars) > 0) {
57+
$pmixed .= $pbars[0] . $pspaces[0];
58+
$pbars = substr($pbars, 1);
59+
$pspaces = substr($pspaces, 1);
60+
}
61+
62+
foreach (str_split($pmixed) as $width) {
63+
$barcode->addBar(new BarcodeBar($width, 1, $drawBar));
64+
$drawBar = ! $drawBar;
65+
}
66+
}
67+
68+
return $barcode;
69+
}
70+
71+
private function getChecksum(string $code): string
72+
{
73+
$total = 0;
74+
75+
for ($charIndex = 0; $charIndex <= (strlen($code) - 1); $charIndex++) {
76+
$integerOfChar = intval($code . substr($charIndex, 1));
77+
$total += $integerOfChar * (($charIndex === 0 || $charIndex % 2 === 0) ? 3 : 1);
78+
}
79+
80+
$checksum = 10 - ($total % 10);
81+
if ($checksum === 10) {
82+
$checksum = 0;
83+
}
84+
85+
return (string)$checksum;
86+
}
87+
}

src/Types/TypeInterleaved25Checksum.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class TypeInterleaved25Checksum implements TypeInterface
1616
{
1717
public function getBarcodeData(string $code): Barcode
1818
{
19+
$chr = [];
1920
$chr['0'] = '11221';
2021
$chr['1'] = '21112';
2122
$chr['2'] = '12112';
@@ -36,6 +37,7 @@ public function getBarcodeData(string $code): Barcode
3637
// add leading zero if code-length is odd
3738
$code = '0' . $code;
3839
}
40+
3941
// add start and stop codes
4042
$code = 'AA' . strtolower($code) . 'ZA';
4143

tests/VerifiedBarcodeTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
/*
77
* Test all supported barcodes types, with as much different but supported input strings.
8-
* Verified files can be build with generate-verified-files.php file.
8+
* Verified files can be built with generate-verified-files.php file.
99
* Only run that file if you added new types or new strings to test.
1010
*
1111
* We use SVG because that output is vector and should be the same on every host system.
@@ -24,6 +24,7 @@ class VerifiedBarcodeTest extends TestCase
2424
['type' => BarcodeGenerator::TYPE_INTERLEAVED_2_5, 'barcodes' => ['1234567890']],
2525
['type' => BarcodeGenerator::TYPE_INTERLEAVED_2_5_CHECKSUM, 'barcodes' => ['1234567890']],
2626
['type' => BarcodeGenerator::TYPE_EAN_13, 'barcodes' => ['081231723897', '0049000004632', '004900000463']],
27+
['type' => BarcodeGenerator::TYPE_ITF_14, 'barcodes' => ['00012345600012', '05400141288766']],
2728
['type' => BarcodeGenerator::TYPE_CODE_128, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']],
2829
['type' => BarcodeGenerator::TYPE_CODE_128_A, 'barcodes' => ['1234567890']],
2930
['type' => BarcodeGenerator::TYPE_CODE_128_B, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']],
@@ -62,7 +63,8 @@ public function testAllSupportedBarcodeTypes()
6263
}
6364
}
6465

65-
protected function getSaveFilename($value) {
66+
protected function getSaveFilename($value)
67+
{
6668
return preg_replace('/[^a-zA-Z0-9_ \-+]/s', '-', $value);
6769
}
6870
}
Lines changed: 46 additions & 0 deletions
Loading
Lines changed: 46 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)