上载时在CodeIgniter中调整图像大小并将其保存在目录中

时间:2019-02-05 06:59:51

标签: php image codeigniter file-upload

我想在上载图像时调整其大小,并使用CodeIgniter保存该调整后的图像。我尝试了一些代码,但是没有用。我也想知道如何将其应用到多个文件上传中。有人可以帮忙

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 个答案:

答案 0 :(得分:1)

要调整图像大小,请使用像这样在codeigniter中的图像库:

    $this->load->library('image_lib');
    $config['allowed_types']='jpg|jpeg|png|gif'; //extension to allow
    $config['image_library'] = 'gd2';
    $config['source_image'] = $file;    //your path uploaded file
    $config['new_image'] = $targetPath;     //path to save the new file
    $config['maintain_ratio'] = FALSE; //to maintain ratio of image
    $config['width'] = 200; // width to resize image
    $config['height'] = 150; 
    $config['quality']   = 75;
    $this->image_lib->initialize($config);
    $this->image_lib->resize();
    $this->image_lib->clear();  `

答案 1 :(得分:0)

创建一个辅助函数。并使用上传的图像文件路径调用帮助程序功能。它将用新尺寸替换您的图像。

int
相关问题