【问题标题】:When 2 different images are uploaded to 2 different folders,then the images are uploaded.but the thumbs are not created当 2 个不同的图像上传到 2 个不同的文件夹时,图像会被上传。但不会创建拇指
【发布时间】:2015-09-30 06:59:59
【问题描述】:

这是我的控制器功能,请帮我创建两个图像的拇指。只有图像被上传到文件夹。我创建了一个名为 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();
}

【问题讨论】:

    标签: image codeigniter codeigniter-2 image-resizing codeigniter-3


    【解决方案1】:

    首先,您在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();
    }
    

    【讨论】:

    • :) 不需要超车。我很欣赏你的才华。
    【解决方案2】:

    您的拇指文件夹(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);

    【讨论】:

    • thump 只是文件夹名称。
    • 是的,但是thump 文件夹是否存在于这些目标中?如果没有这样的文件夹,image_lib 库将不会创建拇指。
    • 伙计,我完全重建了您的示例以找出解决方案。查看编辑后的答案!
    • 不要忘记将其中一个答案标记为正确答案,或者至少要对对您有帮助的答案投票;)
    • 其实我是第一次在堆栈溢出中提问。绝对投票给对我有帮助的答案... :) 完成
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 2017-09-27
    • 2018-01-28
    • 1970-01-01
    • 2015-06-02
    相关资源
    最近更新 更多