Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Problems using image lib in a loop
#1

[eluser]Mr. Pickle[/eluser]
I'm having problems using the image_lib in a loop.

Description:
I have a folder with several image files.
I loop all the image files within a folder to copy them (with new dimensions) to a different directory.

Therefor I use the image_lib with which you have to set the settings (such as source file, dimensions and destination file) in a config array and pass them to the image lib.

This is my code for that part:
Code:
$config['image_library']= 'gd2';
$config['source_image'] = $dir . $file;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width']     = 1024;
$config['height']     = 768;
$config['new_image']     = $this->config->item('photo_dir') . strtolower($file);
    
$this->load->library('image_lib', $config);

Next thing is that I do the resize action:
Code:
$this->image_lib->resize();

After this I clear the image_lib:
Code:
$this->image_lib->clear();

My problem
The image lib is still using the config from the first loop as I only get the first image (name) with the last image content in my destination folder.
#2

[eluser]Mr. Pickle[/eluser]
Found the solution, I had to (re)initialize the image_lib:

First, just call the image_lib without config outside the loop.
After this within the loop I set the settings config for the loop and have the image_lib first cleared and then re-initialized with the settings for that loop.

So outside the loop:
Code:
$this->load->library('image_lib');

And within the loop:
Code:
// Set all the configs
$config['key'] = 'value';

// Clear to be sure
$this->image_lib->clear();

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

// Resize
$this->image_lib->resize();
#3

[eluser]OttoVanzig[/eluser]
many thanks
great it works now, was just loosing my residual hears on this issue !




Theme © iAndrew 2016 - Forum software by © MyBB