当 2 个不同的图像上传到 2 个不同的文件夹时,图像 uploaded.but 不会创建缩略图

When 2 different images are uploaded to 2 different folders,then the images are uploaded.but the thumbs are not created

这是我的控制器函数,请帮我创建两个图像的拇指。只有图像会上传到文件夹中。我创建了一个名为 resize 的函数来创建拇指。这也在控制器中给出。

public function add() {

    $this->load->helper(array('form', 'url'));
    $this->load->helper('file');
    $this->load->library('form_validation');


    $this->form_validation->set_rules('txtPrdname', 'Product Name', 'trim|required|htmlspecialchars');
    $this->form_validation->set_rules('sbPrdcategory', 'Product Category', 'trim|required|htmlspecialchars');
    $this->form_validation->set_rules('sbPrduser', 'Managing User', 'trim|required|htmlspecialchars');
    $this->form_validation->set_rules('txtPrdprofile', 'Product Profile', 'trim|required|htmlspecialchars');

    if ($this->form_validation->run() == FALSE) {

        $data_view["error"] = "";

        $this->load->view('moderator/templates/header');
        $this->load->view('moderator/templates/sidebar');
        $this->load->view('moderator/b2bproduct_add', $data_view);
        $this->load->view('moderator/templates/footer');

    } else {

        // Image uploading codes

        $config['upload_path'] = 'assets/images/b2bproduct';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = '1000';
        $config['max_width'] = '2024';
        $config['max_height'] = '1768';
        $config['overwrite'] = TRUE;
        $config['remove_spaces'] = TRUE;

        if (isset($_FILES['filePrdimage']['name'])) {
            $config['file_name'] = substr(md5(time()), 0, 28) . $_FILES['filePrdimage']['name'];
        }

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

        $this->upload->initialize($config);

        if (!$this->upload->do_upload('filePrdimage')) {

            //no file uploaded or failed upload

            $error = array('error' => $this->upload->display_errors());

        } else {

            $dat = array('upload_data' => $this->upload->data());

            $this->load->library('upload');
            $this->upload->initialize($config);
            $this->resize($dat['upload_data']['full_path'], 'assets/images/b2bproduct/thump/' . $dat['upload_data']['file_name'], 180, 400);
        }

        if (empty($dat['upload_data']['file_name'])) {
            $prdimage = '';
        } else {
            $prdimage = $dat['upload_data']['file_name'];
        }    

        // End Image uploading Codes
        // Logo uploading codes

        $config['upload_path'] = 'assets/images/b2blogo';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = '1000';
        $config['max_width'] = '2024';
        $config['max_height'] = '1768';
        $config['overwrite'] = TRUE;
        $config['remove_spaces'] = TRUE;

        if (isset($_FILES['filePrdlogo']['name'])) {
            $config['file_name'] = substr(md5(time()), 0, 28) . $_FILES['filePrdlogo']['name'];
        }

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

        $this->upload->initialize($config);

        if (!$this->upload->do_upload('filePrdlogo')) {

            //no file uploaded or failed upload

            $error = array('error' => $this->upload->display_errors());

        } else {

            $dat1 = array('upload_data' => $this->upload->data());

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

            $this->resize($dat1['upload_data']['full_path'], 'assets/images/b2blogo/thump/' . $dat1['upload_data']['file_name'], 180, 400);
        }

        if (empty($dat1['upload_data']['file_name'])) {
            $prdlogo = '';
        } else {
            $prdlogo = $dat1['upload_data']['file_name'];
        }

        // End Logo uploading Codes         

        $data = array(
            'prd_name' => $this->input->post('txtPrdname'),
            'prd_category' => $this->input->post('sbPrdcategory'),
            'prd_user' => $this->input->post('sbPrduser'),
            'prd_profile' => $this->input->post('txtPrdprofile'),
            'prd_oem' => $this->input->post('rbtnPrdoem'),
            'prd_protype' => $this->input->post('rbtnPrdprotype'),
            'prd_image' => $prdimage,
            'prd_ranktype' => $this->input->post('sbPrdranktype'),
            'prd_points' => $this->input->post('txtPrdpoints'),
            'prd_extrakey' => $this->input->post('txtPrdextrakey'),
            'prd_dated' => time(),
            'prd_ipadd' => $_SERVER['REMOTE_ADDR']
        );

        $result_id = $this->b2bproduct_model->add($data);

        if ($result_id) {

            redirect(base_url() . 'moderator/b2bproduct/view/' . $result_id, 'refresh');

        } else {

            $data_view["error"] = "Data can't insert due to database error";

            $this->load->view('moderator/templates/header');

            $this->load->view('moderator/templates/sidebar');

            $this->load->view('moderator/b2bproduct_add', $data_view);

            $this->load->view('moderator/templates/footer');

        }

    }

}

调整大小功能

public function resize($source, $destination, $width, $height) {
    $config['image_library'] = 'gd2';
    $config['source_image'] = $source;
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['width'] = $width;
    $config['height'] = $height;
    $config['new_image'] = $destination;
    $this->load->library('image_lib', $config);
    $this->image_lib->resize();
}

难不成你点赞的文件夹(assets/images/b2bproduct/thump/assets/images/b2blogo/thump/)不存在?

这很可能是一个简单的拼写错误,例如 thump 而不是 thumb

编辑:

您真的不必多次加载 uploadimage_lib 库。一开始就做一次。之后,您可以使用 $this->upload->initialize($config);$this->image_lib->initialize($config); 到您现在尝试重新加载库的所有这些地方。

为了使您的代码正常工作,您至少应在 resize 函数中的 $this->image_lib->resize(); 之前添加 $this->image_lib->initialize($config);

首先,您在 function add 中加载了两次库,请在函数顶部加载一次。

在调整大小时使用 $this->image_lib->initialize($config),如下所示

public function resize($source, $destination, $width, $height) {
    $config['image_library'] = 'gd2';
    $config['source_image'] = $source;
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['width'] = $width;
    $config['height'] = $height;
    $config['new_image'] = $destination;
    $this->load->library('image_lib');
    $this->image_lib->initialize($config);
    $this->image_lib->resize();
}