温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • 使用CI框架怎么实现递归生成文件路径并重新生成图片功能

使用CI框架怎么实现递归生成文件路径并重新生成图片功能

发布时间:2021-05-22 16:32:34 来源:亿速云 阅读:165 作者:Leah 栏目:开发技术

本篇文章为大家展示了使用CI框架怎么实现递归生成文件路径并重新生成图片功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

具体如下:

<?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);
    }
  }
  /**
   * 递归生成相应的路径
   * @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;
    }
  }
  /**
   * 根据原图片生成新的图片
   * @param array $img
   * $img = array('source_image'=> '原图片的路径', 'new_image' => '新图片路径')
   */
  private function build_img($img = array())
  {  
    if(!is_array($img) || empty($img))
    {
      return FALSE;
    }
    //设置图像生成参数
    $config['image_library']  = 'gd2';  //设置图像库
    $config['source_image']   = $img['source_image']; //设置原始图像的名字/路径
    $config['create_thumb']   = FALSE;  //让图像处理函数产生一个预览图像
    $config['maintain_ratio']  = TRUE; //指定是否在缩放或使用硬值的时候使图像保持原始的纵横比例
    //$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); //调用
    if ( ! $this->image_lib->resize())
    {
      echo $this->image_lib->display_errors();
    }    
    $this->image_lib->clear(); //清除图片处理参数
  }
}
?>

上述内容就是使用CI框架怎么实现递归生成文件路径并重新生成图片功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI