Home >Backend Development >PHP Tutorial >Introduction to the function of CI framework to recursively generate file paths and regenerate images

Introduction to the function of CI framework to recursively generate file paths and regenerate images

不言
不言Original
2018-06-29 10:18:282273browse

This article mainly introduces the CI framework to implement the function of recursively generating file paths and regenerating images, involving the CodeIgniter framework custom image controller class to implement file directory recursion and calling the image processing extension class to perform image generation related operation skills, which are required Friends can refer to

The example in this article describes the CI framework's ability to recursively generate file paths and regenerate images. Share it with everyone for your reference, the details are as follows:

<?php if ( ! defined(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;);
set_time_limit(0);
class Img_build extends CI_Controller{
  private static $img_path =  &#39;upload_old/&#39;;
  private static $new_path =  &#39;upload/&#39;;
  function __construct()
  {
      parent::__construct();
  }
  /**
   * 获取需要读取的路径的信息
   * $map = array (
   *         &#39;路径名&#39; => array (文件1, 文件2, 文件3)
   *     )
   */
  public function index()
  {
    $this->load->helper(&#39;directory&#39;);
    //读取路径的信息
    $map = directory_map(self::$img_path, FALSE, TRUE);
    echo "<pre class="brush:php;toolbar:false">";
    print_r($map);
    echo "
"; 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."
"; $this->load->helper('directory'); $c_map = directory_map($old_tmp_path, FALSE, TRUE); // echo "
";
//           print_r($c_map);
//           echo "
"; 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."
"; $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(); //清除图片处理参数 } } ?>

The above is the entire content of this article, I hope it will be helpful to everyone’s study , please pay attention to the PHP Chinese website for more related content!

Related recommendations:

How to use the CI framework to optimize file uploads and upload multiple files

About CI Framework Unlimited Implementation of level classification and recursion

The above is the detailed content of Introduction to the function of CI framework to recursively generate file paths and regenerate images. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn