• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Specifying a source_image in do_upload

#1
[eluser]theif_of_always[/eluser]
Ok, so I am trying to get my code to be able to upload and re-size ANY image you want to upload (within the png, gif and jpg parameters), as opposed to just specifying the exact filename.

So, I have done this:

$config['source_image'] = '../books/uploads/fakeimage.jpg';

But this only allows you to upload a specific file called fakeimage.jpg. How do I give the source_image the freedom to pic any image it wants.

Please help me with a solution based on my current code. Thanks in advance!


My Controller Functions:
Code:
public function do_upload(){
    
      
     $usernameArray = array();
  
    $this->load->model('LoginModel');
  
     $usernameQuery = $this->LoginModel->get_users();
  
      foreach ($usernameQuery->result_array() as $username)
      {
        $usernameArray[$username['userID']] = $username['username'];
  
      }
  
    $data['username'] = $usernameArray;
      
     $config['upload_path'] = '../books/uploads/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '100';
  $config['max_width']  = '1024';
  $config['max_height']  = '768';
  
  $config['image_library'] = 'gd/gd2';
  $config['source_image'] = '../books/uploads/fakeimage.jpg';

        
  $config['create_thumb'] = TRUE;
  $config['maintain_ratio'] = TRUE;
  $config['width'] = 75;
  $config['height'] = 50;


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

  $this->image_lib->resize();
  
  if (!$this->image_lib->resize())
  {
       echo $this->image_lib->display_errors();
  }

  $this->load->library('upload', $config);

  if ( !$this->upload->do_upload())
  
  {

   $update = array('userID'=>   $_POST['username'],'avatar'=>$_POST['file_upload']);
   $data['user_id'] =  $_POST['username'];
   $data['avatar'] = $_POST['file_upload'];
  
   $this->load->model('LoginModel');
  
      $usernameQuery = $this->LoginModel->update_user_image($update);
    
   $this->load->view('upload_form_view', $data);
  }
  else
  {
   $data = array('upload_data' => $this->upload->data());
      
   $data['upload_data'] = $this->upload->data();
  
   $this->load->view('upload_form_view', $data);
  
  }  
  
   }

Code:
public function upload_form_view(){
  
  
           $usernameArray = array();
  
     $this->load->model('LoginModel');
  
      $usernameQuery = $this->LoginModel->get_users();
  
      foreach ($usernameQuery->result_array() as $username)
      {
        $usernameArray[$username['userID']] = $username['username'];
  
      }
  
        $data['username'] = $usernameArray;
  
         $this->load->view("upload_form_view",$data);
  
   }
My Model Function:

Code:
public function update_user_image($formData)
    {
        $this->db->where('userID', $formData['userID']);
        $this->db->update("users", $formData);
        
        
        $data = array('upload_data' => $this->upload->data());
   $data['upload_data'] = $this->upload->data();
    }

#2
[eluser]Cristian Gilè[/eluser]
You are mixing the config array of the upload library and the image library. You can resize an image only after you have uploaded it.

#3
[eluser]meer[/eluser]


Code:
function do_upload()
{
config['upload_path'] = './your_dolder_path/';
   $config['allowed_types'] = 'gif|jpg|png|jpeg';
  $config['max_width']  = '1024';
   $config['max_height']  = '768';
         $field_name = 'image';// image field name of your view
   $this->load->library('upload', $config);
    if ( ! $this->upload->do_upload($field_name))
    {
                echo"error";
              return FALSE;
              }
              else
              {
  $image_data = $this->upload->data();
             $filename = $image_data['file_name'];
             $config = array
            (
             'source_image' => $image_data['full_path'],
             'new_image' => './uploads/thumbs/',
             'maintain_ratio' => ftrue,
             'width' => 75,
             'height' => 50
   );
  $this->load->library('image_lib', $config);
             $this->image_lib->resize();  
                .....


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.