public function executeGetLabel() { $student = StudentPeer::retrieveByPK($this->getRequestParameter('id')); $this->forward404Unless($student); $this->student = $student; define('IMG_FORMAT_PNG', 1); define('IMG_FORMAT_JPEG', 2); define('IMG_FORMAT_WBMP', 4); define('IMG_FORMAT_GIF', 8); require 'FColor.php'; require 'BarCode.php'; require 'FDrawing.php'; include 'code128.barcode.php'; $color_black = new FColor(0, 0, 0); $color_white = new FColor(255, 255, 255); $code_generated = new code128(32, $color_black, $color_white, 1, $student->getCode(), 3); $drawing = new FDrawing(1024, 1024, '', $color_white); $drawing->init(); $drawing->add_barcode($code_generated); $drawing->draw_all(); $im = $drawing->get_im(); #$im2 = imagecreate($code_generated->lastX,$code_generated->lastY); $im2 = imagecreate(300, 100); imagecopyresized($im2, $im, 0, 0, 0, 0, 300, 100, 300, 100); ##imagecopyresized($im2, $im, 0, 0, 0, 0, $code_generated->lastX, $code_generated->lastY, $code_generated->lastX, $code_generated->lastY); $drawing->set_im($im2); $drawing->finish(IMG_FORMAT_PNG); }
public function getBarcodeFilePath() { $stu = $this; $filename = sfConfig::get('sf_root_dir') . '/' . sfConfig::get('sf_app') . '/images/tmp/stu_' . $stu->getCode() . '.png'; $url_filename = sfContext::getInstance()->getRequest()->getRelativeUrlRoot() . '/images/tmp/'; define('IMG_FORMAT_PNG', 1); define('IMG_FORMAT_JPEG', 2); define('IMG_FORMAT_WBMP', 4); define('IMG_FORMAT_GIF', 8); require_once 'FColor.php'; require_once 'BarCode.php'; require_once 'FDrawing.php'; include_once 'code128.barcode.php'; $color_black = new FColor(0, 0, 0); $color_white = new FColor(255, 255, 255); /* Here is the list of the arguments: 1 - Thickness 2 - Color of bars 3 - Color of spaces 4 - Resolution 5 - Text 6 - Text Font (or 0) */ $code_generated = new code128(40, $color_black, $color_white, 2, $stu->getCode(), 0); $drawing = new FDrawing(1024, 1024, $filename, $color_white); $drawing->init(); $drawing->add_barcode($code_generated); $drawing->draw_all(); $im = $drawing->get_im(); $im2 = imagecreate($code_generated->lastX, $code_generated->lastY); imagecopyresized($im2, $im, 0, 0, 0, 0, $code_generated->lastX, $code_generated->lastY, $code_generated->lastX, $code_generated->lastY); $drawing->set_im($im2); $drawing->finish(1); return $url_filename . 'stu_' . $this->getCode() . '.png'; }
function cw_barcode_get($barcode, $type, $width, $height, $image_type = 8) { global $app_main_dir; $file = $app_main_dir . '/include/lib/barcode/' . $type . '.BarCode.php'; if (!is_file($file)) { return; } if (!$width || $width > 5) { $width = 2; } if (!$height) { $height = 40; } include_once $app_main_dir . '/include/lib/barcode/BarCode.php'; include_once $app_main_dir . '/include/lib/barcode/FColor.php'; include_once $app_main_dir . '/include/lib/barcode/FDrawing.php'; include_once $file; if ($image_type == IMG_FORMAT_GIF) { header("Content-Type: image/gif"); } $code = new $type($barcode, $height, $width); $drawing = new FDrawing(); $drawing->init(); $drawing->add_barcode($code); $drawing->draw_all(); $im = $drawing->get_im(); $im2 = imagecreate($code->lastX, $code->lastY); imagecopyresized($im2, $im, 0, 0, 0, 0, $code->lastX, $code->lastY, $code->lastX, $code->lastY); $drawing->set_im($im2); $drawing->finish($image_type); }
$color_black = new FColor(0, 0, 0); $color_white = new FColor(255, 255, 255); /* Here is the list of the arguments: 1 - Thickness 2 - Color of bars 3 - Color of spaces 4 - Resolution 5 - Text 6 - Text Font (0-5) */ $code_generated = new code128(25, $color_black, $color_white, 2, $psContenu, 0, "B"); /* Here is the list of the arguments 1 - Width 2 - Height 3 - Filename (empty : display on screen) 4 - Background color */ $drawing = new FDrawing(4096, 4096, "{$path}/" . $psFileName . ".png", $color_white); $drawing->init(); // You must call this method to initialize the image $drawing->add_barcode($code_generated); $drawing->draw_all(); $im = $drawing->get_im(); // Next line create the little picture, the barcode is being copied inside $im2 = imagecreate($code_generated->lastX, $code_generated->lastY); imagecopyresized($im2, $im, 0, 0, 0, 0, $code_generated->lastX, $code_generated->lastY, $code_generated->lastX, $code_generated->lastY); $drawing->set_im($im2); // Header that says it is an image (remove it if you save the barcode to a file) #header('Content-Type: image/png'); // Draw (or save) the image into PNG format. $drawing->finish(IMG_FORMAT_PNG); } }
public function createProductBarcode($productId) { $dir = Mage::getBaseDir("media") . DS . "barcode" . DS; if (!is_dir_writeable($dir)) { $file = new Varien_Io_File(); $file->checkAndCreateFolder($dir); } $product = Mage::getModel("catalog/product")->load($productId); if (!$product->getId()) { return false; } // Creating some Color (arguments are R, G, B) $color_black = new FColor(0, 0, 0); $color_white = new FColor(255, 255, 255); /* Here is the list of the arguments: 1 - Thickness 2 - Color of bars 3 - Color of spaces 4 - Resolution 5 - Text 6 - Text Font (0-5) */ if (intval(Mage::getStoreConfig("barcode/product/conversion") == 1)) { switch (intval(Mage::getStoreConfig("barcode/product/barcode_field"))) { case 0: $field = str_pad($productId, 12, "0", STR_PAD_LEFT); break; case 1: $field = substr(number_format(hexdec(substr(md5($product->getSku()), 0, 16)), 0, "", ""), 0, 12); break; case 2: $attr_id = Mage::getStoreConfig("barcode/product/barcode_source"); $attr = Mage::getModel('eav/entity_attribute')->load($attr_id)->getAttributeCode(); $attr_val = $product->getResource()->getAttribute($attr)->getFrontend()->getValue($product); $field = substr(number_format(hexdec(substr(md5($attr_val), 0, 16)), 0, "", ""), 0, 12); break; } } else { $attr_id = Mage::getStoreConfig("barcode/product/barcode_value"); $attr = Mage::getModel('eav/entity_attribute')->load($attr_id)->getAttributeCode(); $attr_val = $product->getResource()->getAttribute($attr)->getFrontend()->getValue($product); $field = $attr_val; } switch (intval(Mage::getStoreConfig("barcode/general/symbology"))) { case 1: $code_generated = new code128(30, $color_black, $color_white, 1, $field, 3, "A"); break; case 2: $code_generated = new code128(30, $color_black, $color_white, 1, $field, 3, "B"); break; case 3: $code_generated = new code128(30, $color_black, $color_white, 1, $field, 3, "C"); break; case 4: $code_generated = new code39(30, $color_black, $color_white, 1, $field, 3); break; case 5: $code_generated = new i25(30, $color_black, $color_white, 1, $field, 3); break; default: $code_generated = new ean13(30, $color_black, $color_white, 1, $field, 3); break; } $this->_initProductBarcode(); /* Here is the list of the arguments 1 - Width 2 - Height 3 - Filename (empty : display on screen) 4 - Background color */ $path = $dir . $productId . ".png"; $drawing = new FDrawing($this->_width, $this->finalHeight, $path, $color_white); $drawing->init(); // You must call this method to initialize the image $drawing->add_barcode($code_generated); $drawing->draw_all(); $im = $drawing->get_im(); $imgBarcode = imagecreate($this->_barcodeWidth, $this->_barcodeHeight); imagecopyresized($imgBarcode, $im, 0, 0, 0, 0, $this->_barcodeWidth, $this->_barcodeHeight, $code_generated->lastX, $code_generated->lastY); if (Mage::getStoreConfig("barcode/product/bcodelayout") == 1) { $im2 = imagecreate($this->finalWidth, $this->finalHeight); } else { $im2 = imagecreate($this->_width, $this->_height); } imagecolorallocate($im2, 255, 255, 255); $textcolor = imagecolorallocate($im2, 0, 0, 0); $this->_y = $this->_barcodeHeight; //INSERT LOGO if ($this->_logo) { $logoType = exif_imagetype($this->_logo["file"]); if ($logoType == 1) { $logoImage = imagecreatefromgif($this->_logo["file"]); } elseif ($logoType == 2) { $logoImage = imagecreatefromjpeg($this->_logo["file"]); } elseif ($logoType == 3) { $logoImage = imagecreatefrompng($this->_logo["file"]); } else { //Mage::app()->_getSession()->addError($this->__("The logo file is not valid")); //Mage::app()->_redirect("adminhtml/system_config/edit/section/barcode"); return false; } $logoResized = imagecreatetruecolor($this->_logo["width"], $this->_logo["height"]); if ($this->_logo["resize"]) { imagecopyresampled($logoResized, $logoImage, 0, 0, 0, 0, $this->_logo["width"], $this->_logo["height"], $this->_logo["file_width"], $this->_logo["file_height"]); } else { $logoResized = $logoImage; } switch ($this->_logo["position"]) { case 1: // left imagecopymerge($im2, $imgBarcode, $this->_logo["width"] + 3, 10, 0, 0, $this->_barcodeWidth, $this->_barcodeHeight, 100); imagecopymerge($im2, $logoResized, 0, 0, 0, 0, $this->_logo["width"], $this->_logo["height"], 100); break; case 2: // right imagecopymerge($im2, $imgBarcode, 3, 3, 0, 0, $this->_barcodeWidth, $this->_barcodeHeight, 100); imagecopymerge($im2, $logoResized, $this->_barcodeWidth + 10, 0, 0, 0, $this->_logo["width"], $this->_logo["height"], 100); break; case 3: // bottom imagecopymerge($im2, $imgBarcode, 3, 3, 0, 0, $this->_barcodeWidth, $this->_barcodeHeight, 100); imagecopymerge($im2, $logoResized, 0, $this->_barcodeHeight + 10, 0, 0, $this->_logo["width"], $this->_logo["height"], 100); $this->_y += $this->_logo["height"] + 10; break; default: // top imagecopymerge($im2, $imgBarcode, 3, $this->_logo["height"] + 10, 0, 0, $this->_barcodeWidth, $this->_barcodeHeight, 100); imagecopymerge($im2, $logoResized, 0, 0, 0, 0, $this->_logo["width"], $this->_logo["height"], 100); $this->_y += $this->_logo["height"] + 10; break; } } else { imagecopymerge($im2, $imgBarcode, 7, 7, 0, 0, $this->_barcodeWidth, $this->_barcodeHeight, 100); } // Init information for write text $xText = 0; $yText = 0; $i = 0; // for inscrease content height $ifont = 0; // without font if (Mage::getStoreConfig('barcode/product/use_font_for_text')) { $ifont = Mage::getStoreConfig('barcode/product/font_size') > 0 ? Mage::getStoreConfig('barcode/product/font_size') : 12; } else { $ifont = 9; } // die(intval($ifont)); $ifont = intval($ifont) * 1.7; if (Mage::getStoreConfig("barcode/product/text_padding_left")) { $xText = intval(Mage::getStoreConfig("barcode/product/text_padding_left")); } else { $xText = 0; } if (Mage::getStoreConfig("barcode/product/text_padding_top")) { $yText = intval(Mage::getStoreConfig("barcode/product/text_padding_top")); } else { $yText = $this->_y + 10; } $this->font = Mage::getBaseDir("skin") . "/adminhtml/default/default/sm/fonts/arial.ttf"; $this->bfont = Mage::getBaseDir("skin") . "/adminhtml/default/default/sm/fonts/barial.ttf"; if (Mage::getStoreConfig("barcode/product/name_visible")) { $productName = $product->getName(); if (strlen($productName) > 31) { $nameLine2 = ""; $nameLine1 = Mage::helper("core/string")->truncate($productName, 31, '', $nameLine2, false); } else { $nameLine2 = ""; $nameLine1 = $productName; } //write product name , manufacturer code and price if (Mage::getStoreConfig("barcode/product/use_font_for_text")) { if (Mage::getStoreConfig("barcode/product/font_size")) { $fontSize = intval(Mage::getStoreConfig("barcode/product/font_size")); } else { $fontSize = 12; } $yText += 10; imagettftext($im2, $fontSize, 0, $xText, $yText, $textcolor, $this->font, $nameLine1); imagettftext($im2, $fontSize, 0, $xText, $yText + $ifont, $textcolor, $this->font, $nameLine2); $i = $nameLine2 ? $ifont : 0; } else { imagestring($im2, 4, $xText, $yText, $nameLine1, $textcolor); imagestring($im2, 4, $xText, $yText + $ifont, $nameLine2, $textcolor); $i = $nameLine2 ? $ifont : 0; } } if (Mage::getStoreConfig("barcode/product/new_slot_visible1")) { $attr_id = Mage::getStoreConfig("barcode/product/new_slot_visible1"); $attr = Mage::getModel('eav/entity_attribute')->load($attr_id)->getAttributeCode(); $attr_val = $product->getResource()->getAttribute($attr)->getFrontend()->getValue($product); // $field = substr(number_format(hexdec(substr(md5($attr_val), 0, 16)), 0, "", ""), 0, 12); if ($attr_val != '') { if (Mage::getStoreConfig("barcode/product/use_font_for_text")) { if (Mage::getStoreConfig("barcode/product/font_size")) { $fontSize = intval(Mage::getStoreConfig("barcode/product/font_size")); } else { $fontSize = 12; } // $yText += 10; imagettftext($im2, $fontSize, 0, $xText, $yText + $i + $ifont, $textcolor, $this->font, $attr_val); $i += $ifont; } else { imagestring($im2, 4, $xText, $yText + $i + $ifont, $attr_val, $textcolor); $i += $ifont; } } } if (Mage::getStoreConfig("barcode/product/new_slot_visible2")) { $attr_id = Mage::getStoreConfig("barcode/product/new_slot_visible2"); $attr = Mage::getModel('eav/entity_attribute')->load($attr_id)->getAttributeCode(); $attr_val = $product->getResource()->getAttribute($attr)->getFrontend()->getValue($product); // $field = substr(number_format(hexdec(substr(md5($attr_val), 0, 16)), 0, "", ""), 0, 12); if ($attr_val != '') { if (Mage::getStoreConfig("barcode/product/use_font_for_text")) { if (Mage::getStoreConfig("barcode/product/font_size")) { $fontSize = intval(Mage::getStoreConfig("barcode/product/font_size")); } else { $fontSize = 12; } // $yText += 10; imagettftext($im2, $fontSize, 0, $xText, $yText + $i + $ifont, $textcolor, $this->font, $attr_val); $i += $ifont; } else { imagestring($im2, 4, $xText, $yText + $i + $ifont, $attr_val, $textcolor); $i += $ifont; } } } if (Mage::getStoreConfig("barcode/product/new_slot_visible3")) { $attr_id = Mage::getStoreConfig("barcode/product/new_slot_visible3"); $attr = Mage::getModel('eav/entity_attribute')->load($attr_id)->getAttributeCode(); $attr_val = $product->getResource()->getAttribute($attr)->getFrontend()->getValue($product); // $field = substr(number_format(hexdec(substr(md5($attr_val), 0, 16)), 0, "", ""), 0, 12); if ($attr_val != '') { if (Mage::getStoreConfig("barcode/product/use_font_for_text")) { if (Mage::getStoreConfig("barcode/product/font_size")) { $fontSize = intval(Mage::getStoreConfig("barcode/product/font_size")); } else { $fontSize = 12; } // $yText += 10; imagettftext($im2, $fontSize, 0, $xText, $yText + $i + $ifont, $textcolor, $this->font, $attr_val); $i += $ifont; } else { imagestring($im2, 4, $xText, $yText + $i + $ifont, $attr_val, $textcolor); $i += $ifont; } } } if (Mage::getStoreConfig("barcode/product/new_slot_visible4")) { $attr_id = Mage::getStoreConfig("barcode/product/new_slot_visible4"); $attr = Mage::getModel('eav/entity_attribute')->load($attr_id)->getAttributeCode(); $attr_val = $product->getResource()->getAttribute($attr)->getFrontend()->getValue($product); // $field = substr(number_format(hexdec(substr(md5($attr_val), 0, 16)), 0, "", ""), 0, 12); if ($attr_val != '') { if (Mage::getStoreConfig("barcode/product/use_font_for_text")) { if (Mage::getStoreConfig("barcode/product/font_size")) { $fontSize = intval(Mage::getStoreConfig("barcode/product/font_size")); } else { $fontSize = 12; } // $yText += 10; imagettftext($im2, $fontSize, 0, $xText, $yText + $i + $ifont, $textcolor, $this->font, $attr_val); $i += $ifont; } else { imagestring($im2, 4, $xText, $yText + $i + $ifont, $attr_val, $textcolor); $i += $ifont; } } } if ($product->getPrice() && Mage::getStoreConfig("barcode/product/price_visible")) { if (Mage::getStoreConfig("barcode/product/use_font_for_text")) { if (Mage::getStoreConfig("barcode/product/font_size")) { $fontSize = intval(Mage::getStoreConfig("barcode/product/font_size")); } else { $fontSize = 12; } // if($product->getFinalPrice() != $product->getPrice()) { // $formattedPrice = Mage::helper('core')->currency($product->getPrice(),true,false); // $priceLenght = strlen($formattedPrice); // imagettftext($im2, $fontSize, 0, $xText, ($yText + $i + $ifont), $textcolor, $this->font, $formattedPrice); // $i += $ifont; // imageline($im2, 15, $yText + $i - (($fontSize)/2), $priceLenght*(($fontSize+1)/2), $yText + $i - (($fontSize)/2), $textcolor); // $formattedPrice = Mage::helper('core')->currency($product->getFinalPrice(),true,false); // imagettftext($im2, $fontSize, 0, $xText + ($priceLenght*(($fontSize*1.8)/2)), ($yText + $i), $textcolor, $this->bfont, $formattedPrice); // $i += $ifont; // } // else { $formattedPrice = Mage::helper('core')->currency($product->getPrice(), true, false); $priceLenght = strlen($formattedPrice); imagettftext($im2, $fontSize, 0, $xText, $yText + $i + $ifont, $textcolor, $this->bfont, $formattedPrice); $i += $ifont; // } } else { // if($product->getFinalPrice() != $product->getPrice()) { // $formattedPrice = Mage::helper('core')->currency($product->getPrice(),true,false); // $priceLenght = strlen($formattedPrice); // // imagettftext($im2, $fontSize, 0, $xText, ($yText + $i + 30), $textcolor, $this->font, $formattedPrice); // imagestring($im2, 4, $xText, ($yText + $i + $ifont), $formattedPrice, $textcolor); // $i += $ifont; // imageline($im2, 15, $yText + $i + 8, $priceLenght*6, $yText + $i + 8, $textcolor); // $formattedPrice = Mage::helper('core')->currency($product->getFinalPrice(),true,false); // // imagettftext($im2, $fontSize, 0, $xText + ($priceLenght*10), ($yText + $i + 10), $textcolor, $this->bfont, $formattedPrice); // imagestring($im2, 4, $xText + $priceLenght*11, ($yText + $i), $formattedPrice, $textcolor); // $i += $ifont; // } // else { $formattedPrice = Mage::helper('core')->currency($product->getPrice(), true, false); $priceLenght = strlen($formattedPrice); // imagettftext($im2, $fontSize, 0, $xText, ($yText + $i + 30), $textcolor, $this->bfont, $formattedPrice); imagestring($im2, 4, $xText, $yText + $i + $ifont, $formattedPrice, $textcolor); $i += $ifont; // } } } // write manufacturer code // Draw (or save) the image into PNG format. $drawing->set_im($im2); if ($drawing->finish(IMG_FORMAT_PNG)) { return $path; } return false; }