使用黑色背景CodeIgniter 3.x调整图像大小

时间:2017-10-31 06:10:57

标签: php image codeigniter upload image-resizing

控制器:我在上传前调整图像大小的位置,图像正在调整大小,并且当用户上传新图像时也会删除旧的上传图像。但唯一的问题是,它以黑色背景上传,当[create_thumb] = TRUE时,只有缩略图被调整大小并且图像处于原始分辨率。

出了什么问题?

上传的图片黑色背景点击查看:

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');
}

0 个答案:

没有答案