您好,登錄后才能下訂單哦!
<?php
/*
* name:婁喜賀
*/
class MyUpload{
var $saveName;// 保存名
var $tmpPath;// 臨時(shí)保存路徑
var $savePath;// 保存路徑
var $fileFormat = array('gif','jpg','png','doc','application/octet-stream');// 文件格式&MIME限定
var $overwrite = 0;// 覆蓋模式
var $maxSize = 0;// 文件最大字節(jié)
var $ext;// 文件擴(kuò)展名
var $thumb = 0;// 是否生成縮略圖
var $thumbWidth = 130;// 縮略圖寬
var $thumbHeight = 130;// 縮略圖高
var $thumbPrefix = "thumb_";// 縮略圖前綴
var $errno;// 錯(cuò)誤代號(hào)
var $returnArray= array();// 所有文件的返回信息s
var $returninfo= array();// 每個(gè)文件返回信息
var $thumbArray = array();//生成多個(gè)縮略圖
var $_radius;// 圓形圖半徑
var $round;// 設(shè)置是否生成圓形圖,1:是、0:否
var $roundArray;// 處理圓形圖多個(gè)尺寸
//構(gòu)造函數(shù)
// @param tmpPath 文件保存路徑
// @param fileFormat 文件格式限制數(shù)組
// @param maxSize 文件最大尺寸
// @param overwriet 是否覆蓋 1 允許覆蓋 0 禁止覆蓋
function MyUpload($params) {
$tmpPath = $params['tmpPath'];
$savePath = $params['savePath'];
$fileFormat = isset($params['fileFormat']) ? $params['fileFormat'] : '';
$maxSize = isset($params['maxSize']) ? $params['maxSize'] : 0;
$overwrite = isset($params['overwrite']) ? $params['overwrite'] : 0;
$this->setSavepath($tmpPath,$savePath);
$this->setFileformat($fileFormat);
$this->setMaxsize($maxSize);
$this->setOverwrite($overwrite);
$this->setThumb($this->thumb, $this->thumbWidth,$this->thumbHeight);
$this->errno = 0;
}
// 上傳
function run($fileInput,$changeName = 1){
if(isset($_FILES[$fileInput])){
$fileArr = $_FILES[$fileInput];
if(is_array($fileArr['name'])){//上傳同文件域名稱(chēng)多個(gè)文件
for($i = 0; $i < count($fileArr['name']); $i++){
$ar['tmp_name'] = $fileArr['tmp_name'][$i];
$ar['name'] = $fileArr['name'][$i];
$ar['type'] = $fileArr['type'][$i];
$ar['size'] = $fileArr['size'][$i];
$ar['error'] = $fileArr['error'][$i];
$this->getExt($ar['name']);//取得擴(kuò)展名,賦給$this->ext,下次循環(huán)會(huì)更新
$this->setSavename($changeName == 1 ? '' : $ar['name']);//設(shè)置保存文件名
if($this->copyfile($ar)){
$this->returnArray[] = $this->returninfo;
}else{
$this->returninfo['error'] = $this->errmsg();
$this->returnArray[] = $this->returninfo;
}
}
return $this->errno ? false : true;
}else{//上傳單個(gè)文件
$this->getExt($fileArr['name']);//取得擴(kuò)展名
$this->setSavename($changeName == 1 ? '' : $fileArr['name']);//設(shè)置保存文件名
if($this->copyfile($fileArr)){
$this->returnArray[] = $this->returninfo;
}else{
$this->returninfo['error'] = $this->errmsg();
$this->returnArray[] = $this->returninfo;
}
return $this->errno ? false : true;
}
return false;
}else{
$this->errno = 10;
return false;
}
}
// 單個(gè)文件上傳
function copyfile($fileArray){
$this->returninfo = array();
// 返回信息
$this->returninfo['name'] = $fileArray['name'];
$this->returninfo['saveName'] = $this->saveName;
$this->returninfo['size'] = number_format( ($fileArray['size'])/1024 , 0, '.', ' ');//以KB為單位
$this->returninfo['type'] = $fileArray['type'];
$this->returninfo['tmpPath'] = $this->tmpPath;
$this->returninfo['savePath'] = $this->savePath;
// 檢查文件格式
if (!$this->validateFormat()){
$this->errno = 11;
return false;
}
// 檢查目錄是否可寫(xiě)
if(!@is_writable($this->tmpPath)){
$this->errno = 12;
return false;
}
// 如果不允許覆蓋,檢查文件是否已經(jīng)存在
if($this->overwrite == 0 && @file_exists($this->tmpPath.$fileArray['name'])){
$this->errno = 13;
return false;
}
// 如果有大小限制,檢查文件是否超過(guò)限制
if ($this->maxSize != 0 ){
if ($fileArray["size"] > $this->maxSize){
$this->errno = 14;
return false;
}
}
// 文件上傳
if(!@copy($fileArray["tmp_name"], $this->tmpPath.$this->saveName)){
$this->errno = $fileArray["error"];
return false;
}else{
if($this->round){
$fielName = $this->_round_it();
$this->_thumb($fielName, $this->roundArray);
}
if($this->thumb){//創(chuàng)建縮略圖
$this->_thumb($this->saveName, $this->thumbArray);
}
}
// 刪除臨時(shí)文件
if(!@$this->del($fileArray["tmp_name"])){
return false;
}
return true;
}
// 生成畫(huà)布對(duì)象
function _createFunction($fileName){
$CreateFunction = "p_w_picpathcreatefrom".($this->ext == 'jpg' ? 'jpeg' : $this->ext);
if (strtolower($CreateFunction) == "p_w_picpathcreatefromgif"
&& !function_exists("p_w_picpathcreatefromgif")) {
$this->errno = 16;
return false;
} elseif (strtolower($CreateFunction) == "p_w_picpathcreatefromjpeg"
&& !function_exists("p_w_picpathcreatefromjpeg")) {
$this->errno = 17;
return false;
} elseif (!function_exists($CreateFunction)) {
$this->errno = 18;
return false;
}else{
return @$CreateFunction($this->tmpPath.$fileName);
}
}
// 保存圖片
function _saveFunction($createdThumb, $prefix){
$SaveFunction = "p_w_picpath".($this->ext == 'jpg' ? 'jpeg' : $this->ext);
if ( !$SaveFunction($createdThumb,$this->tmpPath.$prefix.$this->saveName) ){
$this->errno = 22; return false;
}else{
return $prefix.$this->saveName;
}
}
//生成圓形圖方畫(huà)
private function _get_lt_rounder_corner() {
$radius = $this->_radius;
$img = p_w_picpathcreatetruecolor($radius, $radius);
$bgcolor = p_w_picpathcolorallocate($img, 255, 255, 255);
$fgcolor = p_w_picpathcolorallocate($img, 0, 0, 0);
p_w_picpathfill($img, 0, 0, $bgcolor);
p_w_picpathfilledarc($img, $radius, $radius, $radius*2, $radius*2, 180, 270, $fgcolor, IMG_ARC_PIE);
p_w_picpathcolortransparent($img, $fgcolor);
return $img;
}
/**
* 生成圓形圖
*/
public function _round_it() {
// load the source p_w_picpath
$src_p_w_picpath = $this->_createFunction($this->saveName);
if ($src_p_w_picpath === false) {
die('Sorry, can/t load the p_w_picpath');
}
$p_w_picpath_width = p_w_picpathsx($src_p_w_picpath);
$p_w_picpath_height = p_w_picpathsy($src_p_w_picpath);
if($p_w_picpath_width < $p_w_picpath_height){
$p_w_picpathSize = $p_w_picpath_width;
}else{
$p_w_picpathSize = $p_w_picpath_height;
}
//$p_w_picpathSize = 800;//$p_w_picpath_width;
$this->_radius = (int)$p_w_picpathSize/2;
// create a new p_w_picpath, with src_width, src_height, and fill it with transparent color
$p_w_picpath = p_w_picpathcreatetruecolor($p_w_picpathSize, $p_w_picpathSize);
$trans_color = p_w_picpathcolorallocate($p_w_picpath, 255, 255, 255);
p_w_picpathfill($p_w_picpath, 0, 0, $trans_color);
// then overwirte the source p_w_picpath to the new created p_w_picpath
p_w_picpathcopymerge($p_w_picpath, $src_p_w_picpath, 0, 0, 0, 0, $p_w_picpathSize, $p_w_picpathSize, 100);
// then just copy all the rounded corner p_w_picpaths to the 4 corners
$radius = $this->_radius;
// lt
$lt_corner = $this->_get_lt_rounder_corner();
p_w_picpathcopymerge($p_w_picpath, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100);
// lb
$lb_corner = p_w_picpathrotate($lt_corner, 90, $trans_color);
p_w_picpathcopymerge($p_w_picpath, $lb_corner, 0, $p_w_picpathSize - $radius, 0, 0, $radius, $radius, 100);
// rb
$rb_corner = p_w_picpathrotate($lt_corner, 180, $trans_color);
p_w_picpathcopymerge($p_w_picpath, $rb_corner, $p_w_picpathSize - $radius, $p_w_picpathSize - $radius, 0, 0, $radius, $radius, 100);
// rt
$rt_corner = p_w_picpathrotate($lt_corner, 270, $trans_color);
p_w_picpathcopymerge($p_w_picpath, $rt_corner, $p_w_picpathSize - $radius, 0, 0, 0, $radius, $radius, 100);
// set the transparency
p_w_picpathcolortransparent($p_w_picpath,$trans_color);
return $this->_saveFunction($p_w_picpath, 'round_');
}
// 生成縮略圖
function _thumb($fileName, $thumbArray){
//返回值(縮略圖)
$Original = $this->_createFunction($fileName);
if (!$Original) {$this->errno = 19; return false;}
$originalHeight = ImageSY($Original);
$originalWidth = ImageSX($Original);
if(!empty($thumbArray)){
foreach($thumbArray as $key => $row){
$this->thumbPrefix = $key;
$this->thumbHeight = $row['height'];
$this->thumbWidth = $row['width'];
//$this->returninfo['']['thumbPrefix'] = $this->thumbPrefix;
$this->returninfo[$this->thumbPrefix] = $this->thumbPrefix.$this->saveName;
if (($originalHeight < $this->thumbHeight
&& $originalWidth < $this->thumbWidth)) {
// 如果比期望的縮略圖小,那只Copy
if( $originalHeight > $originalWidth ){// 寬 > 設(shè)定寬度
$thumbHeight = $originalHeight;
$thumbWidth = $this->thumbWidth*($originalHeight / $this->thumbWidth);
}else{//高 > 設(shè)定高度
$thumbWidth = $this->thumbWidth;
$thumbHeight = $this->thumbHeight*($originalHeight / $this->thumbHeight);
}
} else {
if( $originalWidth > $this->thumbWidth ){// 寬 > 設(shè)定寬度
$thumbWidth = $this->thumbWidth ;
$thumbHeight = $this->thumbWidth * ( $originalHeight / $originalWidth );
if($thumbHeight > $this->thumbHeight){//高 > 設(shè)定高度
$thumbWidth = $this->thumbHeight * ( $thumbWidth / $thumbHeight );
$thumbHeight = $this->thumbHeight ;
}
}elseif( $originalHeight > $this->thumbHeight ){//高 > 設(shè)定高度
$thumbHeight = $this->thumbHeight ;
$thumbWidth = $this->thumbHeight * ( $originalWidth / $originalHeight );
if($thumbWidth > $this->thumbWidth){//寬 > 設(shè)定寬度
$thumbHeight = $this->thumbWidth * ( $thumbHeight / $thumbWidth );
$thumbWidth = $this->thumbWidth ;
}
}
if ($thumbWidth == 0) $thumbWidth = 1;
if ($thumbHeight == 0) $thumbHeight = 1;
}
$createdThumb = p_w_picpathcreatetruecolor($this->thumbWidth, $this->thumbHeight);
$color = p_w_picpathcolorallocate($createdThumb,255,255,255);
p_w_picpathfill($createdThumb, 0, 0, $color);
if ( !$createdThumb ) {$this->errno = 20; return false;}
if ( !p_w_picpathcopyresampled($createdThumb, $Original, ($this->thumbWidth - $thumbWidth)/2, ($this->thumbHeight - $thumbHeight)/2, 0, 0,
$thumbWidth, $thumbHeight, $originalWidth, $originalHeight) )
{$this->errno = 21; return false;}
$this->_saveFunction($createdThumb, $this->thumbPrefix);
}
}
}
//------------------------------------------------------------------------
//建目錄函數(shù),其中參數(shù)$directoryName最后沒(méi)有"/",
//要是有的話(huà),以'/'打散為數(shù)組的時(shí)候,最后將會(huì)出現(xiàn)一個(gè)空值
function makeDirectory($directoryName) {
if(is_dir($directoryName)){
return true;
}
$directoryName = str_replace("\\","/",$directoryName);
$dirNames = explode('/', $directoryName);
$total = count($dirNames) ;
$temp = '';
for($i=0; $i<$total; $i++) {
$temp .= $dirNames[$i].'/';
if (!is_dir($temp)) {
$oldmask = umask(0);
if (!mkdir($temp, 0777)) exit("不能建立目錄 $temp");
umask($oldmask);
}
}
return true;
}
//---------------------------------------------------------------------------
// 文件格式檢查,MIME檢測(cè)
function validateFormat(){
if(!is_array($this->fileFormat)
|| in_array(strtolower($this->ext), $this->fileFormat)
|| in_array(strtolower($this->returninfo['type']), $this->fileFormat) )
return true;
else
return false;
}
//獲取文件擴(kuò)展名
function getExt($fileName){
$ext = explode(".", $fileName);
$ext = $ext[count($ext) - 1];
$this->ext = strtolower($ext);
}
//設(shè)置上傳文件的最大字節(jié)限制
// @param $maxSize 文件大小(bytes) 0:表示無(wú)限制
function setMaxsize($maxSize){
$this->maxSize = $maxSize;
}
//設(shè)置文件格式限定
// @param $fileFormat 文件格式數(shù)組
function setFileformat($fileFormat){
if(is_array($fileFormat)){$this->fileFormat = $fileFormat ;}
}
//設(shè)置覆蓋模式
// @param overwrite 覆蓋模式 1:允許覆蓋 0:禁止覆蓋
function setOverwrite($overwrite){
$this->overwrite = $overwrite;
}
//設(shè)置保存路徑
// @param $tmpPath 文件保存路徑:以 "/" 結(jié)尾,若沒(méi)有 "/",則補(bǔ)上
function setSavepath($tmpPath,$savePath){
$this->tmpPath = substr( str_replace("\\","/", $tmpPath) , -1) == "/"
? $tmpPath : $tmpPath."/";
$this->savePath = substr( str_replace("\\","/", $savePath) , -1) == "/"
? $savePath : $savePath."/";
}
//設(shè)置縮略圖
// @param $thumb = 1 產(chǎn)生縮略圖 $thumbWidth,$thumbHeight 是縮略圖的寬和高
function setThumb($thumb, $thumbWidth = 0,$thumbHeight = 0){
$this->thumb = $thumb;
if($thumbWidth) $this->thumbWidth = $thumbWidth;
if($thumbHeight) $this->thumbHeight = $thumbHeight;
}
//設(shè)置文件保存名
// @saveName 保存名,如果為空,則系統(tǒng)自動(dòng)生成一個(gè)隨機(jī)的文件名
function setSavename($saveName){
if ($saveName == ''){ // 如果未設(shè)置文件名,則生成一個(gè)隨機(jī)文件名
$name = date('YmdHis')."_".rand(100,999).'.'.$this->ext;
} else {
$name = $saveName;
}
$this->saveName = $name;
}
//刪除文件
// @param $file 所要?jiǎng)h除的文件名
function del($fileName){
if(!@unlink($fileName)){
$this->errno = 15;
return false;
}
return true;
}
// 返回上傳文件的信息
function getInfo(){
return $this->returnArray;
}
// 得到錯(cuò)誤信息
function errmsg(){
$uploadClassError = array(
0 =>'文件上傳成功 ',
1 =>'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
2 =>'The uploaded file exceeds the MAX_FILE_SIZE that was specified in the HTML form.',
3 =>'The uploaded file was only partially uploaded. ',
4 =>'No file was uploaded. ',
6 =>'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3. ',
7 =>'Failed to write file to disk. Introduced in PHP 5.1.0. ',
10 =>'Input name is not unavailable!',
11 =>'The uploaded file is Unallowable!',
12 =>'Directory unwritable!',
13 =>'File exist already!',
14 =>'File is too big!',
15 =>'Delete file unsuccessfully!',
16 =>'Your version of PHP does not appear to have GIF thumbnailing support.',
17 =>'Your version of PHP does not appear to have JPEG thumbnailing support.',
18 =>'Your version of PHP does not appear to have pictures thumbnailing support.',
19 =>'An error occurred while attempting to copy the source p_w_picpath .
Your version of php ('.phpversion().') may not have this p_w_picpath type support.',
20 =>'An error occurred while attempting to create a new p_w_picpath.',
21 =>'An error occurred while copying the source p_w_picpath to the thumbnail p_w_picpath.',
22 =>'An error occurred while saving the thumbnail p_w_picpath to the filesystem.
Are you sure that PHP has been configured with both read and write access on this folder?',
);
if ($this->errno == 0)
return false;
else
return $uploadClassError[$this->errno];
}
}
/**
* 調(diào)用上傳圖片類(lèi)
* 上傳圖片
* Enter description here ...
* @param $inputFile表單中上傳文件輸入框input的名字
* @param $thumb設(shè)置是否生成縮略圖,1:是,0:否
* @param $thumbWidth縮略圖寬,單位是像素(px),留空則使用默認(rèn)值 130
* @param $thumbHeight縮略圖高,單位是像素(px),留空則使用默認(rèn)值 130
* @param $thumbPrefix縮略圖前綴,沒(méi)有設(shè)置則使用默認(rèn)值 thumb_
*/
public function _uploadImg($inputFile){
$savePath = UPLOADIMGPATH.date('Y',time()).'/'.date('m',time()).'/'.date('d',time());
$tmpPath = TMPIMGPATH.date('Y',time()).'/'.date('m',time()).'/'.date('d',time());
$params['savePath'] = $savePath;
$params['tmpPath'] = $tmpPath;
$this->load->library('myupload',$params);
$this->myupload->setThumb(1); // 設(shè)置是否生成縮略圖
$this->myupload->round = 1; // 設(shè)置是否生成圓形圖
//生成多個(gè)縮略圖
$this->myupload->thumbArray = array('thumb_'=>array('width'=>76,'height'=>76));
//圓形圖生成縮略圖尺寸
$this->myupload->roundArray = array('round_pro_'=>array('width'=>76,'height'=>76));
//創(chuàng)建目錄
$this->myupload->makeDirectory($savePath); // 保存目錄
$this->myupload->makeDirectory($tmpPath); // 臨時(shí)目錄
//參數(shù)中的uploadinput是表單中上傳文件輸入框input的名字
//后面的0表示不更改文件名,若為1,則由系統(tǒng)生成隨機(jī)文件名
$this->myupload->run($inputFile,1);
//獲取上傳結(jié)果
return $this->myupload->getInfo();
}
?>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。