2015-05-18 62 views
1

我有2个codeigniter函数用于图像上传和拇指创建。图像上传功能正常工作,但创建拇指功能不起作用。 我不知道我的功能中有什么错误?Codeigniter创建缩略图无法运作?

function uploaded() //upload function 
{   
      $config['upload_path'] ='./uploads/'; 
      $config['allowed_types'] ='jpeg|jpg|png|gif|bmp|JPEG|JPG|PNG|GIF|BMP|doc|docx|xlsx|txt'; 
      $config['max_size'] = $this->config->item('max_upload_size'); 
      $filename = strtolower($this->friendly($_FILES['image']['name'])); 
      $config['file_name'] = $filename; 
      $config['remove_spaces'] = TRUE; 
      $this->load->library('upload', $config); 
      if ($this->upload->do_upload('image')) 
      { 
      $this->data['file'] = $this->upload->data(); 
      $this->thumb($filename); 
      return true; 
      } 

} 

在代码中创建拇指功能

function thumb($filename) 
{ 

$config['image_library'] = 'gd2'; 
$config['source_image'] = './uploads/'.$filename.'.jpg'; 
$config['create_thumb'] = TRUE; 
$config['maintain_ratio'] = TRUE; 
$config['width'] = 75; 
$config['height'] = 50; 
$config['new_image'] = './uploads/thumb/'.$filename; 
$this->load->library('image_lib', $config); 

$this->image_lib->initialize($config); 
$this->image_lib->resize(); 
} 
+0

我试着不让工作.. –

+0

什么是上传的路径从根文件夹 – Saty

+0

为什么有这可能发生的几个原因。首先,将'$ this-> image_lib-> resize();'改为'if(!$ this-> image_lib-> resize()){die($ this-> image_lib-> display_errors());}'。这会告诉你是否有任何标准错误。请让我知道错误是什么(如果有的话)。如果没有人请你让我知道你使用的是什么操作系统。 –

回答

0

点点修改。

上传功能更改。

function uploaded() //upload function 
{   
      $config['upload_path'] ='./uploads/'; 
      $config['allowed_types'] ='jpeg|jpg|png|gif|bmp|JPEG|JPG|PNG|GIF|BMP|doc|docx|xlsx|txt'; 
      $config['max_size'] = $this->config->item('max_upload_size'); 
      //$filename = strtolower($this->friendly($_FILES['image']['name'])); #your filename has been commented 
      $filename = preg_replace('/[^a-zA-Z0-9_.]/', '_', $file_name); //replace special char including whitespace with _ 
      $filename = preg_replace('/_+/', '_', $file_name); //replace multiple _ with single one. 
      $config['file_name'] = $filename; 
      $config['remove_spaces'] = TRUE; 
      $this->load->library('upload', $config); 
      if ($this->upload->do_upload('image')) 
      { 
      $this->data['file'] = $this->upload->data(); 
      $this->thumb($filename); 
      return true; 
      } 

} 

Thumb函数的变化。

function thumb($filename) 
{ 

$config['image_library'] = 'gd2'; 
//$config['source_image'] = './uploads/'.$filename.'.jpg'; #no need to make it static as you are allowing multiple extensions in allowed_types. 
$config['source_image'] = './uploads/'.$filename; 
$config['create_thumb'] = TRUE; 
$config['maintain_ratio'] = TRUE; 
$config['width'] = 75; 
$config['height'] = 50; 
$config['new_image'] = './uploads/thumb/'.$filename; 
$this->load->library('image_lib', $config); 

$this->image_lib->initialize($config); 
$this->image_lib->resize(); 
}