在Codeigniter中一次调整多个图像的大小

时间:2013-05-22 10:32:45

标签: php codeigniter file-upload

我正在尝试上传图片并调整其大小,并制作一份较小的副本。但它不会做第二次调整大小。但它返回'TRUE'。

我发现了更多关于它的问题,但他们没有做到这一点,建议如下:

  • 在每次调整大小之前添加$this->image_lib->clear();
  • 每次调整大小时都不要加载库
  • 使您的目录可写(777)

我认为这不是我的代码的问题。

如果我遗失了什么,请告诉我。

private function upload_image($id)
{
    $config_1['upload_path'] = './public/img/news/';
    $config_1['allowed_types'] = 'jpg|png';
    $config_1['file_name'] = 'news_item_'.$id.'.jpg';
    $config_1['overwrite'] = TRUE;
    $config_1['max_size']   = '900';
    $config_1['max_width']  = '5000';
    $config_1['max_height']  = '5000';

    $this->load->library('upload', $config_1);

    if ( ! $this->upload->do_upload('image'))
    {
        $image_data     =   $this->upload->data();
        $this->load->library('image_lib');

        $config['image_library'] = 'gd2';
        $config['source_image'] = $image_data['full_path'];
        $config['maintain_ratio'] = TRUE;
        $config['quality']   = 75;
        if($image_data['image_width'] > 2000){
            $config['width']     = 2000;
            $config['height']    = 1500;
        }
        $this->image_lib->initialize($config); 
        if ( ! $this->image_lib->resize()){echo $this->image_lib->display_errors();}
        $this->image_lib->clear();

        //thumb
        $config['image_library'] = 'gd2';
        $config['source_image'] = $image_data['full_path'];
        $config['new_image']    = './public/img/news/thumb/news_item_'.$id.'.jpg';
        $config['maintain_ratio'] = TRUE;
        $config['quality']   = 75;
        $config['width']     = 650;
        $config['height']    = 500;

        $this->image_lib->initialize($config);
        if ( ! $this->image_lib->resize()){echo $this->image_lib->display_errors();}
        $this->image_lib->clear();
    }
}

就是这样,该死的。 if ( ! $this->upload->do_upload('image'))处的不应该在那里。它现在有效了谢谢你们!

1 个答案:

答案 0 :(得分:1)

我认为你应该这样做

if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$file_name=$this->upload->file_name;
$this->create_thumb($file_name);
}

if ( ! $this->upload->do_upload()) { $error = array('error' => $this->upload->display_errors()); } else { $file_name=$this->upload->file_name; $this->create_thumb($file_name); }

然后在你的控制器中创建这个函数create_thum

function create_thumb($file_name)
     {
        $this->load->library('image_lib');
        $config['image_library'] = 'gd2';
        $config['source_image'] = './uploads/restaurants/restaurants/'.$file_name;  
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = FALSE;
        $config['width'] = 78;
        $config['height'] = 78;
        $config['new_image'] = './uploads/restaurants/restaurants/thumbs/'.$file_name;
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        if(!$this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }
        $config['image_library'] = 'gd2';
        $config['source_image'] = './uploads/restaurants/restaurants/'.$file_name;  
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = FALSE;
        $config['width'] = 304;
        $config['height'] = 251;
        $config['new_image'] = './uploads/restaurants/restaurants/thumbs_big/'.$file_name;
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        if(!$this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }

        $config['image_library'] = 'gd2';
        $config['source_image'] = './uploads/restaurants/restaurants/'.$file_name;  
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = FALSE;
        $config['width'] = 37;
        $config['height'] = 37;
        $config['new_image'] = './uploads/restaurants/restaurants/thumbs_small/'.$file_name;
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        if(!$this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }

        $config['image_library'] = 'gd2';
        $config['source_image'] = './uploads/restaurants/restaurants/'.$file_name;  
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = FALSE;
        $config['width'] = 108;
        $config['height'] = 108;
        $config['new_image'] = './uploads/restaurants/restaurants/thumbs_108/'.$file_name;
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        if(!$this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }
     }
任意数量的拇指