The CI framework implements the function of recursively generating file paths and regenerating pictures

  • 2021-10-24 19:01:47
  • OfStack

This article describes the example of CI framework to achieve recursive generation of file path and image regeneration function. Share it for your reference, as follows:


<?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();
  }
  /**
   *  Get information about the path to read 
   * $map = array (
   *         ' Path name ' => array ( Documents 1,  Documents 2,  Documents 3)
   *     )
   */
  public function index()
  {
    $this->load->helper('directory');
    // Read the information of the path 
    $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);
    }
  }
  /**
   *  Recursively generate the corresponding path 
   * @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;
    }
  }
  /**
   *  Generate a new picture from the original picture 
   * @param array $img
   * $img = array('source_image'=> ' The path of the original picture ', 'new_image' => ' New picture path ')
   */
  private function build_img($img = array())
  {  
    if(!is_array($img) || empty($img))
    {
      return FALSE;
    }
    // Setting Image Generation Parameters 
    $config['image_library']  = 'gd2';  // Setting up Image Gallery 
    $config['source_image']   = $img['source_image']; // Set the name of the original image / Path 
    $config['create_thumb']   = FALSE;  // Let the image processing function generate 1 Preview images 
    $config['maintain_ratio']  = TRUE; // Specifies whether to keep the original aspect scale of the image when scaling or using hard values 
    //$config['quality']     = 200;
    $img_info = array();
    $img_info = getimagesize($config['source_image']);// Get the size of the picture 
    if(is_array($img_info) && !empty($img_info))
    {
      $config['width']      = $img_info[0];
      $config['height']      = $img_info[1];
    }
    $config['new_image']    = $img['new_image']; // New picture path 
    $this->load->library('image_lib', $config); // Load a picture processing class 
    $this->image_lib->initialize($config); // Call 
    if ( ! $this->image_lib->resize())
    {
      echo $this->image_lib->display_errors();
    }    
    $this->image_lib->clear(); // Clear picture processing parameters 
  }
}
?>

More readers interested in CodeIgniter can check the topics on this site: "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "php Excellent Development Framework Summary", "ThinkPHP Introduction Tutorial", "ThinkPHP Common Methods Summary", "Zend FrameWork Framework Introduction Tutorial", "php Object-Oriented Programming Introduction Tutorial", "php+mysql Database Operation Introduction Tutorial" and "php Common Database Operation Skills Summary"

I hope this article is helpful to the PHP programming based on CodeIgniter framework.


Related articles: