如何在PHP CodeIgniter中灰度图像

时间:2018-07-07 21:28:48

标签: php codeigniter

如何在CodeIgniter PHP中使用图像过滤器,我想将用户输入图像转换为灰度图像,我正在PHP CodeIgniter框架中尝试此操作, 它没有将图像转换为灰度,什么也没显示。

这是我的控制器代码:

public function grayscale(){
    $data['title'] ='Grayscale Image';
    $config['upload_path'] = './assets/images/grayscale';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '2048';
    $config['max_width'] = '2000';
    $config['max_height'] = '2000';
    $this->load->library('upload', $config);
    if(!$this->upload->do_upload()){
        $errors = array('error' => $this->upload->display_errors());
        $post_image = 'noimage.jpg';
    } else {
        $data = array('upload_data' => $this->upload->data());
        $realImages = imagecreatefrompng($_FILES['userfile']['tmp_name']);
        $data['img2']=imagefilter($realImages, IMG_FILTER_GRAYSCALE);       
        print $this->load->view('pages/result',$data,true);
    }
}  

查看代码:

<form method="post" id="upload_form" enctype="multipart/form-data">
<input type='file' name="userfile" size="20" onchange="readURL(this);"/>

结果视图:

<div class="invert-grid-item Result">
<img src="<?php echo $img2;?>">            
</div> 

1 个答案:

答案 0 :(得分:1)

尝试一下,如果您不想将图像保存在另一个目录中,只需将$config['upload_path'].'/'.更改为另一个路径

    public function grayscale(){
    $data['title'] ='Grayscale Image';
    $config['upload_path'] = './assets/images/grayscale';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '2048';
    $config['max_width'] = '2000';
    $config['max_height'] = '2000';
    $this->load->library('upload', $config);
    if(!$this->upload->do_upload()){
        $errors = array('error' => $this->upload->display_errors());
        $post_image = 'noimage.jpg';
    } else {
        $data = array('upload_data' => $this->upload->data());
        $realImages = imagecreatefrompng($_FILES['userfile']['tmp_name']);

        $data['img2']=$config['upload_path'].'/'.$_FILES['userfile']['name'];

        imagefilter($realImages, IMG_FILTER_GRAYSCALE);       
        imagepng($realImages, $data['img2']);

        print $this->load->view('pages/result',$data,true);
    }
}