最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年06月29日 09:05:56   作者:WhoAmMe  
這篇文章主要介紹了CI框架實(shí)現(xiàn)遞歸生成文件路徑并重新生成圖片功能,涉及CodeIgniter框架自定義圖片控制器類實(shí)現(xiàn)文件目錄遞歸以及調(diào)用圖片處理擴(kuò)展類進(jìn)行圖片生成相關(guān)操作技巧,需要的朋友可以參考下

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

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
set_time_limit(0);
class Img_build extends CI_Controller{
  private static $img_path =  'upload_old/';
  private static $new_path =  'upload/';
  function __construct()
  {
      parent::__construct();
  }
  /**
   * 獲取需要讀取的路徑的信息
   * $map = array (
   *         '路徑名' => array (文件1, 文件2, 文件3)
   *     )
   */
  public function index()
  {
    $this->load->helper('directory');
    //讀取路徑的信息
    $map = directory_map(self::$img_path, FALSE, TRUE);
    echo "<pre>";
    print_r($map);
    echo "</pre>";
    if(!empty($map) && is_array($map))
    {
      $this->build_path($map);
    }
  }
  /**
   * 遞歸生成相應(yīng)的路徑
   * @param array $map
   */
  private function build_path($map = array())
  { 
    if(!file_exists(self::$new_path))
    {
      mkdir(self::$new_path, 0777);
    }
    foreach($map as $key => $val)
    {
      $old_img_path = self::$img_path;
      $old_tmp_path = self::$img_path.$key.'/';
      $new_img_path = self::$new_path;
      $new_tmp_path = self::$new_path.$key.'/';
      if(is_dir($old_tmp_path))
      {
        //echo $new_tmp_path;
        if(!file_exists($new_tmp_path))
        {
          mkdir($new_tmp_path, 0777);
        }
        self::$img_path = $old_tmp_path;
        self::$new_path = $new_tmp_path;
        echo 'path:'.self::$img_path."<hr>";
        $this->load->helper('directory');
        $c_map = directory_map($old_tmp_path, FALSE, TRUE);
//           echo "<pre>";
//           print_r($c_map);
//           echo "</pre>";
        if(!empty($c_map) && is_array($c_map))
        {
          $this->build_path($c_map);
        }
      }
      if(is_file(self::$img_path.$val))
      {
        echo 'file:'.self::$img_path.$val."<hr>";
        $img = array();
        $img['source_image'] = self::$img_path.$val;
        $img['new_image']  = self::$new_path.$val;
        $this->build_img($img);
      }
      self::$img_path = $old_img_path;
      self::$new_path = $new_img_path;
    }
  }
  /**
   * 根據(jù)原圖片生成新的圖片
   * @param array $img
   * $img = array('source_image'=> '原圖片的路徑', 'new_image' => '新圖片路徑')
   */
  private function build_img($img = array())
  {  
    if(!is_array($img) || empty($img))
    {
      return FALSE;
    }
    //設(shè)置圖像生成參數(shù)
    $config['image_library']  = 'gd2';  //設(shè)置圖像庫
    $config['source_image']   = $img['source_image']; //設(shè)置原始圖像的名字/路徑
    $config['create_thumb']   = FALSE;  //讓圖像處理函數(shù)產(chǎn)生一個(gè)預(yù)覽圖像
    $config['maintain_ratio']  = TRUE; //指定是否在縮放或使用硬值的時(shí)候使圖像保持原始的縱橫比例
    //$config['quality']     = 200;
    $img_info = array();
    $img_info = getimagesize($config['source_image']);//獲取圖片的尺寸
    if(is_array($img_info) && !empty($img_info))
    {
      $config['width']      = $img_info[0];
      $config['height']      = $img_info[1];
    }
    $config['new_image']    = $img['new_image']; //新圖片路徑
    $this->load->library('image_lib', $config); //加載圖片處理類
    $this->image_lib->initialize($config); //調(diào)用
    if ( ! $this->image_lib->resize())
    {
      echo $this->image_lib->display_errors();
    }    
    $this->image_lib->clear(); //清除圖片處理參數(shù)
  }
}
?>

更多關(guān)于CodeIgniter相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 使用php-timeit估計(jì)php函數(shù)的執(zhí)行時(shí)間

    使用php-timeit估計(jì)php函數(shù)的執(zhí)行時(shí)間

    當(dāng)我們在使用php性能優(yōu)化的時(shí)候,需要知道某個(gè)函數(shù)的執(zhí)行時(shí)間,在python中,我們有timeit模塊給我們實(shí)現(xiàn),在php有沒有類似的模塊?接下來,小編給大家分享我寫的一個(gè)簡單timeit函數(shù),需要的朋友可以參考下
    2015-09-09
  • php解析url的三個(gè)示例

    php解析url的三個(gè)示例

    這篇文章主要介紹了使用php解析url的三個(gè)示例,大家參考使用吧
    2014-01-01
  • 最新評論

    霍州市| 黔西县| 富裕县| 孝感市| 栾川县| 土默特右旗| 平定县| 资源县| 上虞市| 会昌县| 利津县| 南岸区| 濉溪县| 平阴县| 菏泽市| 怀安县| 合作市| 和田市| 盈江县| 颍上县| 华池县| 米脂县| 绥棱县| 绥阳县| 七台河市| 当雄县| 岱山县| 正蓝旗| 江达县| 射阳县| 广元市| 襄汾县| 安福县| 崇阳县| 永仁县| 互助| 滨海县| 奉节县| 新和县| SHOW| 平阳县|