Reputation: 742
I'm trying to upload a image and resize it, and make a smaller copy of it. But it won't do the second resize. But it returns 'TRUE'.
I found more questions about it, but they did not do the trick, suggestions like:
$this->image_lib->clear();
before every resizeI think this is not the problem with my code.
If i'm missing something, please let me know.
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();
}
}
SOLUTION
That was it, damn. The ! at if ( ! $this->upload->do_upload('image'))
should not be there. It works now Thanks guys!
Upvotes: 0
Views: 714
Reputation: 616
i think you should do it like this
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$file_name=$this->upload->file_name;
$this->create_thumb($file_name);
}
Then In your controller make this function 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();
}
}
Any number of thumbs
Upvotes: 1