在使用codeigniter绘制之前,必须向条形码提供文本

问题描述:

I use zend libraries to generate barcode in codeigniter.

This is my controller :

$this->load->library('zend');
    $this->zend->load('Zend/Barcode'); 
    $barcode = $this->input->post('barcode'); //nomor id barcode
    $imageResource = Zend_Barcode::factory('code128', 'image', array('text'=>$barcode), array())->render();

    $imageName = $barcode.'.jpg';
    $imagePath = 'barcode/'; // penyimpanan file barcode
     imagejpeg($imageResource, $imagePath.$imageName); 
    $pathBarcode = $imagePath.$imageName; 

    $kd_barang = $this->input->post('kd_barang');

    $pathBarcode = $this->input->post('barcode');



    $editdata=array(
        /*Nama Field => $Nama Variabel*/

        'barcode' => $pathBarcode
    );


    /*Primary Key Sebagai Kunci*/
    $where=array(
        'kd_barang'=>$kd_barang
    );
    /*Mengambil Function Dari Model*/
    $this->m_operator->aksi_update_barang($where,$editdata,'barang');
    redirect('c_op/index');

This is my view :

<form action="<?php echo base_url(). 'index.php/c_op/aksi_editbarang'; ?>" method="post">

    <center>

    <table border="1">
        <?php 

        foreach ($edit->result() as $c){?>
        <tr>
            <td>Kode Barang</td>
            <!-- Primary Key Sebagai Kunci -->
            <td><input type="text" name="kd_barang" value="<?php echo $c->kd_barang ?>" readonly></input></td>

        <tr>
            <td>Barcode</td>
            <td><input type="text" name="barcode" value="<?php echo $c->kd_barang ?>" readonly><?php echo $c->barcode;?></td>
        </tr>
        <tr>
            <td><button type="submit">UPDATE</button></td>
        </tr>
        <?php } ?>
    </table>

    </center>

</form>

But the barcode could not be drawn because it said "A text must be provide to Barcode befeore drawing". But i have already declare that the text for $barcode is $kd_barang. It's saved to the database but as a text, not as image. Please help me.

For whatever reason you aren't getting the $barcode value. I suspect the view you posted isn't directly related.

The following code will validate input to some degree and fix the path issue.

    $this->load->library('zend');
    $this->zend->load('Zend/Barcode');
    $barcode = $this->input->post('barcode'); //nomor id barcode
    $kd_barang = $this->input->post('kd_barang');
    if (is_null($barcode) || is_null($kd_barang)) {
        show_error('Missing parameters');
    }
    // make barcode
    $imageResource = Zend_Barcode::factory('code128', 'image', array('text' => $barcode), array())->render();
    $imageName = $barcode . '.jpg';
    $imagePath = 'barcode/'; // penyimpanan file barcode
    imagejpeg($imageResource, $imagePath . $imageName);
    $pathBarcode = $imagePath . $imageName;
    $editdata = array(
        'barcode' => $pathBarcode
    );
    $where = array(
        'kd_barang' => $kd_barang
    );
    $this->m_operator->aksi_update_barang($where, $editdata, 'barang');
    //redirect('c_op/index'); commented out for debugging