我是Codeigniter的新手。这就是我想要做的事情:
但是,当我上传图片时,我的成功重新调整大小仅适用于一个尺寸,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
,但我没有得到输出。
请帮我解决这个问题。
答案 0 :(得分:7)
我在我的一个CI应用程序中内置了类似的功能。在我的情况下,我需要另一个函数中的配置文件,所以我构建了单独的版本。
不要多次加载库。一旦没事。只需每次使用新配置重新初始化库。
$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();
等等。我想,为了记忆,您可以取消设置并重置配置文件,如上所述,但如果您打算像我一样在单独的控制器方法中使用图像大小调整,我发现单独保存它们会很有帮助。
答案 1 :(得分:3)
问题是,CI在将它们加载到静态数组后存储了所有库,并且一旦加载它就不会再加载库。
所以,你可以做的是,你可以用新配置再次初始化它:
$this->image_lib->clear();
// make changes to the $conig here
$this->image_lib->initialize($config);
这应该有效。
答案 2 :(得分:2)
我建议您使用image_moo库....它简单,精简和干净。请参阅:http://www.matmoo.com/digital-dribble/codeigniter/image_moo/