国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

CI框架實(shí)現(xiàn)遞歸生成文件路徑并重新生成圖片功能

2024-05-04 21:50:47
字體:
供稿:網(wǎng)友

本文實(shí)例講述了CI框架實(shí)現(xiàn)遞歸生成文件路徑并重新生成圖片功能。分享給大家供大家參考,具體如下:

  1. <!--?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
  2. set_time_limit(0); 
  3. class Img_build extends CI_Controller{ 
  4.   private static $img_path =  'upload_old/'
  5.   private static $new_path =  'upload/'
  6.   function __construct() 
  7.   { 
  8.       parent::__construct(); 
  9.   } 
  10.   /** 
  11.    * 獲取需要讀取的路徑的信息 
  12.    * $map = array ( 
  13.    *         '路徑名' =--> array (文件1, 文件2, 文件3) 
  14.    *     ) 
  15.    */ 
  16.   public function index() 
  17.   { 
  18.     $this->load->helper('directory'); 
  19.     //讀取路徑的信息 
  20.     $map = directory_map(self::$img_path, FALSE, TRUE); 
  21.     echo "<pre>"
  22.     print_r($map); 
  23.     echo "</pre>"
  24.     if(!emptyempty($map) && is_array($map)) 
  25.     { 
  26.       $this->build_path($map); 
  27.     } 
  28.   } 
  29.   /** 
  30.    * 遞歸生成相應(yīng)的路徑 
  31.    * @param array $map 
  32.    */ 
  33.   private function build_path($map = array()) 
  34.   {  
  35.     if(!file_exists(self::$new_path)) 
  36.     { 
  37.       mkdir(self::$new_path, 0777); 
  38.     } 
  39.     foreach($map as $key => $val
  40.     { 
  41.       $old_img_path = self::$img_path
  42.       $old_tmp_path = self::$img_path.$key.'/'
  43.       $new_img_path = self::$new_path
  44.       $new_tmp_path = self::$new_path.$key.'/'
  45.       if(is_dir($old_tmp_path)) 
  46.       { 
  47.         //echo $new_tmp_path; 
  48.         if(!file_exists($new_tmp_path)) 
  49.         { 
  50.           mkdir($new_tmp_path, 0777); 
  51.         } 
  52.         self::$img_path = $old_tmp_path
  53.         self::$new_path = $new_tmp_path
  54.         echo 'path:'.self::$img_path."<hr>"
  55.         $this->load->helper('directory'); 
  56.         $c_map = directory_map($old_tmp_path, FALSE, TRUE); 
  57. //           echo "<pre>"; 
  58. //           print_r($c_map); 
  59. //           echo "</pre>"; 
  60.         if(!emptyempty($c_map) && is_array($c_map)) 
  61.         { 
  62.           $this->build_path($c_map); 
  63.         } 
  64.       } 
  65.       if(is_file(self::$img_path.$val)) 
  66.       { 
  67.         echo 'file:'.self::$img_path.$val."<hr>"
  68.         $img = array(); 
  69.         $img['source_image'] = self::$img_path.$val
  70.         $img['new_image']  = self::$new_path.$val
  71.         $this->build_img($img); 
  72.       } 
  73.       self::$img_path = $old_img_path
  74.       self::$new_path = $new_img_path
  75.     } 
  76.   } 
  77.   /** 
  78.    * 根據(jù)原圖片生成新的圖片 
  79.    * @param array $img 
  80.    * $img = array('source_image'=> '原圖片的路徑', 'new_image' => '新圖片路徑') 
  81.    */ 
  82.   private function build_img($img = array()) 
  83.   {   
  84.     if(!is_array($img) || emptyempty($img)) 
  85.     { 
  86.       return FALSE; 
  87.     } 
  88.     //設(shè)置圖像生成參數(shù) 
  89.     $config['image_library']  = 'gd2';  //設(shè)置圖像庫 
  90.     $config['source_image']   = $img['source_image']; //設(shè)置原始圖像的名字/路徑 
  91.     $config['create_thumb']   = FALSE;  //讓圖像處理函數(shù)產(chǎn)生一個(gè)預(yù)覽圖像 
  92.     $config['maintain_ratio']  = TRUE; //指定是否在縮放或使用硬值的時(shí)候使圖像保持原始的縱橫比例 
  93.     //$config['quality']     = 200; 
  94.     $img_info = array(); 
  95.     $img_info = getimagesize($config['source_image']);//獲取圖片的尺寸 
  96.     if(is_array($img_info) && !emptyempty($img_info)) 
  97.     { 
  98.       $config['width']      = $img_info[0]; 
  99.       $config['height']      = $img_info[1]; 
  100.     } 
  101.     $config['new_image']    = $img['new_image']; //新圖片路徑 
  102.     $this->load->library('image_lib'$config); //加載圖片處理類 
  103.     $this->image_lib->initialize($config); //調(diào)用 
  104.     if ( ! $this->image_lib->resize()) 
  105.     { 
  106.       echo $this->image_lib->display_errors(); 
  107.     }//Vevb.com 
  108.     $this->image_lib->clear(); //清除圖片處理參數(shù) 
  109.   } 
  110. ?> 

 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 武穴市| 新丰县| 西林县| 青阳县| 东港市| 华安县| 驻马店市| 大丰市| 拉萨市| 儋州市| 武胜县| 新源县| 昌邑市| 南丰县| 贺兰县| 宜城市| 萝北县| 苍梧县| 三河市| 柘城县| 乐都县| 麻阳| 卢湾区| 衡阳市| 马关县| 奈曼旗| 阿鲁科尔沁旗| 团风县| 昔阳县| 张北县| 古浪县| 互助| 庄浪县| 双柏县| 五大连池市| 玉树县| 左权县| 新源县| 贵南县| 上林县| 江达县|