Skip to content

Commit

Permalink
Merge pull request #187 from dhanar98/feature/jpg-image-format
Browse files Browse the repository at this point in the history
[Feature] Support JPG Barcode Image Format
  • Loading branch information
milon authored Feb 28, 2024
2 parents bcf1190 + df82d50 commit 1f4031a
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 1 deletion.
10 changes: 9 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,16 @@ For version 4.2 alias will be like this-

Bar-code generator like Qr Code, PDF417, C39, C39+, C39E, C39E+, C93, S25, S25+, I25, I25+, C128, C128A, C128B, C128C, 2-Digits UPC-Based Extention, 5-Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI (Variation of Plessey code)

generator in html, png embedded base64 code and SVG canvas
generator in html, png , jpeg embedded base64 code and SVG canvas

```php
echo DNS1D::getBarcodeSVG('4445645656', 'PHARMA2T');
echo DNS1D::getBarcodeHTML('4445645656', 'PHARMA2T');
echo '<img src="data:image/png,' . DNS1D::getBarcodePNG('4', 'C39+') . '" alt="barcode" />';
echo DNS1D::getBarcodePNGPath('4445645656', 'PHARMA2T');
echo '<img src="data:image/png;base64,' . DNS1D::getBarcodePNG('4', 'C39+') . '" alt="barcode" />';
echo DNS1D::getBarcodeJPGPath('4445645656', 'PHARMA2T');
echo '<img src="data:image/jpeg;base64,' . DNS1D::getBarcodeJPG('4', 'C39+') . '" alt="barcode" />';
```

```php
Expand All @@ -156,6 +158,8 @@ echo DNS1D::getBarcodeHTML('4445645656', 'PHARMA2T',3,33);
echo '<img src="' . DNS1D::getBarcodePNG('4', 'C39+',3,33) . '" alt="barcode" />';
echo DNS1D::getBarcodePNGPath('4445645656', 'PHARMA2T',3,33);
echo '<img src="data:image/png;base64,' . DNS1D::getBarcodePNG('4', 'C39+',3,33) . '" alt="barcode" />';
echo DNS1D::getBarcodeJPGPath('4445645656', 'PHARMA2T',3,33);
echo '<img src="data:image/jpeg;base64,' . DNS1D::getBarcodeJPG('4', 'C39+',3,33) . '" alt="barcode" />';
```

## Color
Expand All @@ -166,6 +170,8 @@ echo DNS1D::getBarcodeHTML('4445645656', 'PHARMA2T',3,33,'green');
echo '<img src="' . DNS1D::getBarcodePNG('4', 'C39+',3,33,array(1,1,1)) . '" alt="barcode" />';
echo DNS1D::getBarcodePNGPath('4445645656', 'PHARMA2T',3,33,array(255,255,0));
echo '<img src="data:image/png;base64,' . DNS1D::getBarcodePNG('4', 'C39+',3,33,array(1,1,1)) . '" alt="barcode" />';
echo DNS1D::getBarcodeJPGPath('4445645656', 'PHARMA2T',3,33,array(255,255,0));
echo '<img src="data:image/jpeg;base64,' . DNS1D::getBarcodeJPG('4', 'C39+',3,33,array(1,1,1)) . '" alt="barcode" />';
```

## Show Text
Expand All @@ -176,6 +182,8 @@ echo DNS1D::getBarcodeHTML('4445645656', 'PHARMA2T',3,33,'green', true);
echo '<img src="' . DNS1D::getBarcodePNG('4', 'C39+',3,33,array(1,1,1), true) . '" alt="barcode" />';
echo DNS1D::getBarcodePNGPath('4445645656', 'PHARMA2T',3,33,array(255,255,0), true);
echo '<img src="data:image/png;base64,' . DNS1D::getBarcodePNG('4', 'C39+',3,33,array(1,1,1), true) . '" alt="barcode" />';
echo DNS1D::getBarcodeJPGPath('4445645656', 'PHARMA2T',3,33,array(255,255,0), true);
echo '<img src="data:image/jpeg;base64,' . DNS1D::getBarcodeJPG('4', 'C39+',3,33,array(1,1,1), true) . '" alt="barcode" />';
```

## 2D Barcodes
Expand Down
181 changes: 181 additions & 0 deletions src/Milon/Barcode/DNS1D.php
Original file line number Diff line number Diff line change
Expand Up @@ -2625,6 +2625,187 @@ protected function upce2a($code) {
return '0' . $manufacturer . $itemNumber;
}


/**
* Return a JPG image representation of barcode (requires GD or Imagick library).
* @param $code (string) code to print
* @param $type (string) type of barcode: <ul><li>C39 : CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.</li><li>C39+ : CODE 39 with checksum</li><li>C39E : CODE 39 EXTENDED</li><li>C39E+ : CODE 39 EXTENDED + CHECKSUM</li><li>C93 : CODE 93 - USS-93</li><li>S25 : Standard 2 of 5</li><li>S25+ : Standard 2 of 5 + CHECKSUM</li><li>I25 : Interleaved 2 of 5</li><li>I25+ : Interleaved 2 of 5 + CHECKSUM</li><li>C128 : CODE 128</li><li>C128A : CODE 128 A</li><li>C128B : CODE 128 B</li><li>C128C : CODE 128 C</li><li>EAN2 : 2-Digits UPC-Based Extention</li><li>EAN5 : 5-Digits UPC-Based Extention</li><li>EAN8 : EAN 8</li><li>EAN13 : EAN 13</li><li>UPCA : UPC-A</li><li>UPCE : UPC-E</li><li>MSI : MSI (Variation of Plessey code)</li><li>MSI+ : MSI + CHECKSUM (modulo 11)</li><li>POSTNET : POSTNET</li><li>PLANET : PLANET</li><li>RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)</li><li>KIX : KIX (Klant index - Customer index)</li><li>IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200</li><li>CODABAR : CODABAR</li><li>CODE11 : CODE 11</li><li>PHARMA : PHARMACODE</li><li>PHARMA2T : PHARMACODE TWO-TRACKS</li></ul>
* @param $w (int) Width of a single bar element in pixels.
* @param $h (int) Height of a single bar element in pixels.
* @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
* @return string|false in case of error.
* @protected
*/
public function getBarcodeJPG($code, $type, $w = 2, $h = 30, $color = array(0, 0, 0), $showCode = false) {
if (!$this->store_path) {
$this->setStorPath(app('config')->get("barcode.store_path"));
}
$this->setBarcode($code, $type);
// calculate image size
$width = ($this->barcode_array['maxw'] * $w);
$height = $h;
if (function_exists('imagecreate')) {
// GD library
$imagick = false;
$jpg = imagecreate($width, $height);
$bgcol = imagecolorallocate($jpg, 255, 255, 255);
imagecolortransparent($jpg, $bgcol);
$fgcol = imagecolorallocate($jpg, $color[0], $color[1], $color[2]);
} elseif (extension_loaded('imagick')) {
$imagick = true;

$bgcol = new \imagickpixel('rgb(255,255,255)');
$fgcol = new \imagickpixel('rgb(' . implode(',', $color) .')');
$jpg = new \Imagick();
$jpg->newImage($width, $height, 'white', 'jpg');
$bar = new \imagickdraw();
$bar->setfillcolor($fgcol);
} else {
return false;
}
// print bars
$x = 0;
foreach ($this->barcode_array['bcode'] as $k => $v) {
$bw = round(($v['w'] * $w), 3);
$bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
if($showCode)
$bh -= imagefontheight(3) ;
if ($v['t']) {
$y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
// draw a vertical bar
if ($imagick) {
$bar->rectangle($x, $y, ($x + $bw), ($y + $bh));
} else {
imagefilledrectangle($jpg, $x, $y, ($x + $bw) - 1, ($y + $bh), $fgcol);
}
}
$x += $bw;
}
ob_start();

// Add Code String in bottom
if($showCode)
if ($imagick) {
$bar->setTextAlignment(\Imagick::ALIGN_CENTER);
$bar->annotation( 10 , $h - $bh +10 , $code );
} else {
$width_text = imagefontwidth(3) * strlen($code);
$height_text = imagefontheight(3);
imagestring($jpg, 3, ($width/2) - ($width_text/2) , ($height - $height_text) , $code, $fgcol);

}
// get image out put
if ($imagick) {
$jpg->drawimage($bar);
echo $jpg;
} else {
imagejpeg($jpg);
imagedestroy($jpg);
}
$image = ob_get_clean();
$image = base64_encode($image);
//$image = 'data:image/jpeg;base64,' . base64_encode($image);
return $image;
}


/**
* Return a .jpg file path which create in server
* @param $code (string) code to print
* @param $type (string) type of barcode: <ul><li>C39 : CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.</li><li>C39+ : CODE 39 with checksum</li><li>C39E : CODE 39 EXTENDED</li><li>C39E+ : CODE 39 EXTENDED + CHECKSUM</li><li>C93 : CODE 93 - USS-93</li><li>S25 : Standard 2 of 5</li><li>S25+ : Standard 2 of 5 + CHECKSUM</li><li>I25 : Interleaved 2 of 5</li><li>I25+ : Interleaved 2 of 5 + CHECKSUM</li><li>C128 : CODE 128</li><li>C128A : CODE 128 A</li><li>C128B : CODE 128 B</li><li>C128C : CODE 128 C</li><li>EAN2 : 2-Digits UPC-Based Extention</li><li>EAN5 : 5-Digits UPC-Based Extention</li><li>EAN8 : EAN 8</li><li>EAN13 : EAN 13</li><li>UPCA : UPC-A</li><li>UPCE : UPC-E</li><li>MSI : MSI (Variation of Plessey code)</li><li>MSI+ : MSI + CHECKSUM (modulo 11)</li><li>POSTNET : POSTNET</li><li>PLANET : PLANET</li><li>RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)</li><li>KIX : KIX (Klant index - Customer index)</li><li>IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200</li><li>CODABAR : CODABAR</li><li>CODE11 : CODE 11</li><li>PHARMA : PHARMACODE</li><li>PHARMA2T : PHARMACODE TWO-TRACKS</li></ul>
* @param $w (int) Width of a single bar element in pixels.
* @param $h (int) Height of a single bar element in pixels.
* @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
* @return path or false in case of error.
* @protected
*/
protected function getBarcodeJPGPath($code, $type, $w = 2, $h = 30, $color = array(0, 0, 0), $showCode = false) {
if (!$this->store_path) {
$this->setStorPath(app('config')->get("barcode.store_path"));
}
$this->setBarcode($code, $type);
// calculate image size
$width = ($this->barcode_array['maxw'] * $w);
$height = $h;
if (function_exists('imagecreate')) {
// GD library
$imagick = false;
$jpg = imagecreate($width, $height);
$bgcol = imagecolorallocate($jpg, 255, 255, 255);
imagecolortransparent($jpg, $bgcol);
$fgcol = imagecolorallocate($jpg, $color[0], $color[1], $color[2]);
} elseif (extension_loaded('imagick')) {
$imagick = true;
$bgcol = new imagickpixel('rgb(255,255,255');
$fgcol = new \imagickpixel('rgb(' . implode(',', $color) .')');
$jpg = new Imagick();
$jpg->newImage($width, $height, 'white', 'jpg');
$bar = new imagickdraw();
$bar->setfillcolor($fgcol);
} else {
return false;
}
// print bars
$x = 0;
foreach ($this->barcode_array['bcode'] as $k => $v) {
$bw = round(($v['w'] * $w), 3);
$bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);

if($showCode)
$bh -= imagefontheight(3) ;
if ($v['t']) {
$y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
// draw a vertical bar
if ($imagick) {
$bar->rectangle($x, $y, ($x + $bw), ($y + $bh));
} else {
imagefilledrectangle($jpg, $x, $y, ($x + $bw) - 1, ($y + $bh), $fgcol);
}
}
$x += $bw;
}
if($showCode)
if ($imagick) {
$bar->setTextAlignment(\Imagick::ALIGN_CENTER);
$bar->annotation( 10 , $h - $bh +10 , $code );
} else {
$width_text = imagefontwidth(3) * strlen($code);
$height_text = imagefontheight(3);
imagestring($jpg, 3, ($width/2) - ($width_text/2) , ($height - $height_text) , $code, $fgcol);
}

$file_name= Str::slug($code);
$save_file = $this->checkfile($this->store_path . $file_name . ".jpg");

if ($imagick) {
$jpg->drawimage($bar);
//echo $jpg;
}
if (imagejpeg($jpg, $save_file)) {
imagedestroy($jpg);
return str_replace(public_path(), '', $save_file);
} else {
imagedestroy($jpg);
return $code;
}
}

/**
* Return a .jpg file path which create in server
* @param $code (string) code to print
* @param $type (string) type of barcode: <ul><li>C39 : CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.</li><li>C39+ : CODE 39 with checksum</li><li>C39E : CODE 39 EXTENDED</li><li>C39E+ : CODE 39 EXTENDED + CHECKSUM</li><li>C93 : CODE 93 - USS-93</li><li>S25 : Standard 2 of 5</li><li>S25+ : Standard 2 of 5 + CHECKSUM</li><li>I25 : Interleaved 2 of 5</li><li>I25+ : Interleaved 2 of 5 + CHECKSUM</li><li>C128 : CODE 128</li><li>C128A : CODE 128 A</li><li>C128B : CODE 128 B</li><li>C128C : CODE 128 C</li><li>EAN2 : 2-Digits UPC-Based Extention</li><li>EAN5 : 5-Digits UPC-Based Extention</li><li>EAN8 : EAN 8</li><li>EAN13 : EAN 13</li><li>UPCA : UPC-A</li><li>UPCE : UPC-E</li><li>MSI : MSI (Variation of Plessey code)</li><li>MSI+ : MSI + CHECKSUM (modulo 11)</li><li>POSTNET : POSTNET</li><li>PLANET : PLANET</li><li>RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)</li><li>KIX : KIX (Klant index - Customer index)</li><li>IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200</li><li>CODABAR : CODABAR</li><li>CODE11 : CODE 11</li><li>PHARMA : PHARMACODE</li><li>PHARMA2T : PHARMACODE TWO-TRACKS</li></ul>
* @param $w (int) Width of a single bar element in pixels.
* @param $h (int) Height of a single bar element in pixels.
* @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
* @return url or false in case of error.
* @protected
*/
protected function getBarcodeJPGUri($code, $type, $w = 2, $h = 30, $color = array(0, 0, 0)) {
$path = $this->getBarcodeJPGPath($code, $type, $w, $h, $color);
// Replace backslash (Windows) with forward slashes, to make it compatible with url().
return url(str_replace('\\', '/', $path));
}

/**
* Handle dynamic method calls.
*
Expand Down

0 comments on commit 1f4031a

Please sign in to comment.