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 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂