[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();
}