public function makeBarcodePNG($w = 2, $h = 30, $color = array(0, 0, 0)) { // calculate image size $width = $this->barcode_array['maxw'] * $w; $height = $h; if (function_exists('imagecreate')) { // GD library $imagick = false; $png = imagecreate($width, $height); $bgcol = imagecolorallocate($png, 255, 255, 255); imagecolortransparent($png, $bgcol); $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]); } elseif (extension_loaded('imagick')) { $imagick = true; $bgcol = new imagickpixel('rgb(255,255,255'); $fgcol = new imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); $png = new Imagick(); $png->newImage($width, $height, 'none', 'png'); $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 ($v['t']) { $y = round($v['p'] * $h / $this->barcode_array['maxh'], 3); // draw a vertical bar if ($imagick) { $bar->rectangle($x, $y, $x + $bw - 1, $y + $bh - 1); } else { imagefilledrectangle($png, $x, $y, $x + $bw - 1, $y + $bh - 1, $fgcol); } } $x += $bw; } // send headers //header('Content-Type: image/png'); //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1 //header('Pragma: public'); //header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past //header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); if ($imagick) { $png->drawimage($bar); echo $png; } else { // start buffering ob_start(); imagepng($png); $contents = ob_get_contents(); ob_end_clean(); imagedestroy($png); return '<img src="data:image/png;base64,' . base64_encode($contents) . '" />'; } }
/** * Returns a PNG representation of the barcode * @param int $width The width of a single rectangle element in pixels * @param int $height The height of a single rectangle element in pixels * @param array $color RGB (0-255) foreground color for rectangle elements (background is transparent) * @return string An base64 encoded image string representing the barcode */ public function toPNG($width = 3, $height = 3, $color = array(0, 0, 0)) { // calculate image size $imageWidth = $this->barcode_array['num_cols'] * $width; $imageHeight = $this->barcode_array['num_rows'] * $height; if (function_exists('imagecreate')) { // GD library $imagick = false; $png = imagecreate($imageWidth, $imageHeight); $bgcol = imagecolorallocate($png, 255, 255, 255); imagecolortransparent($png, $bgcol); $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]); } else { if (extension_loaded('imagick')) { $imagick = true; $fgcol = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); $png = new \Imagick(); $png->newImage($imageWidth, $imageHeight, 'none', 'png'); $bar = new \imagickdraw(); $bar->setfillcolor($fgcol); } else { throw new \RuntimeException("PHP 4+ or the imagick extension is required to generate PNG barcodes."); } } // print barcode elements $y = 0; // for each row for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) { $x = 0; // for each column for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) { if ($this->barcode_array['bcode'][$r][$c] == 1) { // draw a single barcode cell if ($imagick) { $bar->rectangle($x, $y, $x + $width, $y + $height); } else { imagefilledrectangle($png, $x, $y, $x + $width, $y + $height, $fgcol); } } $x += $width; } $y += $height; } ob_start(); // get image out put if ($imagick) { $png->drawimage($bar); echo $png; } else { imagepng($png); imagedestroy($png); } $image = ob_get_clean(); $image = base64_encode($image); //$image = 'data:image/png;base64,' . base64_encode($image); return $image; }
/** * Returns a PNG representation of the barcode * @param int $width The width of a single bar element in pixels * @param int $height The height of a single bar element in pixels * @param array $color RGB (0-255) foreground color for bar elements (background is transparent) * @return string An base64 encoded image string representing the barcode */ public function toPNG($width = 2, $height = 30, $color = array(0, 0, 0)) { // calculate image size $imageWidth = $this->barcode_array['maxw'] * $width; $imageHeight = $height; if (function_exists('imagecreate')) { // GD library $imagick = false; $png = imagecreate($imageWidth, $imageHeight); $bgcol = imagecolorallocate($png, 255, 255, 255); imagecolortransparent($png, $bgcol); $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]); } else { if (extension_loaded('imagick')) { $imagick = true; $fgcol = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); $png = new \Imagick(); $png->newImage($imageWidth, $imageHeight, 'none', 'png'); $bar = new \imagickdraw(); $bar->setfillcolor($fgcol); } else { throw new \RuntimeException("PHP 4+ or the imagick extension is required to generate PNG barcodes."); } } // print bars $x = 0; foreach ($this->barcode_array['bcode'] as $k => $v) { $bw = round($v['w'] * $width, 3); $bh = round($v['h'] * $height / $this->barcode_array['maxh'], 3); if ($v['t']) { $y = round($v['p'] * $height / $this->barcode_array['maxh'], 3); // draw a vertical bar if ($imagick) { $bar->rectangle($x, $y, $x + $bw, $y + $bh); } else { imagefilledrectangle($png, $x, $y, $x + $bw - 1, $y + $bh, $fgcol); } } $x += $bw; } ob_start(); // get image out put if ($imagick) { $png->drawimage($bar); echo $png; } else { imagepng($png); imagedestroy($png); } $image = ob_get_clean(); $image = base64_encode($image); //$image = 'data:image/png;base64,' . base64_encode($image); return $image; }
/** * Return a JPG image representation of barcode (requires GD or Imagick library). * * @param string $code code to print * @param string $type type of barcode: * @param int $widthFactor Width of a single bar element in pixels. * @param int $totalHeight Height of a single bar element in pixels. * @param array $color RGB (0-255) foreground color for bar elements (background is transparent). * @return string image data or false in case of error. * @public */ public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $color = array(0, 0, 0)) { $barcodeData = $this->getBarcodeData($code, $type); // calculate image size $width = $barcodeData['maxWidth'] * $widthFactor; $height = $totalHeight; if (function_exists('imagecreate')) { // GD library $imagick = false; $jpg = imagecreate($width, $height); $colorBackground = imagecolorallocate($jpg, 255, 255, 255); imagecolortransparent($jpg, $colorBackground); $colorForeground = imagecolorallocate($jpg, $color[0], $color[1], $color[2]); } elseif (extension_loaded('imagick')) { $imagick = true; $colorForeground = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); $jpg = new \Imagick(); $jpg->newImage($width, $height, 'none', 'jpg'); $imageMagickObject = new \imagickdraw(); $imageMagickObject->setFillColor($colorForeground); } else { return false; } // print bars $positionHorizontal = 0; foreach ($barcodeData['bars'] as $bar) { $bw = round($bar['width'] * $widthFactor, 3); $bh = round($bar['height'] * $totalHeight / $barcodeData['maxHeight'], 3); if ($bar['drawBar']) { $y = round($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight'], 3); // draw a vertical bar if ($imagick && isset($imageMagickObject)) { $imageMagickObject->rectangle($positionHorizontal, $y, $positionHorizontal + $bw, $y + $bh); } else { imagefilledrectangle($jpg, $positionHorizontal, $y, $positionHorizontal + $bw - 1, $y + $bh, $colorForeground); } } $positionHorizontal += $bw; } ob_start(); if ($imagick && isset($imageMagickObject)) { $jpg->drawImage($imageMagickObject); echo $jpg; } else { imagejpeg($jpg); imagedestroy($jpg); } $image = ob_get_clean(); return $image; }
/** * Return a PNG image representation of barcode (requires GD or Imagick library). * * @param string $code code to print * @param string $type type of barcode: * @param int $widthFactor Width of a single bar element in pixels. * @param int $totalHeight Height of a single bar element in pixels. * @param array $color RGB (0-255) foreground color for bar elements (background is transparent). * @return \Imagick imagick object with barcode * @public */ public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $color = array(0, 0, 0)) { $barcodeData = $this->getBarcodeData($code, $type); // calculate image size $width = $barcodeData['maxWidth'] * $widthFactor; $height = $totalHeight; $colorForeground = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); $png = new \Imagick(); $png->newImage($width, $height, 'none', 'png'); $imageMagickObject = new \imagickdraw(); $imageMagickObject->setFillColor($colorForeground); $imageMagickObject->setStrokeAntialias(false); $imageMagickObject->setStrokeColor('rgb(255,255,255)'); // print bars $positionHorizontal = 0; foreach ($barcodeData['bars'] as $bar) { $bw = round($bar['width'] * $widthFactor, 3); $bh = round($bar['height'] * $totalHeight / $barcodeData['maxHeight'], 3); if ($bar['drawBar']) { $y = round($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight'], 3); // draw a vertical bar $imageMagickObject->rectangle($positionHorizontal, $y, $positionHorizontal + $bw, $y + $bh); } $positionHorizontal += $bw; } $png->drawImage($imageMagickObject); return $png; }
/** * Return a .png 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. * @public */ public function getBarcodePNGPath($code, $type, $w = 2, $h = 30, $color = array(0, 0, 0)) { if (!$this->store_path) { //$this->setStorPath(\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; $png = imagecreate($width, $height); $bgcol = imagecolorallocate($png, 255, 255, 255); imagecolortransparent($png, $bgcol); $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]); } elseif (extension_loaded('imagick')) { $imagick = true; $bgcol = new imagickpixel('rgb(255,255,255'); $fgcol = new imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); $png = new Imagick(); $png->newImage($width, $height, 'none', 'png'); $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 ($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($png, $x, $y, $x + $bw, $y + $bh, $fgcol); } } $x += $bw; } $file_name = $this->slug($code); $save_file = $this->checkfile($this->store_path . $file_name . ".png"); if ($imagick) { $png->drawimage($bar); //echo $png; } if (ImagePng($png, $save_file)) { imagedestroy($png); return str_replace($this->store_path, '', $save_file); } else { imagedestroy($png); return $code; } }
/** * Return a PNG image representation of barcode (requires GD or Imagick library). * @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 image or false in case of error. * @public */ public function getBarcodePngData($w = 2, $h = 30, $color = array(0, 0, 0)) { // calculate image size $width = $this->barcode_array['maxw'] * $w; $height = $h; if (function_exists('imagecreate')) { // GD library $imagick = false; $png = imagecreate($width, $height); $bgcol = imagecolorallocate($png, 255, 255, 255); imagecolortransparent($png, $bgcol); $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]); } elseif (extension_loaded('imagick')) { $imagick = true; $bgcol = new imagickpixel('rgb(255,255,255'); $fgcol = new imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); $png = new Imagick(); $png->newImage($width, $height, 'none', 'png'); $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 ($v['t']) { $y = round($v['p'] * $h / $this->barcode_array['maxh'], 3); // draw a vertical bar if ($imagick) { $bar->rectangle($x, $y, $x + $bw - 1, $y + $bh - 1); } else { imagefilledrectangle($png, $x, $y, $x + $bw - 1, $y + $bh - 1, $fgcol); } } $x += $bw; } if ($imagick) { $png->drawimage($bar); return $png; } else { ob_start(); imagepng($png); $imagedata = ob_get_clean(); imagedestroy($png); return $imagedata; } }
/** * Get the barcode as PNG image (requires Imagick library) * * @return object * * @throws BarcodeException if the Imagick library is not installed */ public function getPngDataImagick() { $rgbcolor = $this->color_obj->getNormalizedArray(255); $bar_color = new \imagickpixel('rgb(' . $rgbcolor['R'] . ',' . $rgbcolor['G'] . ',' . $rgbcolor['B'] . ')'); $img = new \Imagick(); $img->newImage($this->width + $this->padding['L'] + $this->padding['R'], $this->height + $this->padding['T'] + $this->padding['B'], 'none', 'png'); $barcode = new \imagickdraw(); $barcode->setfillcolor($bar_color); foreach ($this->bars as $bar) { if ($bar[2] > 0 && $bar[3] > 0) { $barcode->rectangle($this->padding['L'] + $bar[0] * $this->width_ratio, $this->padding['T'] + $bar[1] * $this->height_ratio, $this->padding['L'] + ($bar[0] + $bar[2]) * $this->width_ratio - 1, $this->padding['T'] + ($bar[1] + $bar[3]) * $this->height_ratio - 1); } } $img->drawimage($barcode); return $img; }
/** * Get the barcode as PNG image (requires Imagick library) * * @return object * * @throws BarcodeException if the Imagick library is not installed */ public function getPngDataImagick() { $img = new \Imagick(); $width = ceil($this->width + $this->padding['L'] + $this->padding['R']); $height = ceil($this->height + $this->padding['T'] + $this->padding['B']); $img->newImage($width, $height, 'none', 'png'); $barcode = new \imagickdraw(); if ($this->bg_color_obj !== null) { $rgbcolor = $this->bg_color_obj->getNormalizedArray(255); $bg_color = new \imagickpixel('rgb(' . $rgbcolor['R'] . ',' . $rgbcolor['G'] . ',' . $rgbcolor['B'] . ')'); $barcode->setfillcolor($bg_color); $barcode->rectangle(0, 0, $width, $height); } $rgbcolor = $this->color_obj->getNormalizedArray(255); $bar_color = new \imagickpixel('rgb(' . $rgbcolor['R'] . ',' . $rgbcolor['G'] . ',' . $rgbcolor['B'] . ')'); $barcode->setfillcolor($bar_color); $bars = $this->getBarsArray('XYXY'); foreach ($bars as $rect) { $barcode->rectangle($rect[0], $rect[1], $rect[2], $rect[3]); } $img->drawimage($barcode); return $img->getImageBlob(); }
/** * Return a PNG image representation of barcode (requires GD or Imagick library). * @param $w (int) Width of a single rectangle element in pixels. * @param $h (int) Height of a single rectangle element in pixels. * @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent). * @return image or false in case of error. * @public */ public function getBarcodePNG($w = 3, $h = 3, $color = array(0, 0, 0)) { // calculate image size $width = $this->barcode_array['num_cols'] * $w; $height = $this->barcode_array['num_rows'] * $h; if (function_exists('imagecreate')) { // GD library $imagick = false; $png = imagecreate($width, $height); $bgcol = imagecolorallocate($png, 255, 255, 255); imagecolortransparent($png, $bgcol); $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]); } elseif (extension_loaded('imagick')) { $imagick = true; $bgcol = new imagickpixel('rgb(255,255,255'); $fgcol = new imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); $png = new Imagick(); $png->newImage($width, $height, 'none', 'png'); $bar = new imagickdraw(); $bar->setfillcolor($fgcol); } else { return false; } // print barcode elements $y = 0; // for each row for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) { $x = 0; // for each column for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) { if ($this->barcode_array['bcode'][$r][$c] == 1) { // draw a single barcode cell if ($imagick) { $bar->rectangle($x, $y, $x + $w, $y + $h); } else { imagefilledrectangle($png, $x, $y, $x + $w, $y + $h, $fgcol); } } $x += $w; } $y += $h; } // send headers header('Content-Type: image/png'); header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1 header('Pragma: public'); header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); if ($imagick) { $png->drawimage($bar); echo $png; } else { imagepng($png); imagedestroy($png); } }
/** * Return a PNG image representation of barcode (requires GD or Imagick library). * @param $w (int) Width of a single rectangle element in pixels. * @param $h (int) Height of a single rectangle element in pixels. * @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent). * @return image or false in case of error. * @public */ public function getBarcodePngData($w = 3, $h = 3, $color = array(0, 0, 0)) { // calculate image size $width = $this->barcode_array['num_cols'] * $w; $height = $this->barcode_array['num_rows'] * $h; if (function_exists('imagecreate')) { // GD library $imagick = false; $png = imagecreate($width, $height); $bgcol = imagecolorallocate($png, 255, 255, 255); imagecolortransparent($png, $bgcol); $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]); } elseif (extension_loaded('imagick')) { $imagick = true; $bgcol = new imagickpixel('rgb(255,255,255'); $fgcol = new imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); $png = new Imagick(); $png->newImage($width, $height, 'none', 'png'); $bar = new imagickdraw(); $bar->setfillcolor($fgcol); } else { return false; } // print barcode elements $y = 0; // for each row for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) { $x = 0; // for each column for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) { if ($this->barcode_array['bcode'][$r][$c] == 1) { // draw a single barcode cell if ($imagick) { $bar->rectangle($x, $y, $x + $w - 1, $y + $h - 1); } else { imagefilledrectangle($png, $x, $y, $x + $w - 1, $y + $h - 1, $fgcol); } } $x += $w; } $y += $h; } if ($imagick) { $png->drawimage($bar); return $png; } else { ob_start(); imagepng($png); $imagedata = ob_get_clean(); imagedestroy($png); return $imagedata; } }
/** * Return a .png file path which create in server * <li>$arrcode['code'] code to be printed on text label</li> * <li>$arrcode['num_rows'] required number of rows</li> * <li>$arrcode['num_cols'] required number of columns</li> * <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul> * @param $code (string) code to print * @param $type (string) type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul> * @param $w (int) Width of a single rectangle element in pixels. * @param $h (int) Height of a single rectangle element in pixels. * @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent). * @return path of image whice created * @public */ public function getBarcodePNGPath($code, $type, $w = 3, $h = 3, $color = array(0, 0, 0)) { if ($this->store_path) { $this->setStorPath(\Config::get("barcode::store_path")); } //set barcode code and type $this->setBarcode($code, $type); // calculate image size $width = $this->barcode_array['num_cols'] * $w; $height = $this->barcode_array['num_rows'] * $h; if (function_exists('imagecreate')) { // GD library $imagick = false; $png = imagecreate($width, $height); $bgcol = imagecolorallocate($png, 255, 255, 255); imagecolortransparent($png, $bgcol); $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]); } elseif (extension_loaded('imagick')) { $imagick = true; $bgcol = new imagickpixel('rgb(255,255,255'); $fgcol = new imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); $png = new Imagick(); $png->newImage($width, $height, 'none', 'png'); $bar = new imagickdraw(); $bar->setfillcolor($fgcol); } else { return false; } // print barcode elements $y = 0; // for each row for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) { $x = 0; // for each column for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) { if ($this->barcode_array['bcode'][$r][$c] == 1) { // draw a single barcode cell if ($imagick) { $bar->rectangle($x, $y, $x + $w, $y + $h); } else { imagefilledrectangle($png, $x, $y, $x + $w, $y + $h, $fgcol); } } $x += $w; } $y += $h; } $save_file = $this->checkfile($this->store_path . $code . ".png"); if ($imagick) { $png->drawimage($bar); //echo $png; } if (ImagePng($png, $save_file)) { imagedestroy($png); return str_replace(public_path(), '', $save_file); } else { imagedestroy($png); return $code; } }
/** * Return a PNG image representation of barcode (requires GD or Imagick library). * @param $code (string) code to print * @param $type (string) type of barcode * @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 image or false in case of error. */ public static function getPNG($code, $type, $w = 2, $h = 30, $color = array(0, 0, 0)) { $barcode = self::setBarcode($code, $type); // calculate image size $width = $barcode['maxw'] * $w; $height = $h; if (function_exists('imagecreate')) { // GD library $imagick = false; $png = imagecreate($width, $height); $bgcol = imagecolorallocate($png, 255, 255, 255); imagecolortransparent($png, $bgcol); $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]); } elseif (extension_loaded('imagick')) { $imagick = true; $bgcol = new \imagickpixel('rgb(255,255,255'); $fgcol = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); $png = new \Imagick(); $png->newImage($width, $height, 'none', 'png'); $bar = new \imagickdraw(); $bar->setfillcolor($fgcol); } else { return false; } // print bars $x = 0; foreach ($barcode['bcode'] as $k => $v) { $bw = round($v['w'] * $w, 3); $bh = round($v['h'] * $h / $barcode['maxh'], 3); if ($v['t']) { $y = round($v['p'] * $h / $barcode['maxh'], 3); // draw a vertical bar if ($imagick) { $bar->rectangle($x, $y, $x + $bw, $y + $bh); } else { imagefilledrectangle($png, $x, $y, $x + $bw, $y + $bh, $fgcol); } } $x += $bw; } ob_start(); // get image out put if ($imagick) { $png->drawimage($bar); echo $png; } else { imagepng($png); imagedestroy($png); } $image = ob_get_clean(); $image = base64_encode($image); //$image = 'data:image/png;base64,' . base64_encode($image); return $image; }
/** * Return a .png file path which create in server * * @param string $code * @param string $type * @param int $w * @param int $h * @param array $color * * @return bool * * @throws \Exception */ public function getBarcodePNGPath($code, $type, $w = 2, $h = 30, $color = array(0, 0, 0), $filename = null) { if (is_null($filename)) { $filename = $type . '_' . $code; } $this->setBarcode($code, $type); $bar = null; // calculate image size $width = $this->barcodeArray['maxw'] * $w; $height = $h; if (empty($this->barcodeArray) || !$this->barcodeArray) { throw new \Exception('It not possible to generate barcode of type: ' . $type . ' for number/code: ' . $code . '! May be this is an invalid code pattern!'); } if (function_exists('imagecreate')) { // GD library $imagick = false; $png = imagecreate($width, $height); $bgcol = imagecolorallocate($png, 255, 255, 255); imagecolortransparent($png, $bgcol); $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]); } elseif (extension_loaded('imagick')) { $imagick = true; $fgcol = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); $png = new \Imagick(); $png->newImage($width, $height, 'none', 'png'); } else { return false; } // print bars $x = 0; $sharp = 3; $bwPerc = 100; foreach ($this->barcodeArray['bcode'] as $v) { $bw = round(round($v['w'] * $w, $sharp) * $bwPerc / 100, $sharp); $bh = round($v['h'] * $h / $this->barcodeArray['maxh'], $sharp); if ($v['t']) { $y = round($v['p'] * $h / $this->barcodeArray['maxh'], $sharp); // draw a vertical bar if ($imagick) { $bar = new \imagickdraw(); $bar->setfillcolor($fgcol); $bar->rectangle($x, $y, $x + $bw - 1, $y + $bh - 1); } else { imagefilledrectangle($png, $x, $y, $x + $bw - 1, $y + $bh - 1, $fgcol); } } $x += $bw; } $nType = str_replace('+', 'PLUS', $type); $this->setTempPath($this->savePath); $saveFile = $this->checkfile($this->savePath . $filename . '.png', true); if ($imagick) { $png->drawimage($bar); } if (imagepng($png, $saveFile)) { imagedestroy($png); return $saveFile; } else { imagedestroy($png); throw new \Exception('It not possible to write barcode cache file to path ' . $this->savePath); } }
/** * Return a .png file path which create in server * * @param string $code * @param string $type * @param int $w * @param int $h * @param array $color * * @throws \Exception * * @return bool */ public function getBarcodePNGPath($code, $type, $w = 3, $h = 3, $color = array(0, 0, 0), $filename = null) { if (is_null($filename)) { $filename = $type . '_' . $code; } //set barcode code and type $this->setBarcode($code, $type); $bar = null; if (empty($this->barcodeArray) || !$this->barcodeArray) { throw new \Exception('It not possible to generate barcode of type: ' . $type . ' for number/code: ' . $code . '! May be this is an invalid code pattern!'); } // calculate image size $width = $this->barcodeArray['num_cols'] * $w; $height = $this->barcodeArray['num_rows'] * $h; if (function_exists('imagecreate')) { // GD library $imagick = false; $png = imagecreate($width, $height); $bgcol = imagecolorallocate($png, 255, 255, 255); imagecolortransparent($png, $bgcol); $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]); } elseif (extension_loaded('imagick')) { $imagick = true; $fgcol = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); $png = new \Imagick(); $png->newImage($width, $height, 'none', 'png'); } else { return false; } // print barcode elements $y = 0; // for each row for ($r = 0; $r < $this->barcodeArray['num_rows']; ++$r) { $x = 0; // for each column for ($c = 0; $c < $this->barcodeArray['num_cols']; ++$c) { if ($this->barcodeArray['bcode'][$r][$c] == 1) { // draw a single barcode cell if ($imagick) { $bar = new \imagickdraw(); $bar->setfillcolor($fgcol); $bar->rectangle($x, $y, $x + $w - 1, $y + $h - 1); } else { imagefilledrectangle($png, $x, $y, $x + $w - 1, $y + $h - 1, $fgcol); } } $x += $w; } $y += $h; } $nType = str_replace('+', 'PLUS', $type); $this->setTempPath($this->savePath); $saveFile = $this->checkfile($this->savePath . $filename . '.png', true); if ($imagick) { $png->drawimage($bar); //echo $png; } // ImagePng : weazL if (imagepng($png, $saveFile)) { imagedestroy($png); return $saveFile; } else { imagedestroy($png); throw new \Exception('It not possible to write barcode cache file to path ' . $this->savePath); } }
/** * Return a PNG image representation of barcode (requires GD or Imagick library). * @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 image or false in case of error. * @public */ public function getBarcodePngData($w = 2, $h = 30, $color = array(0, 0, 0), $code = '') { // calculate image size $width = $this->barcode_array['maxw'] * $w; $height = $h; $FontNum = 2; $FontHeight = ImageFontHeight($FontNum); $FontWidth = ImageFontWidth($FontNum); if (function_exists('imagecreate')) { // GD library $imagick = false; $png = imagecreate($width, $height + $FontHeight); $bgcol = imagecolorallocate($png, 255, 255, 255); imagecolortransparent($png, $bgcol); $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]); //----------------------- font ------------------------------------------------------ $BarcodeFull = "*" . strtoupper($code) . "*"; $CenterLoc = (int) (($width - 1) / 2) - (int) ($FontWidth * strlen($BarcodeFull) / 2); ImageString($png, $FontNum, $CenterLoc, $height + $FontHeight - $FontHeight, $BarcodeFull, $fgcol); //--------------------------------------------- FONT ------------------------ } elseif (extension_loaded('imagick')) { $imagick = true; $bgcol = new imagickpixel('rgb(255,255,255'); $fgcol = new imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); $png = new Imagick(); $png->newImage($width, $height, 'none', 'png'); $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 ($v['t']) { $y = round($v['p'] * $h / $this->barcode_array['maxh'], 3); // draw a vertical bar if ($imagick) { $bar->rectangle($x, $y, $x + $bw - 1, $y + $bh - 1); } else { imagefilledrectangle($png, $x, $y, $x + $bw - 1, $y + $bh - 1, $fgcol); } } $x += $bw; } if ($imagick) { $png->drawimage($bar); return $png; } else { ob_start(); imagepng($png); $imagedata = ob_get_clean(); imagedestroy($png); //return $imagedata; return base64_encode($imagedata); //return "<img src='data:img/png;,base64,'".base64_encode($imagedata)." />" } }