CodeIgniter Forums
Image lib problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Image lib problem (/thread-5985.html)



Image lib problem - El Forum - 02-11-2008

[eluser]emperius[/eluser]
I have few fields for uploading files.
And when I'm uploading 1 file resize works great, but when I'm uploading few files images are not resized or I get a black square

here is my code

Code:
if(!empty($_FILES))
{
    foreach($_FILES as $key => $value)
    {
        if(!empty($value['name']))
        {
            $field = $key;
            $config['upload_path'] = realpath('public/object');
            $config['allowed_types'] = 'jpg|jpeg|gif|avi';
            $config['encrypt_name'] = 'TRUE';
            $this->load->library('upload', $config);
            
            $this->upload->do_upload($field);
            $resarr = $this->upload->data();
            $file = $resarr['file_name'];
            
            if($file != '')
            {
                $oldpath = realpath("public/object")."/".$file;
                $fpath = realpath("public/object/thumbs")."/".$file;
                if (!copy($oldpath, $fpath))
                {
                    echo "error copy";
                }
                else
                {
                    $config['image_library'] = 'GD2';
                    $config['source_image'] = $fpath;
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 150;
                    $config['height'] = 110;
                
                    $this->load->library('image_lib', $config);
                    
                    $this->image_lib->resize();
                    
                    echo $this->image_lib->display_errors();
                }
            }
            echo $this->upload->display_errors();
        }
    }            
}



Image lib problem - El Forum - 02-11-2008

[eluser]xwero[/eluser]
Try this. Loading of a library must happen outside a loop.

Code:
if(!empty($_FILES))
{
    $this->load->library('upload');
    $this->load->library('image_lib');
    foreach($_FILES as $key => $value)
    {
        if(!empty($value['name']))
        {
            $field = $key;
            $config['upload_path'] = realpath('public/object');
            $config['allowed_types'] = 'jpg|jpeg|gif|avi';
            $config['encrypt_name'] = 'TRUE';
            $this->upload->initialize($config);
            
            $this->upload->do_upload($field);
            $resarr = $this->upload->data();
            $file = $resarr['file_name'];
            
            if($file != '')
            {
                $oldpath = realpath("public/object")."/".$file;
                $fpath = realpath("public/object/thumbs")."/".$file;
                if (!copy($oldpath, $fpath))
                {
                    echo "error copy";
                }
                else
                {
                    $config['image_library'] = 'GD2';
                    $config['source_image'] = $fpath;
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 150;
                    $config['height'] = 110;
                
                    $this->image_lib->initialize($config);
                    
                    $this->image_lib->resize();
                    
                    echo $this->image_lib->display_errors();
                }
            }
            echo $this->upload->display_errors();
        }
    }            
}