Codeigniter中的图像裁剪

时间:2017-11-09 13:01:26

标签: php codeigniter

我想剪裁我的图片上传并更改我的个人资料照片,但我不知道为什么即使图片上传也无法正常工作,个人资料图片不会更改,您必须退出并且图片是更改。这是我的控制者。

private $pk = 'nim';
private $table = 'mahasiswa';

public function update()
    {
        $nim = $this->session->userdata('nim');
        if($_POST) { 
            $file = $this->upload();
            if ($this->m_data->update($this->pk, $nim, $this->table, $this->field_data($file['data']))) {

            redirect('Mahasiswa/Profil');
    }
} else {
    redirect('Mahasiswa/Formubah');
}
}

private function field_data($file = '')
{

    $data['username'] = $this->input->post('username');
    $data['password'] = $this->input->post('password');
    if ($file != '') {
        $data['foto'] = $file['file_name'];
    }

    return $data;

}

public function upload()
{
    $config['upload_path'] = './assets/image/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = 0;
    $config['remove_spaces'] = TRUE;

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

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

        return[
            'data' => $this->upload->display_errors(),
        ];

    } else {

        $data = $this->upload->data();
        $resize['image_library'] = 'gd2';
        $resize['x_axis'] = 50;
        $resize['y_axis'] = 50;
        $resize['width'] = $img['config']['foto']['width'];

        $this->image_lib->initialize($resize);

        $this->image_lib->crop();

        return [ 
        'data' => $this->upload->data(),
        ];



    }
}

我希望控制器足够了。我忘了告诉,我用这个节目我的照片。 <?php echo base_url('assets/image/').$this->session->userdata('foto');?>

1 个答案:

答案 0 :(得分:0)

您没有正确实施该库。你忘记了图书馆的负载而没有给出它的路径。

$image_data = $this->upload->data();
$config['image_library'] = 'imagemagick';
$config['library_path'] = 'C:ImageMagick-7.0.1-3-portable-Q16-x86';//your library
$config['source_image'] = $image_data['full_path']; //get original image
$config['x_axis'] = 50;
$config['y_axis'] = 50;
$this->load->library('image_lib', $config);
if (!$this->image_lib->crop()) {
    $this->handle_error($this->image_lib->display_errors());
}