diff --git a/Readme.md b/Readme.md index 2eaa1ea..861a2ca 100644 --- a/Readme.md +++ b/Readme.md @@ -58,7 +58,8 @@ $renderer->setForegroundColor($colorRed); file_put_contents('barcode.png', $renderer->render($barcode, 3, 50)); ``` -## Image types +## Image renderers +These are the available renderers: ```php $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); // Vector based SVG $renderer = new Picqer\Barcode\Renderers\PngRenderer(); // Pixel based PNG @@ -67,6 +68,51 @@ $renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); // Pixel based HTML $renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer(); // Vector based HTML (full 'page' width and height) ``` +Each renderer has their own options. Only the barcode is required, the rest is optional. Here are all the options for each renderers: + +### SVG +```php +$renderer = new Picqer\Barcode\Renderers\SvgRenderer(); +$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white +$renderer->setSvgType($renderer::TYPE_SVG_INLINE); // Changes the output to be used inline inside HTML documents, instead of a standalone SVG image (default) +$renderer->setSvgType($renderer::TYPE_SVG_STANDALONE); // If you want to force the default, create a stand alone SVG image + +$renderer->render($barcode, 450.20, 75); // Width and height support floats +```` + +### PNG + JPG +All options for PNG and JPG are the same. +```php +$renderer = new Picqer\Barcode\Renderers\PngRenderer(); +$renderer->setForegroundColor([255, 0, 0]); // Give a color for the bars, the background is always white. Give it as 3 times 0-255 values for red, green and blue. +$renderer->useGd(); // If you have Imagick and GD installed, but want to use GD +$renderer->useImagick(); // If you have Imagick and GD installed, but want to use Imagick +$renderer->render($barcode, 5, 40); // Width factor (how many pixel wide every bar is), and the height in pixels +```` + +### HTML +Gives HTML to use inline in a full HTML document. +```php +$renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); +$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white + +$renderer->render($barcode, 450.20, 75); // Width and height support floats +```` + +### Dynamic HTML +Give HTML here the barcode is using the full width and height, to put inside a container/div that has a fixed size. +```php +$renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer(); +$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white + +$renderer->render($barcode); +```` + +You can put the rendered HTML inside a div like this: +```html +
+``` + ## Accepted barcode types These barcode types are supported. All types support different character sets and some have mandatory lengths. Please see wikipedia for supported chars and lengths per type. diff --git a/src/Renderers/JpgRenderer.php b/src/Renderers/JpgRenderer.php index 30a6b6e..132ec10 100644 --- a/src/Renderers/JpgRenderer.php +++ b/src/Renderers/JpgRenderer.php @@ -14,7 +14,7 @@ protected function createImagickImageObject(int $width, int $height): Imagick return $image; } - protected function generateGdImage($image) + protected function generateGdImage($image): void { \imagejpeg($image); \imagedestroy($image); diff --git a/src/Renderers/PngRenderer.php b/src/Renderers/PngRenderer.php index 59c8269..22cfcc8 100644 --- a/src/Renderers/PngRenderer.php +++ b/src/Renderers/PngRenderer.php @@ -32,7 +32,7 @@ public function __construct() /** * Force the use of Imagick image extension */ - public function useImagick() + public function useImagick(): void { $this->useImagick = true; } @@ -40,7 +40,7 @@ public function useImagick() /** * Force the use of the GD image library */ - public function useGd() + public function useGd(): void { $this->useImagick = false; } @@ -81,14 +81,14 @@ public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30) $image = $this->createImagickImageObject($width, $height); $image->drawImage($imagickBarsShape); return $image->getImageBlob(); + } else { + ob_start(); + $this->generateGdImage($image); + return ob_get_clean(); } - - ob_start(); - $this->generateGdImage($image); - return ob_get_clean(); } - public function setForegroundColor(array $color) + public function setForegroundColor(array $color): void { $this->foregroundColor = $color; } @@ -110,7 +110,7 @@ protected function createImagickImageObject(int $width, int $height): Imagick return $image; } - protected function generateGdImage($image) + protected function generateGdImage($image): void { \imagepng($image); \imagedestroy($image);