【问题标题】:Save generated bar code image in database and in folder using codeigniter使用 codeigniter 将生成的条形码图像保存在数据库和文件夹中
【发布时间】:2015-03-16 18:45:28
【问题描述】:

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

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

在控制器中:

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

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

尽可能帮助儿子。

谢谢!

【问题讨论】:

  • 谁能帮忙解决这个问题?
  • 你不能只对图像文件进行fopen()fwrite() 吗?
  • 你能显示var_dump($bar_code);的结果吗?
  • 当我写 var_dump($bar_code)n 时它只给了我条形码图像
  • 谁能举个例子?

标签: codeigniter barcode


【解决方案1】:

我得到了解决方案

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 函数存储图像。
它将条形码图像存储在条形码文件夹中。

【讨论】:

    【解决方案2】:

    我们可以将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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 2014-12-11
      • 2011-12-22
      • 1970-01-01
      • 2013-06-23
      • 2015-10-28
      • 2014-12-17
      相关资源
      最近更新 更多