我正在尝试上传图片并调整其大小,并制作一份较小的副本。但它不会做第二次调整大小。但它返回'TRUE'。
我发现了更多关于它的问题,但他们没有做到这一点,建议如下:
$this->image_lib->clear();
我认为这不是我的代码的问题。
如果我遗失了什么,请告诉我。
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'))
处的!不应该在那里。它现在有效了谢谢你们!
答案 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();
}
}