CI框架封裝的常用圖像處理方法(縮略圖,水印,旋轉(zhuǎn),上傳等)
本文實(shí)例講述了CI框架封裝的常用圖像處理方法。分享給大家供大家參考,具體如下:
其實(shí)微信手機(jī)端上圖時(shí),列表圖最好是縮略圖,節(jié)省流量,這不,又被移動(dòng)坑了一把,話費(fèi)簽一分就停機(jī),流量欠到90塊才停機(jī),我也是醉了。。。
不說(shuō)廢話了,下面是用CI 的內(nèi)置處理圖像的庫(kù)寫的,小弟不才,遺漏之處敬請(qǐng)指出,謝謝。
/**
* 生成縮略圖
* @param $path 原圖的本地路徑
* @return null 創(chuàng)建一個(gè) 原圖_thumb.擴(kuò)展名 的文件
*
*/
public function dealthumb($path){
$config['image_library'] = 'gd2';
$config['source_image'] = $path;
$config['create_thumb'] = TRUE;
//生成的縮略圖將在保持縱橫比例 在寬度和高度上接近所設(shè)定的width和height
$config['maintain_ratio'] = TRUE;
$config['width'] = 80;
$config['height'] = 80;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();
}
/*
* 處理圖像旋轉(zhuǎn)
*/
public function transroate($path,$imgpath){
$this->load->library('image_lib');
//(必須)設(shè)置圖像庫(kù)
$config['image_library'] = 'gd2';
$newname = time().'_rote.jpg';
//設(shè)置圖像的目標(biāo)名/路徑
$config['new_image'] =$imgpath.$newname;
//(必須)設(shè)置原始圖像的名字/路徑
$config['source_image'] = $path;
//決定新圖像的生成是要寫入硬盤還是動(dòng)態(tài)的存在
$config['dynamic_output'] = FALSE;
//設(shè)置圖像的品質(zhì)。品質(zhì)越高,圖像文件越大
$config['quality'] = '90%';
//有5個(gè)旋轉(zhuǎn)選項(xiàng) 逆時(shí)針90 180 270 度 vrt 豎向翻轉(zhuǎn) hor 橫向翻轉(zhuǎn)
$config['rotation_angle'] = 'vrt';
$this->image_lib->initialize($config);
if(@$this->image_lib->rotate()){
$this->image_lib->clear();
return $config['new_image'];
}else{
$this->image_lib->clear();
return '';
}
}
/**
* 處理圖像水印
*/
public function overlay($path,$imgpath){
$this->load->library('image_lib');
$newname = time().'_over.jpg';
//設(shè)置新圖像名稱
$config['new_image'] =$imgpath.$newname;
//調(diào)用php gd庫(kù) 繪圖
$config['image_library'] = 'gd2';
//源圖像 本地地址
$config['source_image'] = $path;
//覆蓋文字
$config['wm_text'] = 'Copyright 2015 - Friker';
//覆蓋類型 文字/圖像
$config['wm_type'] = 'text';
//文字字體類型
//$config['wm_font_path'] = 'C:\Windows\Fonts\vrinda.ttf';
//字體大小
$config['wm_font_size'] = '16';
//字體顏色
$config['wm_font_color'] = 'ff0000';
//垂直方向距離頂端距離
$config['wm_vrt_alignment'] = '20';
//水平方向距離左端距離
$config['wm_hor_alignment'] = 'center';
//padding
$config['wm_padding'] = '20';
$this->image_lib->initialize($config);
if($this->image_lib->watermark()){
$this->image_lib->clear();
return $config['new_image'];
}else{
$this->image_lib->clear();
return '';
}
}
/**
* 處理圖片上傳
* 文件上傳類 通過(guò)前臺(tái) 上傳文件
*/
public function uploadfile(){
//文件上傳部分
// 處理文件
// $data = '';
$this->load->helper('url');
$formpic = key($_FILES);
//文件處理部分
if(false === empty($_FILES[$formpic]['tmp_name'])){
//設(shè)置文件上傳的路徑
$upload['upload_path'] = "./public/img/";
//限制文件上傳的類型
$upload['allowed_types'] = 'jpeg|jpg|gif|png';
//限制文件上傳的大小
$upload['max_size'] = 2048;
//設(shè)置文件上傳的路徑
$upload['file_name'] = date('YmdHis', time()).rand(10000, 99999);
//加載文件上傳配置信息
$this->load->library('upload', $upload);
//處理文件上傳
$this->upload->do_upload($formpic);
//返回文件上傳信息
$image = $this->upload->data();
/*
'file_name' => string '2015071702051718388.jpg' (length=23)
'file_type' => string 'image/jpeg' (length=10)
'file_path' => string 'E:/wamp/www/testci/public/img/' (length=30)
'full_path' => string 'E:/wamp/www/testci/public/img/2015071702051718388.jpg' (length=53)
'raw_name' => string '2015071702051718388' (length=19)
'orig_name' => string '2015071702051718388.jpg' (length=23)
'client_name' => string 'u=415761610,1548338330&fm=116&gp=0.jpg' (length=38)
'file_ext' => string '.jpg' (length=4)
'file_size' => float 3.74
'is_image' => boolean true
'image_width' => int 146
'image_height' => int 220
'image_type' => string 'jpeg' (length=4)
'image_size_str' => string 'width="146" height="220"' (length=24)
*/
//var_dump($image);
//返回文件上傳名字
$data = $image['file_name'];
$this->dealthumb($image['full_path']);
$this->overlay($image['full_path'],$image['file_path']);
$this->transroate($image['full_path'],$image['file_path']);//
$thumbdata = '';
//生成縮略圖名稱
$pos = strripos($image['file_name'], ".");
$newname = substr($image['file_name'], 0,$pos)."_thumb".substr($image['file_name'], $pos);
if(file_exists($image['file_path'].$newname)){
$thumbdata = $newname;
}
}
//$dirroot = $_SERVER['DOCUMENT_ROOT'];
//$this->dealthumb($dirroot."/public/img/".$data);
//上傳失敗
if(!$data){
echo json_encode(array('status'=>0,'msg'=>"上傳失??!"));
}else{
//上傳成功
echo json_encode(array(
'name'=>$data,
'pic'=>base_url()."public/img/".$data,
'picthumb'=>$thumbdata == '' ?$data:$thumbdata
));
}
}
下面是前端的基本html代碼:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/public/stylesheets/bootstrap.min.css" />
<link rel="stylesheet" href="/public/stylesheets/bootstrap-responsive.min.css" />
<link rel="stylesheet" href="/public/stylesheets/matrix-style.css" />
<link rel="stylesheet" href="/public/stylesheets/matrix-media.css" />
<script type="text/javascript" src="/public/javascripts/jquery.min.js"></script>
<script type="text/javascript" src="/public/javascripts/jquery.form.js"></script>
<script type="text/javascript" src="/public/javascripts/jquery.validate.js"></script>
<style type="text/css">
body{background:#eeeeee; margin:0px;}
</style>
</head>
<body>
<div class="control-group">
<label class="control-label"> 分享logo: </label>
<div class="controls">
<input type="file" name="sharepic" id="sharepic"/>
<input type="hidden" name="act_sharepic" value="" id="act_sharepic"/>(<sapn class="fred">最佳大小為 80 X 80 像素</sapn>)
<p style="margin:20px 0;"><img src="/public/img/default.png" alt="" id="sharepic_img"></p>
</div>
</div>
<script type="text/javascript">
$(function () {
/*****************圖片上傳部分開(kāi)始 *******************/
var act = "<form class='myupload' action='"+"<?php echo site_url('mytest/uploadfile');?>"+"' method='post' enctype='multipart/form-data'></form>";
$("#sharepic").change(function(){
$(this).wrap(act);
$(this).parent(".myupload").ajaxSubmit({
dataType: 'json',
success: function(data) {
var src = data.pic;
//更改預(yù)覽圖像地址
$('#sharepic_img').attr("src",src);
$('#act_sharepic').val(data.name);
$('#sharepic').unwrap();
},
error:function(xhr){
alert(JSON.parse(xhr));
}
});
});
})
</script>
</body>
</html>
更多關(guān)于CodeIgniter相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。
- CI框架實(shí)現(xiàn)優(yōu)化文件上傳及多文件上傳的方法
- CI框架文件上傳類及圖像處理類用法分析
- SWFUpload與CI不能正確上傳識(shí)別文件MIME類型解決方法分享
- php基于CodeIgniter實(shí)現(xiàn)圖片上傳、剪切功能
- codeigniter上傳圖片不能正確識(shí)別圖片類型問(wèn)題解決方法
- 2個(gè)Codeigniter文件批量上傳控制器寫法例子
- Codeigniter實(shí)現(xiàn)多文件上傳并創(chuàng)建多個(gè)縮略圖
- 使用CodeIgniter的類庫(kù)做圖片上傳
- 解決Codeigniter不能上傳rar和zip壓縮包問(wèn)題
- codeigniter教程之多文件上傳使用示例
- CodeIgniter上傳圖片成功的全部過(guò)程分享
- CI(CodeIgniter)框架實(shí)現(xiàn)圖片上傳的方法
相關(guān)文章
PHP中關(guān)于php.ini參數(shù)優(yōu)化詳解
在本篇文章里小編給大家整理的是關(guān)于PHP引擎php.ini參數(shù)優(yōu)化的相關(guān)知識(shí)點(diǎn),有興趣的朋友們可以學(xué)習(xí)下。2020-02-02
基于ThinkPHP5框架使用QueryList爬取并存入mysql數(shù)據(jù)庫(kù)操作示例
這篇文章主要介紹了基于ThinkPHP5框架使用QueryList爬取并存入mysql數(shù)據(jù)庫(kù)操作,結(jié)合實(shí)例形式分析了thinkPHP5框架整合QueryList爬取數(shù)據(jù)存入mysql相關(guān)操作技巧及注意事項(xiàng),需要的朋友可以參考下2019-05-05
ThinkPHP 3.2.2實(shí)現(xiàn)事務(wù)操作的方法
這篇文章主要介紹了ThinkPHP 3.2.2實(shí)現(xiàn)事務(wù)操作的方法,簡(jiǎn)單分析了thinkPHP中事務(wù)的啟動(dòng)、提交、回滾等操作方法并給出了完整的事務(wù)提交與回滾操作實(shí)例,需要的朋友可以參考下2017-05-05
thinkPHP連接sqlite3數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法(附Thinkphp代碼生成器下載)
這篇文章主要介紹了thinkPHP連接sqlite3數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法,涉及thinkPHP的數(shù)據(jù)庫(kù)相關(guān)配置技巧,并附帶了Thinkphp代碼生成器供讀者下載使用,需要的朋友可以參考下2016-05-05
Discuz論壇標(biāo)題和底部去掉版權(quán)信息實(shí)例講解
這篇文章主要介紹了Discuz論壇標(biāo)題和底部去掉版權(quán)信息實(shí)例講解,有正好遇到這個(gè)問(wèn)題但是不知道修改哪個(gè)文件的同學(xué)可以跟著操作下,可以節(jié)省很多不必要的時(shí)間2021-03-03
PHP中如何使用session實(shí)現(xiàn)保存用戶登錄信息
這篇文章主要給大家介紹在php中是如何使用session實(shí)現(xiàn)保存用戶登錄信息的,涉及到php session 用戶登錄等一些知識(shí)點(diǎn),使用session保存用戶登錄信息要比cookie安全很多。感興趣的朋友一起學(xué)習(xí)吧2015-10-10
php如何根據(jù)IP獲取當(dāng)前經(jīng)緯度以及地域信息
這篇文章主要給大家介紹了關(guān)于php如何根據(jù)IP獲取當(dāng)前經(jīng)緯度以及地域信息的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01

