将生成的QR码图像保存到路径中

时间:2018-08-02 13:05:08

标签: php codeigniter codeigniter-3 codeigniter-upload

我正在从数据库动态生成QR码,我需要将它们存储在指定的路径中。当生成QR码并单独保存时,但是现在批量生成它们,我需要将它们存储在我的首选路径中。我该如何实现?

这是我的功能

function print_qr1(){
    $qr_code_config = array();
    $qr_code_config['cacheable']    = $this->config->item('cacheable');
    $qr_code_config['cachedir']     = $this->config->item('cachedir');
    $qr_code_config['imagedir']     = $this->config->item('imagedir');
    $qr_code_config['errorlog']     = $this->config->item('errorlog');
    $qr_code_config['ciqrcodelib']  = $this->config->item('ciqrcodelib');
    $qr_code_config['quality']      = $this->config->item('quality');
    $qr_code_config['size']         = $this->config->item('size');
    $qr_code_config['black']        = $this->config->item('black');
    $qr_code_config['white']        = $this->config->item('white');
    $this->ci_qr_code->initialize($qr_code_config);

    // get full name and user details
    $user_details['doctordata'] = $this->user->get_users();
    foreach ($user_details['doctordata'] as $value) {
        $image_name = $value->DOCTORCODE . ".png";

        $codeContents = "www.36d.co.in/qr_code=";
        $codeContents .= $value->DOCTORCODE;
        $codeContents .= "\n";

        $params['data'] = $codeContents;
        $params['level'] = 'H';
        $params['size'] = 10;

        $params['savename'] = FCPATH . $qr_code_config['imagedir'] . $image_name;
        $this->ci_qr_code->generate($params);

        $this->data['qr_code_image_url'] = base_url() . $qr_code_config['imagedir'] . $image_name;

        // save image path in tree table
        $this->user->change_userqr($value->DOCTORCODE);
        // then redirect to see image link
        $file = $params['savename'];
         function do_upload($file){
            $config['upload_path']          = './uploads/qrcode/';
            $config['allowed_types']        = 'png';
            $config['max_size']             = 0;
            $config['max_width']            = 0;
            $config['max_height']           = 0;

            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload($file))
            {
                    $error = array('error' => $this->upload->display_errors());

                    $this->load->view('upload_form', $error);
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());

                    $this->load->view('upload_success', $data);
            }
        }

1 个答案:

答案 0 :(得分:1)

希望此功能对您有帮助:

您的print_qr1方法应如下所示:

function print_qr1()
{
    $qr_code_config = array();
    $qr_code_config['cacheable']    = $this->config->item('cacheable');
    $qr_code_config['cachedir']     = $this->config->item('cachedir');
    $qr_code_config['imagedir']     = $this->config->item('imagedir');
    $qr_code_config['errorlog']     = $this->config->item('errorlog');
    $qr_code_config['ciqrcodelib']  = $this->config->item('ciqrcodelib');
    $qr_code_config['quality']      = $this->config->item('quality');
    $qr_code_config['size']         = $this->config->item('size');
    $qr_code_config['black']        = $this->config->item('black');
    $qr_code_config['white']        = $this->config->item('white');
    $this->ci_qr_code->initialize($qr_code_config);

    // get full name and user details
    $user_details['doctordata'] = $this->user->get_users();
    foreach ($user_details['doctordata'] as $value) {
        $image_name = $value->DOCTORCODE . ".png";

        $codeContents = "www.36d.co.in/qr_code=";
        $codeContents .= $value->DOCTORCODE;
        $codeContents .= "\n";

        $params['data'] = $codeContents;
        $params['level'] = 'H';
        $params['size'] = 10;

        $params['savename'] = FCPATH . $qr_code_config['imagedir'] . $image_name;
        $this->ci_qr_code->generate($params);

        $this->data['qr_code_image_url'] = base_url().$qr_code_config['imagedir']. $image_name;

        // save image path in tree table
        $this->user->change_userqr($value->DOCTORCODE);
        // then redirect to see image link
        $file = $params['savename'];
        my_upload($file);
    }
}

这是您的my_upload方法

function my_upload($file)
{
    $config['upload_path']          = './uploads/qrcode/';
    $config['allowed_types']        = 'png';

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload($file))
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }
}

更多信息:https://www.codeigniter.com/user_guide/libraries/file_uploading.html