I am new to Codeigniter. This is what I want to do:
But what I when I upload the image is that it's re-sized successfully only for one dimension, 400×300.
Here is my code:
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 400;
$config['height'] = 300;
$config['new_image'] = 400x300image;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear()
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 200;
$config['height'] = 200;
$config['new_image'] = 200x200image;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear()
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 32;
$config['height'] = 32;
$config['new_image'] = 32x32image;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();
I tried using different config names like $config2
and $config3
but I did't get the output.
Please help me to solve the issue.
I have a similar function built into one of my CI apps. In my case, I needed the config files in another function, so I built separate versions.
Don't load the library multiple times. Once is fine. Just reinitialize the the library with your new config each time.
$this->load->library('image_lib');
/* First size */
$configSize1['image_library'] = 'gd2';
$configSize1['source_image'] = '/path/to/image/mypic.jpg';
$configSize1['create_thumb'] = TRUE;
$configSize1['maintain_ratio'] = TRUE;
$configSize1['width'] = 400;
$configSize1['height'] = 300;
$configSize1['new_image'] = '400x300image';
$this->image_lib->initialize($configSize1);
$this->image_lib->resize();
$this->image_lib->clear();
/* Second size */
$configSize2['image_library'] = 'gd2';
$configSize2['source_image'] = '/path/to/image/mypic.jpg';
$configSize2['create_thumb'] = TRUE;
$configSize2['maintain_ratio'] = TRUE;
$configSize2['width'] = 200;
$configSize2['height'] = 200;
$configSize2['new_image'] = '200x200image';
$this->image_lib->initialize($configSize2);
$this->image_lib->resize();
$this->image_lib->clear();
And so on. I suppose, for memory's sake, you could unset and reset the config file, as was suggested above, but if you plan on using the image resizing in separate controller methods as I did, I found it helpful to save those separately.
The problem is, CI stores all the libraries after loading them into a static array, and will not load a library again once it is loaded.
So, what you can do is, you can initialize it again with new config:
$this->image_lib->clear();
// make changes to the $conig here
$this->image_lib->initialize($config);
That should work.
Source.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With