CI框架實(shí)現(xià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ù)庫操作技巧匯總》
希望本文所述對(duì)大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。
- CI(CodeIgniter)框架實(shí)現(xiàn)圖片上傳的方法
- codeigniter上傳圖片不能正確識(shí)別圖片類型問題解決方法
- Codeigniter實(shí)現(xiàn)智能裁剪圖片的方法
- 使用CodeIgniter的類庫做圖片上傳
- CodeIgniter實(shí)現(xiàn)從網(wǎng)站抓取圖片并自動(dòng)下載到文件夾里的方法
- php基于CodeIgniter實(shí)現(xiàn)圖片上傳、剪切功能
- CI框架無限級(jí)分類+遞歸的實(shí)現(xiàn)代碼
- CI框架文件上傳類及圖像處理類用法分析
- CI框架實(shí)現(xiàn)優(yōu)化文件上傳及多文件上傳的方法
相關(guān)文章
php實(shí)例分享之mysql數(shù)據(jù)備份
本代碼實(shí)現(xiàn)了表結(jié)構(gòu)和數(shù)據(jù)完全分開,默認(rèn)有一個(gè)文件會(huì)記錄所有表的結(jié)構(gòu),然后表中數(shù)據(jù)的備份 如果超過分卷的大小則會(huì)分成多個(gè)文件,不然則一個(gè)文件2014-05-05
Laravel認(rèn)證原理以及完全自定義認(rèn)證詳解
最近在學(xué)習(xí)laravel框架,所以下面這篇文章主要給大家介紹了關(guān)于Laravel認(rèn)證原理以及完全自定義認(rèn)證的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
PHPStudy hosts文件可能不存在或被阻止打開及同步hosts失敗問題
這篇文章主要介紹了PHPStudy hosts文件可能不存在或被阻止打開,同步hosts失敗,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
使用ThinkPHP的自動(dòng)完成實(shí)現(xiàn)無限級(jí)分類實(shí)例詳解
這篇文章主要介紹了使用ThinkPHP的自動(dòng)完成實(shí)現(xiàn)無限級(jí)分類功能,本文通過圖文并茂的方式給大家展示,介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09

