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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀