Codeigniter文件在上传时调整大小


Codeigniter file resizing on upload

我一直在尝试上传图像并调整其大小。上传似乎终于可以工作了,但没有调整大小。

在我的构造函数中,我正在加载库

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

然后在我的函数中,我调用调整大小并上传

    $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');
     }

我也试过这个

    $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');
     }  

我有什么东西不见了吗?我也试过在最初上传后调整大小,但似乎也没有什么效果。任何帮助都将不胜感激。

$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['upload_path'] = './HTML/files/';
$config['file_path'] = './HTML/files/photo1.jpg';
$config['source_image'] = $config['file_path'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width']     = 75;
$config['height']   = 50;
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();

如果以上代码不起作用,请检查上传图像的文件夹权限。

您也可以通过以下方式授予777对文件夹的权限:-

sudo chmod -R 777 path

解决方案肯定会帮助您

我对这段代码进行了多次检查,最终解决了这个问题。当你有的时候

$config['create_thumb'] = TRUE;

在您的代码中,它将创建一个具有调整大小比例的缩略图,如果您不告诉它去哪里,它将把它隐藏在文件夹中。所以调整大小是有效的,只是不在正确的图像上。

为了解决这个问题,我将上面的更改为.

$config['create_thumb'] = FALSE;