Welcome Guest, Not a member yet? Register   Sign In
Problems copying an uploaded image twice[SOLVED]
#1

[eluser]SaminOz[/eluser]
I wish to upload an image, place an untouched version in one directory and a thumb in another. I wrote something similar to the following:

Both private methods work separately - but not one after the other. Is there some obvious reason for this?

Code:
if ( ! $this->upload->do_upload())
{
$data['error'] = array('error' => $this->upload->display_errors());
}
else
{
$this->load->helper('file');
$data['error'] = $this->file_data = $this->upload->data();
                
$this->create_copy();
                
$this->create_thumb();

delete_file('path/to/file');
}

private function create_copy()
{            
$config['source_image'] = $this->file_data['full_path'];
$config['new_image'] = './full_image/full_image_' .$this->identifier.$this->file_data['file_ext'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
    
private function create_thumb()
{
$config['image_library'] = 'gd2';
$config['source_image'] = $this->file_data['full_path'];
$config['new_image'] = './thumb_image/thumb_image_' .$this->identifier.$this->file_data['file_ext'];
$config['maintain_ratio'] = TRUE;
$config['width'] = 168;
$config['height'] = 101;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
#2

[eluser]Irfan Cikoglu[/eluser]
Use this:
Code:
if ( ! $this->upload->do_upload())
{
$data['error'] = array('error' => $this->upload->display_errors());
}
else
{
$this->load->helper('file');
$data['error'] = $this->file_data = $this->upload->data();
                
$this->create_copy($this->file_data['full_path'], $this->file_data['file_ext']);
                
$this->create_thumb($this->file_data['full_path'], $this->file_data['file_ext']);

delete_file('path/to/file');
}

private function create_copy($fullpath, $ext)
{            
$config['source_image'] = $fullpath;
$config['new_image'] = './full_image/full_image_' .$this->identifier.$ext;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
    
private function create_thumb($fullpath, $ext)
{
$config['image_library'] = 'gd2';
$config['source_image'] = $fullpath;
$config['new_image'] = './thumb_image/thumb_image_' .$this->identifier.$ext;
$config['maintain_ratio'] = TRUE;
$config['width'] = 168;
$config['height'] = 101;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
#3

[eluser]SaminOz[/eluser]
Thanks for your reply Irfan - The issue was solved by not re-loading the library (class) and using $this->image_lib->initialize($config) to pass the new parameters.

FYI - your suggestion of passing the file info as parameters to the methods would not have been effective in the sense that the class properties already held the required info. This may not have been clear from the way I presented the code. I should have shown it something like this:

Code:
class someclass extends CI_controller {

public function show_images_for_this_id ($id = NULL)
{
    //do various things here, then:
    if ( ! $this->upload->do_upload())
    {
        $data['error'] = array('error' => $this->upload->display_errors());
    }
    else
    {
        $this->load->helper('file');
        $data['error'] = $this->file_data = $this->upload->data();
                
        $this->create_copy();
        $this->create_thumb();
        delete_files($this->file_data['file_path']);
       //do some other stuff like load views
    }
}
    private function create_copy()
    {        
        $config['image_library'] = 'gd2';
        $config['source_image'] = $this->file_data['full_path'];
        $config['new_image'] = './full_image/full_image_' .$this->some_obj->obj_id. '_XX'.$this->file_data['file_ext'];
        
        $this->load->library('image_lib');
        $this->image_lib->initialize($config);
        if ( ! $this->image_lib->resize())
        {
            var_dump('1', $this->image_lib->display_errors());
        }
    }
    
    private function create_thumb()
    {
        $this->image_lib->clear();
        $config['image_library'] = 'gd2';
        $config['source_image'] = $this->file_data['full_path'];
        $config['new_image'] = './thumb_image/thumb_image_' .$this->some_obj->obj_id. '_12'.$this->file_data['file_ext'];
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 168;
        $config['height'] = 101;
        $this->image_lib->initialize($config);
        if ( ! $this->image_lib->resize())
        {
            var_dump('2', $this->image_lib->display_errors());
        }
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB