Welcome Guest, Not a member yet? Register   Sign In
adding image upload to existing function
#1

[eluser]DumpProgrammer[/eluser]
I have followed a tutorial to create my first CI blog and I am looking at expanding it, bit by bit to match my needs. I now need to add images to the blog entries and I have done this tutorial . The problem is this tutorial uses controllers instead of the models where I have put my functions so is it possible then to add these same functions to the models folder and pass the imaage files as a variable in controller? In the controller I would then call the model.
#2

[eluser]DumpProgrammer[/eluser]
I managed to add the function into my model and also created a thumbnail in the process making my life easier. Here is the model below
Code:
function addPost(){
     $now = date("Y-m-d H:i:s");
    $data = array(
        'title' => $this->input->post('title'),
        'tags' => $this->input->post('tags'),
        'status' => $this->input->post('status'),
        'body' => $this->input->post('body'),
        'category_id' => $this->input->post('category_id'),
        'user_id' => $_SESSION['userid'],
        'pubdate' => $now
    
    );
    
        $config['upload_path'] = 'uploads/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = '1000';
        $config['max_width'] = '1920';
        $config['max_height'] = '1280';                        
        
        $this->load->library('upload', $config);
        
        if(!$this->upload->do_upload('image')) echo $this->upload->display_errors();
        else {
            $fInfo = $this->upload->data();
            $this->_createThumbnail($fInfo['file_name']);
            
            $data['image'] = $fInfo['file_name'];
            $data['thumbnail'] = $fInfo['raw_name'] . '_thumb' . $fInfo['file_ext'];                
        }

    $this->db->insert('posts', $data);    
}

function _createThumbnail($fileName) {
        $config['image_library'] = 'gd2';
        $config['source_image'] = 'uploads/' . $fileName;    
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 75;
        $config['height'] = 75;
        
        $this->load->library('image_lib', $config);
        if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
    }




Theme © iAndrew 2016 - Forum software by © MyBB