4

我是 Codeigniter 的新手。这就是我想要做的:

  1. 用户上传个人资料图片。
  2. 我将原始图像调整为多种尺寸,例如 400×300、200×200 和 32×32。

但是我上传图片时,它只成功地调整了一个尺寸,400×300。

这是我的代码:

 $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();

我尝试使用不同的配置名称$config2$config3但我没有得到输出。

请帮我解决问题。

4

3 回答 3

7

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.

于 2012-09-05T05:06:38.203 回答
3

问题是,CI 将所有库加载到静态数组后存储,一旦加载,就不会再次加载库。

所以,你可以做的是,你可以用新的配置再次初始化它:

$this->image_lib->clear();

// make changes to the $conig here

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

那应该行得通。

来源。

于 2012-09-05T04:56:57.213 回答
2

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

于 2012-09-05T09:37:43.297 回答