Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
casperbakker committed Mar 31, 2024
1 parent f20b1ef commit bbe5415
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
48 changes: 47 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
<div style="width: 400px; height: 75px"><?php echo $renderedBarcode; ?></div>
```

## 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.

Expand Down
2 changes: 1 addition & 1 deletion src/Renderers/JpgRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions src/Renderers/PngRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public function __construct()
/**
* Force the use of Imagick image extension
*/
public function useImagick()
public function useImagick(): void
{
$this->useImagick = true;
}

/**
* Force the use of the GD image library
*/
public function useGd()
public function useGd(): void
{
$this->useImagick = false;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down

0 comments on commit bbe5415

Please sign in to comment.