Welcome Guest, Not a member yet? Register   Sign In
image upload ,form validation, and image resize
#1

[eluser]matt2012[/eluser]
Having spent a bit of time on this and seeing others here doing the same thought id post my controller here, which will:

1) validate form
2) validate upload
3) upload image
4) create thumbnail *edit changed to GD2 for better quality thumbnails
5) insert data into table

you will need to load validation and form library somewhere first.

There's my stuff in there which can be ignored (i.e. basic page set up).

Code:
function edit_service_6()
    {        
        //basic page setup
        $data = template($this->data['options']);    
        $pid = $this->input->post('id_dir1');
        $id = $this->uri->segment(3,$pid);        
        $data['id'] = $id;
        $data['content'] = 'admin/edit_service_6';
        //upload set up
        $data['error'] = '';
        $data['upload_data'] = '';
        //check if folder exists
        if (!is_dir("./services/".$data['name'])){
        //if not create it with correct permissions
        mkdir ("./services/".$data['name'], 0777); }
        $config['upload_path'] = './services/'.$data['name'];
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '500';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        //form set up
        $rules['title']    = "required|min_length[5]";    
        $fields['title']  = 'title';    
        $this->validation->set_rules($rules);
        $this->validation->set_fields($fields);    
        //thumbnail set up
        $config2['image_library'] = 'GD2';      
        $config2['create_thumb'] = TRUE;
        $config2['maintain_ratio'] = TRUE;
        $config2['width'] = 300;
        $config2['height'] = 200;
        
        // check whether form has been run and validates
        if ($this->validation->run() == FALSE)
        {
        //if no do nothing
        }
        //else yes so do upload
        else
        {
        $this->load->library('upload', $config);
            //if upload not success display errors
            if ( ! $this->upload->do_upload())
            {
                $data['error'] = $this->upload->display_errors();        
            }
            //else do form insert, image upload and image resize!!
            else
            {        
            //upload data
            $data['upload_data'] = $this->upload->data();
            //resize image
            //needed this from upload data
            $config2['source_image'] = $data['upload_data']['full_path'];
            $this->load->library('image_lib', $config2);
            $this->image_lib->resize();        
            //form insert
            $type = str_replace('image/','.',$data['upload_data']['file_type']);
            $this->Servicemodel->insert_photo($data['upload_data']['raw_name'], $data['upload_data']['file_name'], $type);                
            }    
        }    
        $data['images'] = $this->Servicemodel->get_service_photos($id);
        $this->load->view('MyViews/container2', $data);
    }
#2

[eluser]gunter[/eluser]
Hi!
this was very helpfull! The user guide is nice, but seeing the more than one library in action and in an short (!) example is a good lesson!
#3

[eluser]elvix[/eluser]
exactly what i was looking for, thanks!!
#4

[eluser]abmcr[/eluser]
I have try with the code proposed and modified by me in
Code:
function articoli_edit(){
        $this->load->helper(array('form', 'url'));
        $this->load->library('validation');
        
        $rules['title']    = "required";
        $this->validation->set_rules($rules);
        $fields['title']    = 'Titolo';
        $this->validation->set_fields($fields);

        if ($this->uri->segment(3)=="modify"){
            $query = $this->db->query("SELECT * FROM articles where article_id=".$this->uri->segment(4)." LIMIT 1");
            foreach($query->result_array() as $value)
            {
                   foreach($value as $key => $value){
                         $this->validation->$key = $value;
                    }
             }
        }
        if ($this->validation->run() == FALSE)
        {
            $this->load->view('edita', array('error' => ' ' ));
        }
        else
        {
             //upload set up
        $data['error'] = '';
            $config['upload_path'] = 'uploads/';
            $config['allowed_types'] = 'gif|jpg|png|zip';
            $config['max_size']    = '10000';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            $this->load->library('upload', $config);
        //if upload not success display errors
        if ( ! $this->upload->do_upload('file'))
        {
          $data['error'] = $this->upload->display_errors();        
        }
        else
        {        
          //get upload data
          $data['upload_data'] = $this->upload->data();              
        }    
        $this->db->set("title",$this->input->post("title"));
        $this->db->set("file",(isset($data['upload_data']['file_name'])?$data['upload_data']['file_name']:""));
        //edita o inserisce
        if ($this->uri->segment(3)=="modify"){
            $this->db->where('article_id',$this->uri->segment(4));
            $this->db->update('articles');
        }else  {
            $this->db->set('author_id',$this->uri->segment(5));
            $this->db->insert('articles');
        }
        redirect ("form/articoli/".$this->uri->segment(5));
        }
    }

All work fine, but if i have an error in the upload (for example a file too big), i want to stop the update or insert in the database and i want to reload the form with the value of others fields as edited .....
How to do this? Thank you.....




Theme © iAndrew 2016 - Forum software by © MyBB