Welcome Guest, Not a member yet? Register   Sign In
Upload image and resize it.
#1

[eluser]Andy78[/eluser]
I am trying to upload images and resize them, so far the upload is working and also the creation of thumb nail images. But I cant get the original image to be resized. All that is happening is the file is directly uploaded to the folder in its original size. I guess I am trying to take this image resize it then overwrite the originally uploaded file.

Here is my code what am I missing:

Code:
//Initialise the upload file class
        $config['upload_path'] = './image_uploads/';
$config['allowed_types'] = 'jpg|jpeg|gif|png';
$config['max_size'] = '6048';
        $this->load->library('upload', $config);
        
        $userfile = "_upload_image";
                //check if a file is being uploaded
                if(strlen($_FILES["_upload_image"]["name"])>0){
                    
                    if ( !$this->upload->do_upload($userfile))//Check if upload is unsuccessful
                    {
                        $upload_errors = $this->upload->display_errors('', '');
                        $this->session->set_flashdata('errors', 'Error: '.$upload_errors);  
                        redirect('admin/view_food/'.$food->course_id);  
                    }
                    else
                    {
                        //$image_data = $this->upload->data();
                        
                         ////[ THUMB IMAGE ]
                        $config2['image_library'] = 'gd2';
                        $config2['source_image'] = $this->upload->upload_path.$this->upload->file_name;
                        $config2['new_image'] = './image_uploads/thumbs';
                        $config2['maintain_ratio'] = TRUE;
                        $config2['create_thumb'] = TRUE;
                        $config2['thumb_marker'] = '_thumb';
                        $config2['width'] = 75;
                        $config2['height'] = 50;
                        $this->load->library('image_lib',$config2);
                        
                        if ( !$this->image_lib->resize()){
                    $this->session->set_flashdata('errors', $this->image_lib->display_errors('', ''));  
                  }
                        
                        //[ MAIN IMAGE ]
                        $config['image_library'] = 'gd2';
                        $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
                        //$config['new_image'] = './image_uploads/';
                        $config['maintain_ratio'] = TRUE;
                        $config['width'] = 250;
                        $config['height'] = 250;
                        $this->load->library('image_lib',$config);
                        
                        if ( !$this->image_lib->resize()){
                    $this->session->set_flashdata('errors', $this->image_lib->display_errors('', ''));  
                  }
                   }      
               }
#2

[eluser]Andy78[/eluser]
Iv managed to get it working like this:

Code:
userfile = "_upload_image";
                //check if a file is being uploaded
                if(strlen($_FILES["_upload_image"]["name"])>0){
                    
                    if ( !$this->upload->do_upload($userfile))//Check if upload is unsuccessful
                    {
                        $upload_errors = $this->upload->display_errors('', '');
                        $this->session->set_flashdata('errors', 'Error: '.$upload_errors);  
                        redirect('admin/edit_food/');  
                    }
                    else
                    {
                        $image_data = $this->upload->upload_path.$this->upload->file_name;
                        $this->load->library('image_lib');
                        
                         ////[ THUMB IMAGE ]
                        $config_0['image_library'] = 'gd2';
                        $config_0['source_image'] =  $image_data;
                        $config_0['new_image'] = './image_uploads/thumbs';
                        $config_0['maintain_ratio'] = TRUE;
                        $config_0['create_thumb'] = TRUE;
                        $config_0['thumb_marker'] = '_thumb';
                        $config_0['width'] = 75;
                        $config_0['height'] = 50;
                        
                        //[ MAIN IMAGE ]
                        $config_1['image_library'] = 'gd2';
                        $config_1['source_image'] =  $image_data;
                        $config_1['maintain_ratio'] = TRUE;
                        $config_1['create_thumb'] = FALSE;
                        $config_1['width'] = 250;
                        $config_1['height'] = 250;
                        

                        //iterate and resize image twice
                        for($i=0;$i<2;$i++){
                            
                            eval("\$this->image_lib->initialize(\$config_".$i.");");
                            if(!$this->image_lib->resize()){
                                $this->session->set_flashdata('errors', $this->image_lib->display_errors('', ''));
                                redirect('admin/edit_food/');                            
                            }
                            //clear image resize setting after each iteration
                            $this->image_lib->clear();
                        
                        }
                        
                        
                         $image_info = $this->upload->data();
                         //Get image name for saving
                        $food->image = $image_info['file_name'];
                        //Get thumb nail image name
                        $food->thumb =  image_info['raw_name'].'_thumb'.$image_info['file_ext'];    
                   }      
               }




Theme © iAndrew 2016 - Forum software by © MyBB