Controller: Where I'm resizing an image before uploading, the image is resizing properly and the old uploaded image is also deleted when the new image is uploaded by the user. But the only problem is, it gets uploaded with black background, and when the [create_thumb]=TRUE, only the thumbnail gets resized and the image is in its original resolution.
What's going wrong?
Uploaded image black background click to view:
public function img_upload()
{
if(!$this->session->userdata('logged_in_user'))
{
redirect('login');
}
$id = $this->session->userdata('id');
$oldimg = $this->input->post('oldimg', TRUE);
$extension = explode(".", $_FILES["userfile"]["name"]);
$newfilename = time().random_string('numeric',5) . '.' . end($extension);
$_FILES['userfile']['name'] = $newfilename;
$file = $_FILES['userfile']['name'];
$path = FCPATH.'assets/uploads/profilepic/'.$id;
$post_image = '';
if(!is_dir($path))
{
$dir = mkdir($path,0777,TRUE);
// print_r($dir); die('Unable to create folder');
}
$this->load->library('image_lib');
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 15000;
$config['max_width'] = 0;
$config['max_height'] = 0;
$this->load->library('upload', $config);
if(!$this->upload->do_upload('userfile'))
{
$this->session->set_flashdata('file_error', $this->upload->display_errors());
$user = $this->profile_model->getprofile($id);
$post_image = $user['user_image'];
}
else
{
$data = $this->upload->data();
$configer['image_library'] = 'gd2';
$configer['source_image'] = $data['full_path'];
// $configer['new_image'] = $path."_new";
// $configer['create_thumb'] = TRUE;
$configer['maintain_ratio'] = FALSE;
$configer['width'] = 700;
$configer['height'] = 450;
// Load the Library
$this->image_lib->clear();
$this->image_lib->initialize($configer);
// resize image
$this->image_lib->resize();
// handle if there is any problem
if ( ! $this->image_lib->resize()){
echo $this->image_lib->display_errors();
}
$post_image = 'assets/uploads/profilepic/'.$id.'/'.$file;
}
$retu = $this->profile_model->user_image($post_image,$id);
if($retu)
{
if(!empty($file))
{
$delimg = FCPATH.$oldimg;
unlink($delimg);
}
}
$this->session->set_flashdata('profile','Your profile image updated successfully');
redirect('profile/vabout');
}