dqh1984 2015-05-18 10:27
浏览 50

Codeigniter创建缩略图不起作用?

I have 2 codeigniter functions for image upload and thumb creation.The Image upload function is working properly but creating thumb function is not working. I don't Know What is the Mistake in my function?

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

}

create thumb function

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条回答 默认 最新

  • drix47193 2015-05-19 06:49
    关注

    little bit modification in your code.

    Upload Function changes.

    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 changes.

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

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛