0

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');
}
Pang
  • 9,564
  • 146
  • 81
  • 122
  • add this and check `$config['maintain_ratio'] = TRUE;` – Abdulla Nilam Oct 31 '17 at 06:16
  • I've already tried that @AbdullaNilam not working. – Sohail Shaikh Oct 31 '17 at 06:18
  • set this both TRUE `$configer['create_thumb'] = TRUE; $configer['maintain_ratio'] = TRUE;` – Abdulla Nilam Oct 31 '17 at 06:19
  • Now the thumbnail is resized but the image remains in its original resolution @AbdullaNilam – Sohail Shaikh Oct 31 '17 at 06:22
  • Please read the resize() part of the image lib documentation. The original file isn't touched unless create thumb and or new image is not defined or default. What you are experiencing is the default behavior. If you want to resize the original image after processing the thumbnail initialize the image lib without the above options as per the docs. – Alex Jan 30 '18 at 21:58

0 Answers0