0

我正在尝试上传图像并调整其大小,并制作一个较小的副本。但它不会进行第二次调整大小。但它返回“真”。

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

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

解决方案 就是这样,该死的。 _ atif ( ! $this->upload->do_upload('image'))不应该在那里。它现在可以工作了谢谢大家!

4

1 回答 1

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

然后在你的控制器中创建这个函数 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();
        }
     }

任意数量的拇指

于 2013-05-22T11:50:07.650 回答