使用 codeigniter 将生成的条形码图像保存在数据库和文件夹中

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

我使用库生成条形码。条形码已生成,但问题是如何保存生成的图像?

这里我给出我使用过的代码:

在控制器中:

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;
}

如何保存生成的条码图像?

尽可能帮助儿子。

谢谢!

我得到了解决方案

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';
}

使用 imgepng 函数存储图像。
它将条码图像存储在条码文件夹中。

我们可以将 barcode 图像保存到一个目录中,或者我们可以将它保存到数据库中,或者两者都保存。

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;
}