I'm struggling with barcode generation of Zend framework. My goal is generating a barcode image then save to product image while saving products.

I'm not sure my code is correct and I have no idea how to save rendered barcode into product image attribute product_barcode_unit

Please can any help me figure out it ?. I really appreciate your helps.

UPDATE: I currently can generate barcode image while saving product, I used event catalog_product_save_before. But I cannot save barcode image to product gallery images because of this error:

Warning: Invalid argument supplied for foreach() in /srv/magento/foodexporta/production/www/devnew/app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php on line 329

Here is my code trigger in event:

public function generateBarcode($observer)
        $_product = $observer->getEvent()->getProduct();    
        $_params = Mage::app()->getRequest()->getParams();
        $data = $_params['product'];
            $barcodeString = (strlen($data['product_barcode_unit']) > 12) ? substr($data['product_barcode_unit'], 0, 12) : $data['product_barcode_unit'] ;
            // Only the text to draw is required
            $barcodeOptions = array('text' => $barcodeString);

            // No required options
            $rendererOptions = array();

            // Draw the barcode in a new image,
            // send the headers and the image
            $barcodeImage = Zend_Barcode::draw(
                'ean13', 'image', $barcodeOptions, $rendererOptions

            $path = Mage::getBaseDir('media').DS."Barcode".DS.$_product->getId()."-barcode-unit-".$barcodeString.".png";

            $store_image = imagepng($barcodeImage,$path);

                try {
                    $_product->addImageToMediaGallery($path, 'product_barcode_unit', true, false);
                } catch (Mage_Core_Exception $e){
        return $this;

I'm really grateful for your help. .


Use below code. Create Barcode directory in media folder.

$file = Zend_Barcode::draw('code128', 'image', array('text' => $barcodeString), array());
$product = Mage::getModel('catalog/product')->load(<ProductId>);
$store_image = imagepng($file,Mage::getBaseDir('media').$barcode_path);

    $path = Mage::getBaseDir('media') . DS . $barcode_path;

    $product->addImageToMediaGallery($path, 'image', true, false);
  • I really appreciate your answer, it helped me so much to solve my issue. – HungDQ Dec 6 '16 at 3:03
  • For now, I can generate barcode image but cannot save to product image attribute, do you have any ideas ? – HungDQ Dec 6 '16 at 3:45
  • @HungDQ $product->setBarcodeInnerImage($barcode_path); by this you are saving path of image. echo $product->getBarcodeInnerImage() use this to get bar code image path. I am assuming here that you have created barcode_inner_image attribute for product – Gopal Patel Dec 6 '16 at 6:34
  • it's image attribute that show in gallery images of product. I'm dealing with addImageToMediaGallery function but still struggling. – HungDQ Dec 6 '16 at 6:35
  • @HungDQ check updated answer – Gopal Patel Dec 6 '16 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.