Skip to content

Commit 9aba4bc

Browse files
committed
Add support for inline SVG's
1 parent 1c4bc99 commit 9aba4bc

8 files changed

Lines changed: 284 additions & 137 deletions

File tree

src/BarcodeGeneratorJPG.php

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,51 @@
22

33
namespace Picqer\Barcode;
44

5-
use Imagick;
6-
7-
class BarcodeGeneratorJPG extends BarcodeGeneratorPNG
5+
class BarcodeGeneratorJPG extends BarcodeGenerator
86
{
9-
protected function createImagickImageObject(int $width, int $height): Imagick
7+
protected ?bool $useImagick = null;
8+
9+
/**
10+
* Return a PNG image representation of barcode (requires GD or Imagick library).
11+
*
12+
* @param string $barcode code to print
13+
* @param BarcodeGenerator::TYPE_* $type (string) type of barcode
14+
* @param int $widthFactor Width of a single bar element in pixels.
15+
* @param int $height Height of a single bar element in pixels.
16+
* @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent).
17+
* @return string image data or false in case of error.
18+
*/
19+
public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]): string
1020
{
11-
$image = new Imagick();
12-
$image->newImage($width, $height, 'white', 'JPG');
21+
$barcodeData = $this->getBarcodeData($barcode, $type);
22+
23+
$renderer = new \Picqer\Barcode\Renderers\JpgRenderer();
24+
$renderer->setForegroundColor($foregroundColor);
25+
26+
if (! is_null($this->useImagick)) {
27+
if ($this->useImagick) {
28+
$renderer->useImagick();
29+
} else {
30+
$renderer->useGd();
31+
}
32+
}
1333

14-
return $image;
34+
return $renderer->render($barcodeData, $widthFactor, $height);
35+
}
36+
37+
/**
38+
* Force the use of Imagick image extension
39+
*/
40+
public function useImagick()
41+
{
42+
$this->useImagick = true;
1543
}
1644

17-
protected function generateGdImage($image)
45+
/**
46+
* Force the use of the GD image library
47+
*/
48+
public function useGd()
1849
{
19-
imagejpeg($image);
20-
imagedestroy($image);
50+
$this->useImagick = false;
2151
}
2252
}

src/BarcodeGeneratorPNG.php

Lines changed: 19 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,9 @@
22

33
namespace Picqer\Barcode;
44

5-
use Imagick;
6-
use imagickdraw;
7-
use imagickpixel;
8-
use Picqer\Barcode\Exceptions\BarcodeException;
9-
105
class BarcodeGeneratorPNG extends BarcodeGenerator
116
{
12-
protected $useImagick = true;
13-
14-
/**
15-
* @throws BarcodeException
16-
*/
17-
public function __construct()
18-
{
19-
// Auto switch between GD and Imagick based on what is installed
20-
if (extension_loaded('imagick')) {
21-
$this->useImagick = true;
22-
} elseif (function_exists('imagecreate')) {
23-
$this->useImagick = false;
24-
} else {
25-
throw new BarcodeException('Neither gd-lib or imagick are installed!');
26-
}
27-
}
28-
29-
/**
30-
* Force the use of Imagick image extension
31-
*/
32-
public function useImagick()
33-
{
34-
$this->useImagick = true;
35-
}
36-
37-
/**
38-
* Force the use of the GD image library
39-
*/
40-
public function useGd()
41-
{
42-
$this->useImagick = false;
43-
}
7+
protected ?bool $useImagick = null;
448

459
/**
4610
* Return a PNG image representation of barcode (requires GD or Imagick library).
@@ -55,67 +19,34 @@ public function useGd()
5519
public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]): string
5620
{
5721
$barcodeData = $this->getBarcodeData($barcode, $type);
58-
$width = round($barcodeData->getWidth() * $widthFactor);
59-
60-
if ($this->useImagick) {
61-
$imagickBarsShape = new imagickdraw();
62-
$imagickBarsShape->setFillColor(new imagickpixel('rgb(' . implode(',', $foregroundColor) .')'));
63-
} else {
64-
$image = $this->createGdImageObject($width, $height);
65-
$gdForegroundColor = imagecolorallocate($image, $foregroundColor[0], $foregroundColor[1], $foregroundColor[2]);
66-
}
6722

68-
// print bars
69-
$positionHorizontal = 0;
70-
/** @var BarcodeBar $bar */
71-
foreach ($barcodeData->getBars() as $bar) {
72-
$barWidth = round(($bar->getWidth() * $widthFactor), 3);
23+
$renderer = new \Picqer\Barcode\Renderers\PngRenderer();
24+
$renderer->setForegroundColor($foregroundColor);
7325

74-
if ($bar->isBar() && $barWidth > 0) {
75-
$y = round(($bar->getPositionVertical() * $height / $barcodeData->getHeight()), 3);
76-
$barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3);
77-
78-
// draw a vertical bar
79-
if ($this->useImagick) {
80-
$imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight));
81-
} else {
82-
imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor);
83-
}
26+
if (! is_null($this->useImagick)) {
27+
if ($this->useImagick) {
28+
$renderer->useImagick();
29+
} else {
30+
$renderer->useGd();
8431
}
85-
$positionHorizontal += $barWidth;
86-
}
87-
88-
if ($this->useImagick) {
89-
$image = $this->createImagickImageObject($width, $height);
90-
$image->drawImage($imagickBarsShape);
91-
return $image->getImageBlob();
9232
}
9333

94-
ob_start();
95-
$this->generateGdImage($image);
96-
return ob_get_clean();
34+
return $renderer->render($barcodeData, $widthFactor, $height);
9735
}
9836

99-
protected function createGdImageObject(int $width, int $height)
100-
{
101-
$image = imagecreate($width, $height);
102-
$colorBackground = imagecolorallocate($image, 255, 255, 255);
103-
imagecolortransparent($image, $colorBackground);
104-
105-
return $image;
106-
}
107-
108-
protected function createImagickImageObject(int $width, int $height): Imagick
37+
/**
38+
* Force the use of Imagick image extension
39+
*/
40+
public function useImagick()
10941
{
110-
$image = new Imagick();
111-
$image->newImage($width, $height, 'none', 'PNG');
112-
113-
return $image;
42+
$this->useImagick = true;
11443
}
11544

116-
protected function generateGdImage($image)
45+
/**
46+
* Force the use of the GD image library
47+
*/
48+
public function useGd()
11749
{
118-
imagepng($image);
119-
imagedestroy($image);
50+
$this->useImagick = false;
12051
}
12152
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Picqer\Barcode\Exceptions;
4+
5+
class InvalidOptionException extends BarcodeException {}

src/Renderers/JpgRenderer.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Picqer\Barcode\Renderers;
4+
5+
use Imagick;
6+
7+
class JpgRenderer extends PngRenderer
8+
{
9+
protected function createImagickImageObject(int $width, int $height): Imagick
10+
{
11+
$image = new Imagick();
12+
$image->newImage($width, $height, 'none', 'JPG');
13+
14+
return $image;
15+
}
16+
17+
protected function generateGdImage($image)
18+
{
19+
\imagejpeg($image);
20+
\imagedestroy($image);
21+
}
22+
}

src/Renderers/PngRenderer.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace Picqer\Barcode\Renderers;
4+
5+
use Imagick;
6+
use ImagickDraw;
7+
use ImagickPixel;
8+
use Picqer\Barcode\Barcode;
9+
use Picqer\Barcode\BarcodeBar;
10+
use Picqer\Barcode\Exceptions\BarcodeException;
11+
12+
class PngRenderer
13+
{
14+
protected array $foregroundColor = [0, 0, 0];
15+
protected bool $useImagick;
16+
17+
/**
18+
* @throws BarcodeException
19+
*/
20+
public function __construct()
21+
{
22+
// Auto switch between GD and Imagick based on what is installed
23+
if (extension_loaded('imagick')) {
24+
$this->useImagick = true;
25+
} elseif (function_exists('imagecreate')) {
26+
$this->useImagick = false;
27+
} else {
28+
throw new BarcodeException('Neither gd-lib or imagick are installed!');
29+
}
30+
}
31+
32+
/**
33+
* Force the use of Imagick image extension
34+
*/
35+
public function useImagick()
36+
{
37+
$this->useImagick = true;
38+
}
39+
40+
/**
41+
* Force the use of the GD image library
42+
*/
43+
public function useGd()
44+
{
45+
$this->useImagick = false;
46+
}
47+
48+
public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30): string
49+
{
50+
$width = round($barcode->getWidth() * $widthFactor);
51+
52+
if ($this->useImagick) {
53+
$imagickBarsShape = new ImagickDraw();
54+
$imagickBarsShape->setFillColor(new ImagickPixel('rgb(' . implode(',', $this->foregroundColor) .')'));
55+
} else {
56+
$image = $this->createGdImageObject($width, $height);
57+
$gdForegroundColor = \imagecolorallocate($image, $this->foregroundColor[0], $this->foregroundColor[1], $this->foregroundColor[2]);
58+
}
59+
60+
// print bars
61+
$positionHorizontal = 0;
62+
/** @var BarcodeBar $bar */
63+
foreach ($barcode->getBars() as $bar) {
64+
$barWidth = round(($bar->getWidth() * $widthFactor), 3);
65+
66+
if ($bar->isBar() && $barWidth > 0) {
67+
$y = round(($bar->getPositionVertical() * $height / $barcode->getHeight()), 3);
68+
$barHeight = round(($bar->getHeight() * $height / $barcode->getHeight()), 3);
69+
70+
// draw a vertical bar
71+
if ($this->useImagick) {
72+
$imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight));
73+
} else {
74+
\imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor);
75+
}
76+
}
77+
$positionHorizontal += $barWidth;
78+
}
79+
80+
if ($this->useImagick) {
81+
$image = $this->createImagickImageObject($width, $height);
82+
$image->drawImage($imagickBarsShape);
83+
return $image->getImageBlob();
84+
}
85+
86+
ob_start();
87+
$this->generateGdImage($image);
88+
return ob_get_clean();
89+
}
90+
91+
public function setForegroundColor(array $color)
92+
{
93+
$this->foregroundColor = $color;
94+
}
95+
96+
protected function createGdImageObject(int $width, int $height)
97+
{
98+
$image = \imagecreate($width, $height);
99+
$colorBackground = \imagecolorallocate($image, 255, 255, 255);
100+
\imagecolortransparent($image, $colorBackground);
101+
102+
return $image;
103+
}
104+
105+
protected function createImagickImageObject(int $width, int $height): Imagick
106+
{
107+
$image = new Imagick();
108+
$image->newImage($width, $height, 'none', 'PNG');
109+
110+
return $image;
111+
}
112+
113+
protected function generateGdImage($image)
114+
{
115+
\imagepng($image);
116+
\imagedestroy($image);
117+
}
118+
}

0 commit comments

Comments
 (0)