我想用两个函数创建两个不同大小的缩略图。 上传图片() 在uploadIhumb($ name){它在第一个函数内调用}时创建大缩略图创建小缩略图。 第一个功能创建缩略图但第二个不起作用。 请解决问题并突出显示问题所在?
enter code here:
function uploadImage()
{
$imageName2=$_FILES['file']['tmp_name'];
$config['image_library'] = 'gd2';
//$config['source_image'] = $imageName2;// this is temporary folder where image place on uploading time
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = false;
$config['width'] = 696;
$config['height'] = 241;
$config['new_image'] = 'images/uploads';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$file_name= basename( $imageName2, ".tmp");
$filename=$file_name.'_thumb.tmp';
////////////////////Rename code///////////////////////////
$name= $this->rand_string( 6 );
$dir=$_SERVER['DOCUMENT_ROOT'].'/images/uploads/';
rename($dir . $filename, $dir . $name);
echo $name;
$this->uploadIhumb($name); //FUNCTION CALL
}
function uploadIhumb($name)
{
$config['image_library'] = 'gd2';
$config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/images/uploads/'.$name;
$config['create_thumb'] = TRUE;
$config['width'] = 80;
$config['height'] = 80;
$config['new_image'] = $_SERVER['DOCUMENT_ROOT'].'images/uploads/thumbs/';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
答案 0 :(得分:5)
无需任何其他图书馆:-( 如果我们在函数的开始只加载一次图像库,则可以解决问题。 并且只为第二个拇指初始化配置。成功的代码就像运输工具一样: -
{ function createThumb1($ imageName)//传递文件名 {
// this thumbnail created
$config['image_library'] = 'gd2';
$config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/uploads/'.$imageName;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = false;
$config['width'] = 80;
$config['height'] = 80;
$config['new_image'] = $_SERVER['DOCUMENT_ROOT'].'/images/uploads/thumbs/'.$imageName;
$this->load->library('image_lib', $config);
if ( ! $this->image_lib->resize()){echo $this->image_lib->display_errors();}
$this->image_lib->clear();
// unable to create this this thumbnail
$config['image_library'] = 'gd2';
$config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/uploads/'.$imageName;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = false;
$config['width'] = 696;
$config['height'] = 241;
$config['new_image'] = $_SERVER['DOCUMENT_ROOT'].'/images/uploads/'.$imageName;
//$this->load->library('image_lib', $config); // this line cause problem
$this->image_lib->initialize($config); // with this problem resolved
if ( ! $this->image_lib->resize()){echo $this->image_lib->display_errors();}
$this->image_lib->clear();
$this->load->view('admin/upload_form',array('error' => ' ' ));
}
}
答案 1 :(得分:2)
尝试使用以下方法清除功能顶部的配置:
$this->image_lib->clear();
您还可以使用以下方法检查错误:
if ( ! $this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
希望这有帮助。
答案 2 :(得分:0)
是的,这适用于最后制作 $这 - > image_lib->清除();
和 为第二个thambnail // $ this-> load-> library('image_lib',$ config);
$this->image_lib->initialize($config);
注释掉第1行并替换为第2行。 您的查询将被解决。