dtcu5885 2019-02-05 06:59
浏览 57
已采纳

在上传时调整CodeIgniter中的图像并将调整后的图像保存在目录中

I want to resize an image while uploading it, and save that resized image using CodeIgniter. I tried some code, but doesn't work. Also I want to know how to apply this in multiple files upload too.Can someone help

public function submited()
{
  //$this->load->library('image_lib');
  $config['upload_path'] = './images';
  $config['allowed_types'] = 'gif|jpg|png|jpeg';

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

  if($this->upload->do_upload('myfile'))
  {
    $filedata = $this->upload->data();
    $this->load->library('image_lib');
    $this->load->library('image_lib');
    $config['allowed_types']='jpg|jpeg|png|gif';
    $config['image_library'] = 'gd2';
    $config['source_image'] = './images'; 
    $config['new_image'] = './images'; 
    $config['maintain_ratio'] = FALSE; 
    $config['width'] = 200;
    $config['height'] = 150; 
    $config['quality']   = 75;
    $this->image_lib->initialize($config);
    $this->image_lib->resize();
    $this->image_lib->clear(); 

  }
}
  • 写回答

2条回答 默认 最新

  • dsf5632 2019-02-05 09:15
    关注

    Create a helper function. And call helper function with the uploaded image file path. It will replace your image with the new size.

    public function submitd()
    {
             $config['upload_path'] = './images';
             $config['allowed_types'] = 'gif|jpg|png|jpeg';
             $this->load->library('upload',$config);
             if($this->upload->do_upload('myfile'))
            {
               $filedata = $this->upload->data();
               $this->resize_image($filedata['full_path']);
            }
    }
    //you can create this function in helper or in same controller.
    function resize_image($file_path) {
      $CI =& get_instance();
        // Set your config up
        $config['image_library']    = "gd2";      
        $config['source_image']     = $file_path;      
        $config['create_thumb']     = TRUE;      
        $config['maintain_ratio']   = TRUE;     
        $config['new_image'] = $file_path; 
        $config['width'] = "320";      
        $config['height'] = "240";  
        $config['thumb_marker']=FALSE;
        $CI->load->library('image_lib');
    
        $CI->image_lib->initialize($config);
        // Do your manipulation
    
        if(!$CI->image_lib->resize())
        {
           $CI->image_lib->display_errors();  
        } 
        $CI->image_lib->clear(); 
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗