首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在PHP GrayScale CodeIgniter中获取图像

如何在PHP GrayScale CodeIgniter中获取图像
EN

Stack Overflow用户
提问于 2018-07-07 21:28:47
回答 1查看 341关注 0票数 0

如何在CodeIgniter PHP中使用图像过滤器,我想在灰度图像中转换用户输入的图像,我在PHP框架中尝试这一点,它不是将图像转换为灰度,什么都没有显示。

以下是我的控制器代码:

代码语言:javascript
复制
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);
    }
}  

查看代码:

代码语言:javascript
复制
<form method="post" id="upload_form" enctype="multipart/form-data">
<input type='file' name="userfile" size="20" onchange="readURL(this);"/>

结果视图:

代码语言:javascript
复制
<div class="invert-grid-item Result">
<img src="<?php echo $img2;?>">            
</div> 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-07 23:47:12

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

代码语言:javascript
复制
    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);
    }
}  
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51227073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档