ci框架中文件上传类及图像处理类的应用

作者:管理员 发布于:2016-06-29 浏览量:858

  1. //列表页banner图片  
  2.     public function edit_list_page_banner($category_id=""){  
  3.         $category_idempty($category_id)?$_POST["category_id"]:$category_id;  
  4.   
  5.         //上传图片  
  6.         if(isset($_POST["key"]) && $_POST["key"] == "upload"){  
  7.   
  8.             /*  
  9.             1.set_upload_path  
  10.             */  
  11.             $config['upload_path']="./upload/source/".date("Y/m/d");//文件上传目录  
  12.             if(!file_exists("./upload/source/".date("Y/m/d"))){  
  13.                 mkdir("./upload/source/".date("Y/m/d"),0777,true);//原图路径  
  14.             }  
  15.             if(!file_exists("./upload/big_thumb/".date("Y/m/d"))){  
  16.                 mkdir("./upload/big_thumb/".date("Y/m/d"),0777,true);//大缩略图路径  
  17.             }  
  18.             if(!file_exists("./upload/small_thumb/".date("Y/m/d"))){  
  19.                 mkdir("./upload/small_thumb/".date("Y/m/d"),0777,true);//小缩略图路径  
  20.             }  
  21.             $config['allowed_types']="gif|jpg|png|txt";//文件类型  
  22.             $config['max_size']="20000";//最大上传大小  
  23.             $this->load->library("upload",$config);  
  24.             if($this->upload->do_upload('userfile'))//表单中name="userfile"  
  25.             {  
  26.                 //上传成功之后,生成两张缩略图  
  27.                 $data=$this->upload->data();//返回上传图片的信息  
  28.   
  29.                 $this->load->library("image_lib");//载入图像处理类库  
  30.   
  31.                 //第一种方式:大缩略图的配置参数  
  32.                 /*  
  33.                 $config_big_thumb['image_library'] = 'gd2';//gd2图库  
  34.                 $config_big_thumb['source_image'] = $data['full_path'];//原图  
  35.                 $config_big_thumb['new_image'] = "./upload/big_thumb/".date("Y/m/d")."/".$data['file_name'];//大缩略图  
  36.                 $config_big_thumb['create_thumb'] = true;//是否创建缩略图  
  37.                 $config_big_thumb['maintain_ratio'] = true;  
  38.                 $config_big_thumb['width'] = 300;//缩略图宽度  
  39.                 $config_big_thumb['height'] = 300;//缩略图的高度  
  40.                 $config_big_thumb['thumb_marker']="_300_300";//缩略图名字后加上 "_300_300",可以代表是一个300*300的缩略图  
  41.                 */  
  42.   
  43.                 //第二种:大缩略图的配置参数  
  44.                 /*  
  45.                 $config_big_thumb=array(  
  46.                     'image_library' => 'gd2',//gd2图库  
  47.                     'source_image' => $data['full_path'],//原图  
  48.                     'new_image' => "./upload/big_thumb/".date("Y/m/d")."/".$data['file_name'],//大缩略图  
  49.                     'create_thumb' => true,//是否创建缩略图  
  50.                     'maintain_ratio' => true,  
  51.                     'width' => 300,//缩略图宽度  
  52.                     'height' => 300,//缩略图的高度  
  53.                     'thumb_marker'=>"_300_300"//缩略图名字后加上 "_300_300",可以代表是一个300*300的缩略图  
  54.                 );  
  55.                 */  
  56.                   
  57.                 //第三种方式:将部分配置信息放到了config.php文件中  
  58.                 $config_big_thumb=$this->config->item("config_big_thumb");  
  59.                 $config_big_thumb['source_image']=$data['full_path'];  
  60.                 $config_big_thumb['new_image']="./upload/big_thumb/".date("Y/m/d")."/".$data['file_name'];  
  61.   
  62.   
  63.                 //小缩略图的配置参数  
  64.                 /*  
  65.                 $config_small_thumb['image_library'] = 'gd2';//gd2图库  
  66.                 $config_small_thumb['source_image'] = $data['full_path'];//原图  
  67.                 $config_small_thumb['new_image'] = "./upload/small_thumb/".date("Y/m/d")."/".$data['file_name'];//大缩略图  
  68.                 $config_small_thumb['create_thumb'] = true;//是否创建缩略图  
  69.                 $config_small_thumb['maintain_ratio'] = true;  
  70.                 $config_small_thumb['width'] = 100;//缩略图宽度  
  71.                 $config_small_thumb['height'] = 100;//缩略图的高度  
  72.                 $config_small_thumb['thumb_marker']="_100_100";//缩略图名字后加上 "_100_100",可以代表是一个100*100的缩略图  
  73.                 */  
  74.   
  75.   
  76.                 //小缩略图的配置参数  
  77.                 $config_small_thumb=array(  
  78.                     'image_library' => 'gd2',//gd2图库  
  79.                     'source_image' => $data['full_path'],//原图  
  80.                     'new_image' => "./upload/small_thumb/".date("Y/m/d")."/".$data['file_name'],//大缩略图  
  81.                     'create_thumb' => true,//是否创建缩略图  
  82.                     'maintain_ratio' => true,  
  83.                     'width' => 100,//缩略图宽度  
  84.                     'height' => 100,//缩略图的高度  
  85.                     'thumb_marker'=>"_100_100"//缩略图名字后加上 "_300_300",可以代表是一个300*300的缩略图  
  86.                 );  
  87.   
  88.                 //$this->load->library("image_lib",$config_thumb);  
  89.   
  90.                 $this->image_lib->initialize($config_big_thumb);  
  91.                 $this->image_lib->resize();//生成big缩略图  
  92.                 $this->image_lib->initialize($config_small_thumb);  
  93.                 $this->image_lib->resize();//生成small缩略图  
  94.   
  95.                 //插入数据库  
  96.                 $data_array = array(  
  97.                     'category_id' => $category_id,  
  98.                     'pic_url' => "./upload/source/".date("Y/m/d")."/".$data['file_name'],  
  99.                     'addtime' => time(),  
  100.                     'is_stop' => 1,  
  101.                     'sort'=>0,  
  102.                     'gender' => $_POST["gender"],  
  103.                     'link_url'=>$_POST["link_url"],  
  104.                     'user_id' => intval($this->cur_user ['user_id'])  
  105.                 );  
  106.                 $this->category_model->add_category_banner($data_array);  
  107.   
  108.   
  109.             }  
  110.   
  111.         }  
  112.           
  113.   
  114.   
  115.         $con_arr[] = " category_id'{$category_id}'";  
  116.         if ($gender=='' ) {  
  117.             $gender=0;  
  118.         }  
  119.         $con_arr[] = " gender'{$gender}'";  
  120.         $condition = implode( ' and ', $con_arr);  
  121.         $banner_list = $this->category_model->get_banner_all($condition);  
  122.         $this->tp->assign('banner_list', $banner_list);  
  123.         $this->tp->assign('base_url', base_url());  
  124.         $this->tp->assign('gender', $gender);  
  125.         $this->tp->assign('category_id', $category_id);  
  126.         $this->tp->display("category/edit_list_page_banner.php");  
  127.     }  


config.php文件中有关缩略图的配置项:

  1. //大缩略图的配置参数  
  2. $config_big_thumb=array(  
  3.     'image_library' => 'gd2',//gd2图库  
  4.     'create_thumb' => true,//是否创建缩略图  
  5.     'maintain_ratio' => true,  
  6.     'width' => 300,//缩略图宽度  
  7.     'height' => 300,//缩略图的高度  
  8.     'thumb_marker'=>"_300_300"//缩略图名字后加上 "_300_300",可以代表是一个300*300的缩略图  
  9. ); 

转载自:CSDN

如需评论,请 登录注册

相关文章

文章评论

zhoujielun,2008-5-4 10:33:22
你发如雪凄美了离别,我焚香感动了谁,邀明月,让回忆皎洁,爱在月光下完美,你发如雪,纷飞了眼泪,我等待苍老了谁
zhoujielun,2008-5-4 10:33:22
你发如雪凄美了离别,我焚香感动了谁,邀明月,让回忆皎洁,爱在月光下完美,你发如雪,纷飞了眼泪,我等待苍老了谁
zhoujielun,2008-5-4 10:33:22
你发如雪凄美了离别,我焚香感动了谁,邀明月,让回忆皎洁,爱在月光下完美,你发如雪,纷飞了眼泪,我等待苍老了谁
zhoujielun,2008-5-4 10:33:22
你发如雪凄美了离别,我焚香感动了谁,邀明月,让回忆皎洁,爱在月光下完美,你发如雪,纷飞了眼泪,我等待苍老了谁
zhoujielun,2008-5-4 10:33:22
你发如雪凄美了离别,我焚香感动了谁,邀明月,让回忆皎洁,爱在月光下完美,你发如雪,纷飞了眼泪,我等待苍老了谁

发表评论

评论内容:
验 证 码:    
用 户 名: 密 码: