Pathik Vejani
Pathik Vejani

Reputation: 4501

Save generated bar code image in database and in folder using codeigniter

I have used library to generate barcode. Bar code is generated but problem is that how can i save that generated image?

Here i am giving my code i have used:

In controller:

function add($bar_code)
{
   $postData['bar_code'] = $this->set_barcode($postData['bar_code']);
}

function set_barcode($code)
{
    $this->load->library('zend');
    $this->zend->load('Zend/Barcode');
    $bar_code = Zend_Barcode::render('code128', 'image', array('text'=>$code), array('imageType' => 'jpg'))->draw();
    return $bar_code;
}

How can i save the image of generated bar code?

Help as son as possible.

Thanks!

Upvotes: 0

Views: 7402

Answers (2)

Mahfuzur Rahman
Mahfuzur Rahman

Reputation: 1545

We can save barcode image into a directory or we can save it into database or both.

function set_barcode($code){
    //load library
    $this->load->library('zend');
    //load in folder Zend
    $this->zend->load('Zend/Barcode');

    //generate barcode
    $barcode = Zend_Barcode::factory('code128', 'image', array('text' => $code, 'barHeight'=>30, 'factor'=>2), array('imageType' => 'png'));

    //set dir path for barcode image store
    $path = './you/dir/path/'.$code.'.gif';
    imagegif($barcode->draw(), $path);

    /* if you want to permanently store your barcode image, and 
       save the path into your database, 
       just return this path. */
    // return $path

    //convert image into base64
    $code_img_base64 = base64_encode(file_get_contents($path));

    //if you want, remove the temporary barcode image
    unlink($path);

    return $code_img_base64;
}

Upvotes: 0

Pathik Vejani
Pathik Vejani

Reputation: 4501

I got the solution:

function set_barcode($code)
{
   $this->load->library('zend');
   $this->zend->load('Zend/Barcode');
   $file = Zend_Barcode::draw('code128', 'image', array('text' => $code), array());
   $code = time().$code;
   $store_image = imagepng($file,"../barcode/{$code}.png");
   return $code.'.png';
}

store the image using imgepng function.
it will store the bar code image in barcode folder.

Upvotes: 7

Related Questions