barcodeFactory = new Barcode(); } /** * Generate barcode as SVG, save and get saved file name. * * @param $code * @param $type * @param int $w * @param int $h * @param string $color * * @return string * * @throws \Com\Tecnick\Barcode\Exception * @throws \Com\Tecnick\Color\Exception */ public function getGeneratedBarcodeSVGFileName($code, $type, $w = 2, $h = 30, $color = 'black') { // If UPCE use old library https://github.com/tecnickcom/TCPDF with fix from https://github.com/milon/barcode/issues/27 if ('UPCE' === $type) { $tcpdfBarcode = new \TCPDFBarcode($code, 'UPCE'); $svgContent = $tcpdfBarcode->getBarcodeSVGcode(8, 480, 'black'); } else { // Get SVG code for generated barcode. $svgContent = $this->barcodeFactory ->getBarcodeObj($type, $code, $w, $h, $color) ->setBackgroundColor('white') ->getSvgCode(); } // Save content to file. $fileName = sha1(md5(uniqid(rand(), 1))).'.svg'; $saveFile = $this->checkfile($this->storePath.$fileName); $result = file_put_contents($saveFile, $svgContent); // Return path to file or empty string return false !== $result ? $fileName : ''; } /** * Set store path for generated files. * * @param $path * * @return $this */ public function setStorPath($path) { $this->storePath = $path; return $this; } /** * Check if file exists, remove it. * * @param $path * * @return mixed */ protected function checkfile($path) { // Check if file exists. if (file_exists($path)) { unlink($path); } return $path; } }