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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用