本文实例讲述了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(); //清除图片处理参数 }}?>
更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。