22
33namespace Picqer \Barcode ;
44
5- use Imagick ;
6- use imagickdraw ;
7- use imagickpixel ;
8- use Picqer \Barcode \Exceptions \BarcodeException ;
9-
105class 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}
0 commit comments