duanshan5259 2014-10-11 14:43
浏览 117
已采纳

Codeigniter文件在上传时调整大小

I've been trying to upload and resize an image. The upload seems to be finally be working, but it's not resizing.

In my constructor I am loading the library

$this->load->library('image_lib');

and then in my function I'm calling the the resize and upload

    $this->require_auth();      
    $img = $this->input->post('photo1');

    $config['upload_path'] = './HTML/files/';
    $config['file_name'] = 'photo1.jpg';
    $config['file_path'] = './HTML/files/photo1.jpg';
    $config['max-size'] = 2000;
    $config['image_library'] = 'gd2';
    $config['source_image'] = $config['file_path'];
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = FALSE;
    $config['width']    = 549;
    $config['height']   = 549;
    $config['overwrite'] = TRUE;
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['new_image'] = $config['file_path'];
    $this->load->library('image_lib', $config); 
    $this->image_lib->resize();
    $this->load->library('upload', $config);                


    if ( ! $this->upload->do_upload('photo1')){        
       $error = array('error' => $this->upload->display_errors());
       var_dump($error);
     }else{
         $this->blog_model->editServiceImg1($config['file_path']); 
         redirect('/blog');
     }

I have also tried this

    $this->require_auth();      
    $img = $this->input->post('service_photo1');

    $config['upload_path'] = './HTML/files/';
    $config['file_name'] = 'service_photo1.jpg';
    $config['file_path'] = './HTML/files/service_photo1.jpg';
    $config['max-size'] = 2000;
    $config['overwrite'] = TRUE;
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $this->load->library('upload', $config);                


    if ( ! $this->upload->do_upload('service_photo1')){        
       $error = array('error' => $this->upload->display_errors());
       var_dump($error);
     }else{
        $config['image_library'] = 'gd2';
        $config['source_image'] = $config['file_path'];
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = FALSE;
        $config['width']    = 549;
        $config['height']   = 549;
        $config['new_image'] = $config['file_path'];
        $this->image_lib->initialize($config);
        $this->image_lib->resize();


         $this->blog_model->editServiceImg1($config['file_path']); 
         redirect('/blog');
     }  

Is there something I'm missing? I've tried doing the resize after the initial upload as well and that seemed to do nothing as well. Any help would be much appreciated.

  • 写回答

2条回答 默认 最新

  • dousao6313 2014-10-14 18:41
    关注

    I went through this code many times and finally resolved this issue. It seems that when you have

    $config['create_thumb'] = TRUE;
    

    in your code it will create a thumbnail with the resized proportions and if you do not tell it where to go it will hide it in a folder. So the resizing was working, just not on the right image.

    To resolve this, I changed the above to.

    $config['create_thumb'] = FALSE;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常