Thursday, 15 August 2013

Saving Magento Barcode image in excel -



Saving Magento Barcode image in excel -

i trying save generate , save barcode in excel. however, having problem saving image in right format. image saved in lastly column. but, not sure how save it.

hope can help. code below:

<?php require_once '../app/mage.php'; umask(0); mage::app(); mage::app()->loadarea(mage_core_model_app_area::area_frontend); header("content-type:text/octect-stream"); header("content-disposition:attachment;filename=exportmyeancodes.csv"); $eanprefixcode="0123456"; $storeid = mage::app()->getstore()->getid(); $product = mage::getmodel('catalog/product'); $products = $product->getcollection()->addstorefilter($storeid)->getallids(); echo '"sku","ean","barcode"'. "\n"; foreach($products $productid) { $product = mage::getmodel('catalog/product')->load($productid); $sku = $product->getsku(); $ean=ean13_check_digit($eanprefixcode. str_pad($product->getid(), 5, "0", str_pad_left)); $bc = new barcode(); //$bc->build($ean); $output='"'. $sku. '","'. $ean. '","'.$bc->build($ean).'"'; echo $output. "\n"; } $bc = new barcode(); $bc->build($ean); function ean13_check_digit($digits){ $digits =(string)$digits; $even_sum = $digits{1} + $digits{3} + $digits{5} + $digits{7} + $digits{9} + $digits{11}; $even_sum_three = $even_sum * 3; $odd_sum = $digits{0} + $digits{2} + $digits{4} + $digits{6} + $digits{8} + $digits{10}; $total_sum = $even_sum_three + $odd_sum; $next_ten = (ceil($total_sum/10))*10; $check_digit = $next_ten - $total_sum; homecoming $digits . $check_digit; } class barcode { public $bcheight, $bcthinwidth, $bcthickwidth, $bcfontsize, $mode; function __construct($mode='gif', $height=50, $thin=2, $thick=3, $fsize=2) { $this->bcheight = $height; $this->bcthinwidth = $thin; $this->bcthickwidth = $this->bcthinwidth * $thick; $this->fontsize = $fsize; $this->mode = $mode; $this->outmode = array('gif'=>'gif', 'png'=>'png', 'jpeg'=>'jpeg', 'wbmp'=>'vnd.wap.wbmp'); $this->codemap = array( '0'=>'010000101', '1'=>'100100001', '2'=>'001100001', '3'=>'101100000', '4'=>'000110001', '5'=>'100110000', '6'=>'001110000', '7'=>'000100101', '8'=>'100100100', '9'=>'001100100', 'a'=>'100001001', 'b'=>'001001001', 'c'=>'101001000', 'd'=>'000011001', 'e'=>'100011000', 'f'=>'001011000', 'g'=>'000001101', 'h'=>'100001100', 'i'=>'001001100', 'j'=>'000011100', 'k'=>'100000011', 'l'=>'001000011', 'm'=>'101000010', 'n'=>'000010011', 'o'=>'100010010', 'p'=>'001010010', 'q'=>'000000111', 'r'=>'100000110', 's'=>'001000110', 't'=>'000010110', 'u'=>'110000001', 'v'=>'011000001', 'w'=>'111000000', 'x'=>'010010001', 'y'=>'110010000', 'z'=>'011010000', ' '=>'011000100', '$'=>'010101000', '%'=>'000101010', '*'=>'010010100', '+'=>'010001010', '-'=>'000110100', '.'=>'110000100', '/'=>'010100010' ); } public function build($text='', $showtext=true, $filename=null) { if (trim($text) <= ' ') throw new exception('barcode::build - must passed text operate'); if (!$filetype = $this->outmode[$this->mode]) throw new exception("barcode::build - unrecognized output format ({$this->mode})"); if (!function_exists("image{$this->mode}")) throw new exception("barcode::build - unsupported output format ({$this->mode} - check phpinfo)"); $text = strtoupper($text); $disptext = "* $text *"; $text = "*$text*"; // adds start , stop chars $textlen = strlen($text); $barcodewidth = $textlen * (2 * $this->bcthinwidth + 3 * $this->bcthickwidth) - $this->bcthinwidth; $im = imagecreate($barcodewidth, $this->bcheight); $black = imagecolorallocate($im, 0, 0, 0); $white = imagecolorallocate($im, 255, 255, 255); imagefill($im, 0, 0, $white); $xpos = 0; ($idx=0; $idx<$textlen; $idx++) { if (!$char = $text[$idx]) $char = '-'; ($ptr=0; $ptr<=8; $ptr++) { $elementwidth = ($this->codemap[$char][$ptr]) ? $this->bcthickwidth : $this->bcthinwidth; if (($ptr + 1) % 2) imagefilledrectangle($im, $xpos, 0, $xpos + $elementwidth-1, $this->bcheight, $black); $xpos += $elementwidth; } $xpos += $this->bcthinwidth; } if ($showtext) { $pxwid = imagefontwidth($this->fontsize) * strlen($disptext) + 10; $pxht = imagefontheight($this->fontsize) + 2; $bigcenter = $barcodewidth / 2; $textcenter = $pxwid / 2; imagefilledrectangle($im, $bigcenter - $textcenter, $this->bcheight - $pxht, $bigcenter + $textcenter, $this->bcheight, $white); imagestring($im, $this->fontsize, ($bigcenter - $textcenter) + 5, ($this->bcheight - $pxht) + 1, $disptext, $black); } $badmode = false; if (!$filename) header("content-type: image/{$filetype}"); switch($this->mode) { case 'gif': imagegif($im, $filename); break; case 'png': imagepng($im, $filename); break; case 'jpeg': imagejpeg($im, $filename); break; case 'wbmp': imagewbmp($im, $filename); break; default: $badmode = true; } imagedestroy($im); if ($badmode) throw new exception("barcode: unknown graphics type '{$this->mode}'"); } } ?>

i did testing , i, too, saw lot of same characters!

perhaps alternative method: have script output desired csv file, in 3rd field, create url. create little script in var/export/sku.php:

<?php require_once '../../app/mage.php'; umask(0); mage::app(); mage::app()->loadarea(mage_core_model_app_area::area_frontend); if( isset($_get['sku']) && strlen($_get['sku']) > 5 ) { header('content-type: image/jpeg'); $barcodeoptions = array('text' => $_get['sku']); $rendereroptions = array(); $imageresource = zend_barcode::draw( 'code39', 'image', $barcodeoptions, $rendereroptions ); imagejpeg($imageresource); } ?>

then in script, have (sku),ean,http://yoursite.com/var/export/sku.php?sku=(sku) there way have spreadsheet load images -- though haven't done personally, have seen links displayed embedded images.

my sample above outputs code39 barcode, scannable. can replace own image generation. why asked if saw barcodes, haven't output rendered images before.

excel magento

No comments:

Post a Comment