本文實(shí)例講述了CI框架文件上傳類及圖像處理類用法。分享給大家供大家參考,具體如下:
//列表頁(yè)banner圖片
public function edit_list_page_banner($category_id=""){
$category_id= empty($category_id)?$_POST["category_id"]:$category_id;
//上傳圖片
if(isset($_POST["key"]) && $_POST["key"] == "upload"){
/*
1.set_upload_path
*/
$config['upload_path']="./upload/source/".date("Y/m/d");//文件上傳目錄
if(!file_exists("./upload/source/".date("Y/m/d"))){
mkdir("./upload/source/".date("Y/m/d"),0777,true);//原圖路徑
}
if(!file_exists("./upload/big_thumb/".date("Y/m/d"))){
mkdir("./upload/big_thumb/".date("Y/m/d"),0777,true);//大縮略圖路徑
}
if(!file_exists("./upload/small_thumb/".date("Y/m/d"))){
mkdir("./upload/small_thumb/".date("Y/m/d"),0777,true);//小縮略圖路徑
}
$config['allowed_types']="gif|jpg|png|txt";//文件類型
$config['max_size']="20000";//最大上傳大小
$this->load->library("upload",$config);
if($this->upload->do_upload('userfile'))//表單中name="userfile"
{
//上傳成功之后,生成兩張縮略圖
$data=$this->upload->data();//返回上傳圖片的信息
$this->load->library("image_lib");//載入圖像處理類庫(kù)
//第一種方式:大縮略圖的配置參數(shù)
/*
$config_big_thumb['image_library'] = 'gd2';//gd2圖庫(kù)
$config_big_thumb['source_image'] = $data['full_path'];//原圖
$config_big_thumb['new_image'] = "./upload/big_thumb/".date("Y/m/d")."/".$data['file_name'];//大縮略圖
$config_big_thumb['create_thumb'] = true;//是否創(chuàng)建縮略圖
$config_big_thumb['maintain_ratio'] = true;
$config_big_thumb['width'] = 300;//縮略圖寬度
$config_big_thumb['height'] = 300;//縮略圖的高度
$config_big_thumb['thumb_marker']="_300_300";//縮略圖名字后加上 "_300_300",可以代表是一個(gè)300*300的縮略圖
*/
//第二種:大縮略圖的配置參數(shù)
/*
$config_big_thumb=array(
'image_library' => 'gd2',//gd2圖庫(kù)
'source_image' => $data['full_path'],//原圖
'new_image' => "./upload/big_thumb/".date("Y/m/d")."/".$data['file_name'],//大縮略圖
'create_thumb' => true,//是否創(chuàng)建縮略圖
'maintain_ratio' => true,
'width' => 300,//縮略圖寬度
'height' => 300,//縮略圖的高度
'thumb_marker'=>"_300_300"http://縮略圖名字后加上 "_300_300",可以代表是一個(gè)300*300的縮略圖
);
*/
//第三種方式:將部分配置信息放到了config.php文件中
$config_big_thumb=$this->config->item("config_big_thumb");
$config_big_thumb['source_image']=$data['full_path'];
$config_big_thumb['new_image']="./upload/big_thumb/".date("Y/m/d")."/".$data['file_name'];
//小縮略圖的配置參數(shù)
/*
$config_small_thumb['image_library'] = 'gd2';//gd2圖庫(kù)
$config_small_thumb['source_image'] = $data['full_path'];//原圖
$config_small_thumb['new_image'] = "./upload/small_thumb/".date("Y/m/d")."/".$data['file_name'];//大縮略圖
$config_small_thumb['create_thumb'] = true;//是否創(chuàng)建縮略圖
$config_small_thumb['maintain_ratio'] = true;
$config_small_thumb['width'] = 100;//縮略圖寬度
$config_small_thumb['height'] = 100;//縮略圖的高度
$config_small_thumb['thumb_marker']="_100_100";//縮略圖名字后加上 "_100_100",可以代表是一個(gè)100*100的縮略圖
*/
//小縮略圖的配置參數(shù)
$config_small_thumb=array(
'image_library' => 'gd2',//gd2圖庫(kù)
'source_image' => $data['full_path'],//原圖
'new_image' => "./upload/small_thumb/".date("Y/m/d")."/".$data['file_name'],//大縮略圖
'create_thumb' => true,//是否創(chuàng)建縮略圖
'maintain_ratio' => true,
'width' => 100,//縮略圖寬度
'height' => 100,//縮略圖的高度
'thumb_marker'=>"_100_100"http://縮略圖名字后加上 "_300_300",可以代表是一個(gè)300*300的縮略圖
);
//$this->load->library("image_lib",$config_thumb);
$this->image_lib->initialize($config_big_thumb);
$this->image_lib->resize();//生成big縮略圖
$this->image_lib->initialize($config_small_thumb);
$this->image_lib->resize();//生成small縮略圖
//插入數(shù)據(jù)庫(kù)
$data_array = array(
'category_id' => $category_id,
'pic_url' => "./upload/source/".date("Y/m/d")."/".$data['file_name'],
'addtime' => time(),
'is_stop' => 1,
'sort'=>0,
'gender' => $_POST["gender"],
'link_url'=>$_POST["link_url"],
'user_id' => intval($this->cur_user ['user_id'])
);
$this->category_model->add_category_banner($data_array);
}
}
$con_arr[] = " category_id= '{$category_id}'";
if ($gender=='' ) {
$gender=0;
}
$con_arr[] = " gender= '{$gender}'";
$condition = implode( ' and ', $con_arr);
$banner_list = $this->category_model->get_banner_all($condition);
$this->tp->assign('banner_list', $banner_list);
$this->tp->assign('base_url', base_url());
$this->tp->assign('gender', $gender);
$this->tp->assign('category_id', $category_id);
$this->tp->display("category/edit_list_page_banner.php");
}
config.php文件中有關(guān)縮略圖的配置項(xiàng):
//大縮略圖的配置參數(shù)
$config_big_thumb=array(
'image_library' => 'gd2',//gd2圖庫(kù)
'create_thumb' => true,//是否創(chuàng)建縮略圖
'maintain_ratio' => true,
'width' => 300,//縮略圖寬度
'height' => 300,//縮略圖的高度
'thumb_marker'=>"_300_300"http://縮略圖名字后加上 "_300_300",可以代表是一個(gè)300*300的縮略圖
);
希望本文所述對(duì)大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。