I’m having trouble trying to resize an image and then create a thumb.
Both functions are working separately but when trying to call them both, only the first call works. No Thumb is created, why ?
Here is my controller:
if($this->upload->do_upload()){
// Si oui, tout va bien
//Update DB
$pictureData = $this->upload->data();
if($pictureData['file_name']!=''){
//On resize l'immage
//On charge la librarie
$this->load->library('thumbs');
$this->thumbs->resize($pictureData['full_path'], 300, 600);
$News['image']=$pictureData['file_name'];
//On crée un Thumbnail:
// on envoi le full path
if($this->thumbs->create($pictureData['full_path']))
// on stock le path recu
$News['thumb'] = $pictureData['raw_name'].'_thumb'.$pictureData['file_ext'];
}
}
And here is the library I’ve created for both functions:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Thumbs
{
function __construct()
{
$CI =& get_instance();
$this->load->library('image_lib');
}
public function create($picPath) {
$CI =& get_instance();
$config['image_library'] = 'gd2';
$config['source_image'] = $picPath;
$config['maintain_ratio'] = TRUE;
$config['create_thumb'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$CI->image_lib->clear();
$CI->image_lib->initialize($config);
if(!$CI->image_lib->resize()){
$CI->session->set_flashdata('flashError', $CI->image_lib->display_errors());
return False;
}
$CI->image_lib->clear();
Return True;
}
public function resize($picPath, $x, $y) {
$CI =& get_instance();
$config1['image_library'] = 'gd2';
$config1['source_image'] = $picPath;
$config1['maintain_ratio'] = TRUE;
if($x!=0)
$config1['width'] = $x;
if($y!=0)
$config1['height'] = $y;
$CI->image_lib->clear();
$CI->image_lib->initialize($config1);
if(!$CI->image_lib->resize()){
$CI->session->set_flashdata('flashError', $CI->image_lib->display_errors());
return False;
}
$CI->image_lib->clear();
Return True;
}
}
So if I call only one, it works fine, wether it is the create or resize function. Otherwise, if I try to call them both, only the first function that has been called works.
EDIT: Thanks to fccotech, I made it. here is the solution he came with:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Thumbs
{
function __construct()
{
$this->CI =& get_instance();
$this->CI->load->library('image_lib');
}
public function create($picPath, $picName) {
$config =array();
$config['image_library'] = 'gd2';
$config['source_image'] = $picPath;
$config['maintain_ratio'] = TRUE;
$config['create_thumb'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$config['new_image'] = $picName;
// $CI->image_lib->clear();
$this->CI->image_lib->initialize($config);
if(!$this->CI->image_lib->resize()){
$this->CI->session->set_flashdata('flashError', $this->CI->image_lib->display_errors());
return False;
}
$this->CI->image_lib->clear();
Return True;
}
public function resize($picPath, $x, $y) {
$config1 = Array();
$config1['image_library'] = 'gd2';
$config1['source_image'] = $picPath;
$config1['maintain_ratio'] = TRUE;
if($x!=0)
$config1['width'] = $x;
if($y!=0)
$config1['height'] = $y;
// $CI->image_lib->clear();
$this->CI->image_lib->initialize($config1);
if(!$this->CI->image_lib->resize()){
$this->CI->session->set_flashdata('flashError', $this->CI->image_lib->display_errors());
$this->CI->image_lib->clear();
return False;
}
$this->CI->image_lib->clear();
Return True;
}
}
After resize add
So also clear before you do a new $CI->image_lib->initialize($config);