Codeigniter创建缩略图不起作用?

时间:2015-05-18 10:27:38

标签: php codeigniter file-upload thumbnails

我有2个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();
}

1 个答案:

答案 0 :(得分: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; 
            }

}

拇指功能改变。

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();
}