Welcome Guest, Not a member yet? Register   Sign In
Problem with the image_lib class batch processing
#1

[eluser]Marcellus[/eluser]
I am trying to read the first two images in a directory of multiple images and use the image_lib class to create thumbnails. Only the first file is changed. I think it has something to do with loading the image_lib library twice. I am not sure. Any feedback would be grateful. Here is the code:
Code:
$this->load->helper('file');
        //$this->load->library('image_lib');
        $this->load->helper('url');
        $this->load->helper('array');
        //will attempt to read the file names into an array
        print "Base URL: ". base_url(). "<br />";
        
        $file_array = get_dir_file_info('images/presto_rotate/');
        
        $img_config['create_thumb']=TRUE;
        $img_config['maintain_ratio'] = TRUE;
        $img_config['width']=800;
        $img_config['height']=600;
        $img_config['thumb_marker']="_small";
        
        $i = 0;
        foreach( $file_array as $key => $value )
        {   if( $i < 2 )
            {
                $img_config['source_image'] = $value['server_path'];
                print $img_config['source_image'] ."<br />"; //just prints out the complete image with path for testing
                $this->load->library('image_lib', $img_config);
                if( ! $this->image_lib->resize())
                {
                    echo $this->image_lib->display_errors('<p>', '</p>');
                }
                
            }
            $i++;
        }
#2

[eluser]Marcellus[/eluser]
found it:
Code:
$this->image_lib->clear()

...after each use, before the next loop interation. It was right in front of me. I feel foolish...but it IS 4 in the morning.
#3

[eluser]cmgmyr[/eluser]
I'm having the same problem with this even with the clear function. Any ideas anyone? It's only resizing the first image. There are supposed to be about 30+.

Code:
$dir = $this->config->config['file_url'].$this->config->config['products_dir'];
        $sql = "SELECT styleno FROM products WHERE front = '1' ORDER BY styleno ASC";
        $query = $this->db->query($sql);
        
        if($query->num_rows() > 0){
            foreach($query->result() as $row){
                if(file_exists($dir.$row->styleno.'_f.jpg')){
                    unlink($dir.$row->styleno.'_f.jpg');
                }
                $config['image_library'] = 'gd2';
                $config['source_image'] = $dir.$row->styleno.'.jpg';
                $config['create_thumb'] = TRUE;
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 400;
                $config['height'] = 400;
                $config['thumb_marker'] = '_f';
                
                $this->load->library('image_lib', $config);
                
                if(!$this->image_lib->resize()){
                    echo '<p>'.$row->styleno.' not resized</p>';
                }else{
                    echo '<p>'.$row->styleno.'</p>';
                }
                $this->image_lib->clear();
            }
        }

Thanks,
-Chris
#4

[eluser]Marcellus[/eluser]
Try to set up the majority of your $config hash outside of the foreach. Once in the foreach loop, set whichever setting you need ( $config['source_image'] = $dir.$row.... ), then call

$this->image_lib->initialize($config);

before your resize() call. So it should look something like this:

Code:
$this->load->library('image_lib');
$dir = $this->config->config['file_url'].$this->config->config['products_dir'];
$sql = "SELECT styleno FROM products WHERE front = '1' ORDER BY styleno ASC";
$query = $this->db->query($sql);
        
if($query->num_rows() > 0){
       $config['image_library'] = 'gd2';
              
       $config['create_thumb'] = TRUE;
       $config['maintain_ratio'] = TRUE;
       $config['width'] = 400;
       $config['height'] = 400;
       $config['thumb_marker'] = '_f';

       foreach($query->result() as $row){
           if(file_exists($dir.$row->styleno.'_f.jpg')){
                unlink($dir.$row->styleno.'_f.jpg');
           }  
                $config['source_image'] = $dir.$row->styleno.'.jpg';
                
                
                
                
                $this->image_lib->initialize($config);  //MUST CALL THIS METHOD
                if(!$this->image_lib->resize()){
                    echo '<p>'.$row->styleno.' not resized</p>';
                }else{
                    echo '<p>'.$row->styleno.'</p>';
                }
                $this->image_lib->clear();
            }
        }
#5

[eluser]cmgmyr[/eluser]
Hey, thanks for the post. I actually just did the same type of thing. Here is the code:
Code:
$dir = $this->config->config['file_url'].$this->config->config['products_dir'];
        $sql = "SELECT styleno FROM products WHERE front = '1' ORDER BY styleno ASC";
        $query = $this->db->query($sql);
        $this->load->library('image_lib');
        
        if($query->num_rows() > 0){
            foreach($query->result() as $row){
                if(file_exists($dir.$row->styleno.'_f.jpg')){
                    unlink($dir.$row->styleno.'_f.jpg');
                }
                $config['image_library'] = 'gd2';
                $config['source_image'] = $dir.$row->styleno.'.jpg';
                $config['create_thumb'] = TRUE;
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 400;
                $config['height'] = 400;
                $config['thumb_marker'] = '_f';
                
                $this->image_lib->initialize($config);
                
                if(!$this->image_lib->resize()){
                    echo '<p>'.$row->styleno.' not resized</p>';
                }else{
                    echo '<p>'.$row->styleno.'</p>';
                }
                $this->image_lib->clear();
            }
        }




Theme © iAndrew 2016 - Forum software by © MyBB