-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c4bc99
commit 9aba4bc
Showing
8 changed files
with
284 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace Picqer\Barcode\Exceptions; | ||
|
||
class InvalidOptionException extends BarcodeException {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Picqer\Barcode\Renderers; | ||
|
||
use Imagick; | ||
|
||
class JpgRenderer extends PngRenderer | ||
{ | ||
protected function createImagickImageObject(int $width, int $height): Imagick | ||
{ | ||
$image = new Imagick(); | ||
$image->newImage($width, $height, 'none', 'JPG'); | ||
|
||
return $image; | ||
} | ||
|
||
protected function generateGdImage($image) | ||
{ | ||
\imagejpeg($image); | ||
\imagedestroy($image); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
namespace Picqer\Barcode\Renderers; | ||
|
||
use Imagick; | ||
use ImagickDraw; | ||
use ImagickPixel; | ||
use Picqer\Barcode\Barcode; | ||
use Picqer\Barcode\BarcodeBar; | ||
use Picqer\Barcode\Exceptions\BarcodeException; | ||
|
||
class PngRenderer | ||
{ | ||
protected array $foregroundColor = [0, 0, 0]; | ||
protected bool $useImagick; | ||
|
||
/** | ||
* @throws BarcodeException | ||
*/ | ||
public function __construct() | ||
{ | ||
// Auto switch between GD and Imagick based on what is installed | ||
if (extension_loaded('imagick')) { | ||
$this->useImagick = true; | ||
} elseif (function_exists('imagecreate')) { | ||
$this->useImagick = false; | ||
} else { | ||
throw new BarcodeException('Neither gd-lib or imagick are installed!'); | ||
} | ||
} | ||
|
||
/** | ||
* Force the use of Imagick image extension | ||
*/ | ||
public function useImagick() | ||
{ | ||
$this->useImagick = true; | ||
} | ||
|
||
/** | ||
* Force the use of the GD image library | ||
*/ | ||
public function useGd() | ||
{ | ||
$this->useImagick = false; | ||
} | ||
|
||
public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30): string | ||
{ | ||
$width = round($barcode->getWidth() * $widthFactor); | ||
|
||
if ($this->useImagick) { | ||
$imagickBarsShape = new ImagickDraw(); | ||
$imagickBarsShape->setFillColor(new ImagickPixel('rgb(' . implode(',', $this->foregroundColor) .')')); | ||
} else { | ||
$image = $this->createGdImageObject($width, $height); | ||
$gdForegroundColor = \imagecolorallocate($image, $this->foregroundColor[0], $this->foregroundColor[1], $this->foregroundColor[2]); | ||
} | ||
|
||
// print bars | ||
$positionHorizontal = 0; | ||
/** @var BarcodeBar $bar */ | ||
foreach ($barcode->getBars() as $bar) { | ||
$barWidth = round(($bar->getWidth() * $widthFactor), 3); | ||
|
||
if ($bar->isBar() && $barWidth > 0) { | ||
$y = round(($bar->getPositionVertical() * $height / $barcode->getHeight()), 3); | ||
$barHeight = round(($bar->getHeight() * $height / $barcode->getHeight()), 3); | ||
|
||
// draw a vertical bar | ||
if ($this->useImagick) { | ||
$imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight)); | ||
} else { | ||
\imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor); | ||
} | ||
} | ||
$positionHorizontal += $barWidth; | ||
} | ||
|
||
if ($this->useImagick) { | ||
$image = $this->createImagickImageObject($width, $height); | ||
$image->drawImage($imagickBarsShape); | ||
return $image->getImageBlob(); | ||
} | ||
|
||
ob_start(); | ||
$this->generateGdImage($image); | ||
return ob_get_clean(); | ||
} | ||
|
||
public function setForegroundColor(array $color) | ||
{ | ||
$this->foregroundColor = $color; | ||
} | ||
|
||
protected function createGdImageObject(int $width, int $height) | ||
{ | ||
$image = \imagecreate($width, $height); | ||
$colorBackground = \imagecolorallocate($image, 255, 255, 255); | ||
\imagecolortransparent($image, $colorBackground); | ||
|
||
return $image; | ||
} | ||
|
||
protected function createImagickImageObject(int $width, int $height): Imagick | ||
{ | ||
$image = new Imagick(); | ||
$image->newImage($width, $height, 'none', 'PNG'); | ||
|
||
return $image; | ||
} | ||
|
||
protected function generateGdImage($image) | ||
{ | ||
\imagepng($image); | ||
\imagedestroy($image); | ||
} | ||
} |
Oops, something went wrong.