Search code examples
imagecodeignitercodeigniter-2image-resizingcodeigniter-3

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


Here is my controller function, please help me to create the thumbs of both images. Only the images are uploaded to the folder. i created a function named resize to create the thumbs. that's also given in the controller.

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');

        }

    }

}

Resize function

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

Solution

  • First of all you are loading library two times in your function add please load it one time probably at the top of function.

    in resize use $this->image_lib->initialize($config) as below

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