Unique codeigniter image in several sizes

I am new to Codeigniter. This is what I want to do:

  • User uploads profile photo.
  • I resize the original image to several sizes, for example. 400 × 300, 200 × 200 and 32 × 32.

But what I, when I upload the image, is that it successfully scales for only 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 configuration names, such as $config2 and $config3 , but I did not get the output.

Please help me solve the problem.

+4
source share
3 answers

I have a similar feature built into one of my CI applications. In my case, I need configuration files in another function, so I created separate versions.

Do not load the library several times. Once all is well. Just update the library with the new configuration every 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 believe that for the sake of memory, you could disable and reset the configuration file, as suggested above, but if you plan to use image resizing in separate controller methods, like me, I found it useful to keep them separate.

+7
source

The problem is that CI saves all libraries after loading them into a static array and does not load the library again after loading it.

So what you can do, you can initialize it again with the new configuration:

 $this->image_lib->clear(); // make changes to the $conig here $this->image_lib->initialize($config); 

That should work.

A source

+3
source

I suggest you use the image_moo library for this ... its dead simple, thin and clean. See this: http://www.matmoo.com/digital-dribble/codeigniter/image_moo/

+2
source

Source: https://habr.com/ru/post/1432404/


All Articles