CodeIgniter Forums

Full Version: Upload and resize image problem.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, 
I have the below code and i want to upload and resize the image.

The thing is that even though the inputs are changing the image is not uploaded.. I have the same code in some other functions too but i don't get any errors. Any thoughts on that?

Controller:
Code:
public function update_user_data() {
   if ( $this->session->userdata('logged_in') == true ) {
     $user_id = $this->session->userdata('user_id');

     //Image uploading
     $config = array(
       'upload_path' => './assets/img/users/' . $user_id,
       'encrypt_name' => TRUE,
       'allowed_types' => 'gif|jpg|jpeg|png',
       'max_size' => '2048000',
       'max_width' => '4096',
       'max_height' => '4096'
     );
     $this->load->library('upload', $config);
     $dir_exist = true; //Default flag for dir existence

     if ( !is_dir('./assets/img/users/' . $user_id) ) {
       mkdir('./assets/img/users/' . $user_id, 0777, true);
       $dir_exist = false; // dir not exist
     }
     if ( !$this->upload->do_upload('userfile') ) {
       if(!$dir_exist)
       rmdir('./assets/img/users/' . $user_id);
       $image = '';
     } else {
       $config['image_library'] = 'gd2';
       $config['source_image'] = $this->upload->data('full_path');
       $config['maintain_ratio'] = TRUE;
       $config['width'] = 350;
       $config['height'] = 350;

       $this->load->library('image_lib', $config);
       if ( !$this->image_lib->resize() ){
         echo $this->image_lib->display_errors();
       }
       $data = array('upload_data' => $this->upload->data());
       $image = $this->upload->data('file_name');
     }

     $update = $this->user_model->update_user_data($user_id, $image);

     redirect ('users/' . $user_id);
   }
 }


Model:
Code:
public function update_user_data($user_id, $image){
   //Update contact data
   $contact_data = array();
   $contact_data['primary_num'] = $this->input->post('userNum');
   $contact_data['num_1'] = $this->input->post('userNum2');
   $contact_data['num_2'] = $this->input->post('userNum3');
   $contact_data['num_3'] = $this->input->post('userNum4');

   //User data in array
   $data = array();
   if ($this->input->post('userPassword')) {
     $data['password'] = $this->input->post('userPassword');
   }

   if ( !empty($image) ) {
     $data['user_image'] = $image;
   }

   $data['screen_name'] = $this->input->post('userScreenName');
   $data['about'] = $this->input->post('userAbout');
   $data['website'] = $this->input->post('userSite');
   //Update data
   $this->db->update('contact_info', $contact_data, array('to_user_id' => $user_id));

   return $this->db->update('users', $data, array('user_id' => $user_id));

 }


Even though i've done an extensive search in google i couldn't find anything....
Try setting some debug conditions to see where it is failing.

So do an echo in each function to see how far it is getting.

Also go to index.php in the set flag to 'testing' or 'development' to check any errors.
One thing that stands out is you are not calling this function:

$this->image_lib->initialize($config);
(09-08-2018, 02:54 PM)ignitedcms Wrote: [ -> ]One thing that stands out is you are not calling this function:

$this->image_lib->initialize($config);

I have the exact same code in another form without the function call you mentioned and works like charm! I'm trying to figure it out...
OK if that's not the issue check first to see if the folders are being created etc.

I'd start by doing something very simple like creating a controller and form that uploads an image and puts it into a directory already created and crops and resizes it. If that works it is simply a flaw in your logic. Also check to see if gd2 is enabled in php settings.

https://stackoverflow.com/questions/1921...age-resize
(09-08-2018, 08:27 PM)ignitedcms Wrote: [ -> ]OK if that's not the issue check first to see if the folders are being created etc.

I'd start by doing something very simple like creating a controller and form that uploads an image and puts it into a directory already created and crops and resizes it. If that works it is simply a flaw in your logic. Also check to see if gd2 is enabled in php settings.

https://stackoverflow.com/questions/1921...age-resize

You can officially tell that i'm an idiot.... The ONLY thing was that i forgot to add "_multipart" in my form open script. Now it works fine! 

Thank you very much for your time guys!
I appreciate it!
You have to use like this.


$this->load->library('image_lib');
foreach($results as $row) {
    $config['image_library'] = 'gd2';
    $config['source_image'] = '/img/proizvodi/'.$row->proizvodid.'.jpg';
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['width']     = 75;
    $config['height']   = 50;

    $this->image_lib->clear();
    $this->image_lib->initialize($config);
    $this->image_lib->resize();
}

You shouldn't load image_lib in foreach. Try to use the code below


Image Resize Codeigniter Configuration

You need to do simple configuration to resize images, set the configurations properties like image_library, source_image,create_thumb etc width, height as 50 * 50, For that we are going to use inbuilt image library gd2.

// Configuration
 $config['image_library'] = 'gd2';
 $config['source_image'] = './img/images/uploaded/'.$imgName.".jpeg";
 $config['new_image'] = './img/images/uploaded/'.$imgName."_new.jpeg";
 $config['create_thumb'] = TRUE;
 $config['maintain_ratio'] = TRUE;
 $config['width'] = 50;
 $config['height'] = 50;

Load library After Configuration
$this->load->library('image_lib', $config);

Call the resize function
 // resize image
 $this->image_lib->resize();
 // handle if there is any problem
 if ( ! $this->image_lib->resize()){
  echo $this->image_lib->display_errors();
 }

 // resize image
 $this->image_lib->resize();
 // handle if there is any problem
 if ( ! $this->image_lib->resize()){
  echo $this->image_lib->display_errors();
 }

resize() function resize the image file and create new image file in specified folder. You will get errors "$this->image_lib->display_errors()", if there is any problem while image manipulation.

function imageResize50X50($imgName){
 $img_path =  realpath("img")."\\images\\uploaded\\".$imgName.".jpeg";
 
 // Configuration
 $config['image_library'] = 'gd2';
 $config['source_image'] = './img/images/uploaded/'.$imgName.".jpeg";
 $config['new_image'] = './img/images/uploaded/'.$imgName."_new.jpeg";
 $config['create_thumb'] = TRUE;
 $config['maintain_ratio'] = TRUE;
 $config['width'] = 50;
 $config['height'] = 50;

 // Load the Library
 $this->load->library('image_lib', $config);
 
 // resize image
 $this->image_lib->resize();
 // handle if there is any problem
 if ( ! $this->image_lib->resize()){
  echo $this->image_lib->display_errors();
 }
}

Hope this will help you.