我在尝试调整图像大小然后创建一个拇指时遇到了麻烦。
这两个功能都是分开工作但是当试图同时调用它们时,只有第一个呼叫有效。没有创建Thumb,为什么?
这是我的控制器:
if($this->upload->do_upload()){
// Si oui, tout va bien
//Update DB
$pictureData = $this->upload->data();
if($pictureData['file_name']!=''){
//On resize l'immage
//On charge la librarie
$this->load->library('thumbs');
$this->thumbs->resize($pictureData['full_path'], 300, 600);
$News['image']=$pictureData['file_name'];
//On crée un Thumbnail:
// on envoi le full path
if($this->thumbs->create($pictureData['full_path']))
// on stock le path recu
$News['thumb'] = $pictureData['raw_name'].'_thumb'.$pictureData['file_ext'];
}
}
这是我为两个函数创建的库:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Thumbs
{
function __construct()
{
$CI =& get_instance();
$this->load->library('image_lib');
}
public function create($picPath) {
$CI =& get_instance();
$config['image_library'] = 'gd2';
$config['source_image'] = $picPath;
$config['maintain_ratio'] = TRUE;
$config['create_thumb'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$CI->image_lib->clear();
$CI->image_lib->initialize($config);
if(!$CI->image_lib->resize()){
$CI->session->set_flashdata('flashError', $CI->image_lib->display_errors());
return False;
}
$CI->image_lib->clear();
Return True;
}
public function resize($picPath, $x, $y) {
$CI =& get_instance();
$config1['image_library'] = 'gd2';
$config1['source_image'] = $picPath;
$config1['maintain_ratio'] = TRUE;
if($x!=0)
$config1['width'] = $x;
if($y!=0)
$config1['height'] = $y;
$CI->image_lib->clear();
$CI->image_lib->initialize($config1);
if(!$CI->image_lib->resize()){
$CI->session->set_flashdata('flashError', $CI->image_lib->display_errors());
return False;
}
$CI->image_lib->clear();
Return True;
}
}
因此,如果我只调用一个,它可以正常工作,那就是创建或调整大小的功能。否则,如果我尝试同时调用它们,则只有第一个被调用的函数可以工作。
编辑:感谢fccotech,我做到了。这是他带来的解决方案:<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Thumbs
{
function __construct()
{
$this->CI =& get_instance();
$this->CI->load->library('image_lib');
}
public function create($picPath, $picName) {
$config =array();
$config['image_library'] = 'gd2';
$config['source_image'] = $picPath;
$config['maintain_ratio'] = TRUE;
$config['create_thumb'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$config['new_image'] = $picName;
// $CI->image_lib->clear();
$this->CI->image_lib->initialize($config);
if(!$this->CI->image_lib->resize()){
$this->CI->session->set_flashdata('flashError', $this->CI->image_lib->display_errors());
return False;
}
$this->CI->image_lib->clear();
Return True;
}
public function resize($picPath, $x, $y) {
$config1 = Array();
$config1['image_library'] = 'gd2';
$config1['source_image'] = $picPath;
$config1['maintain_ratio'] = TRUE;
if($x!=0)
$config1['width'] = $x;
if($y!=0)
$config1['height'] = $y;
// $CI->image_lib->clear();
$this->CI->image_lib->initialize($config1);
if(!$this->CI->image_lib->resize()){
$this->CI->session->set_flashdata('flashError', $this->CI->image_lib->display_errors());
$this->CI->image_lib->clear();
return False;
}
$this->CI->image_lib->clear();
Return True;
}
}
答案 0 :(得分:7)
调整大小后添加
$this->image_lib->clear();
所以在你做一个新的$ CI-&gt; image_lib-&gt; initialize($ config)之前也要清楚;