$path
$path :
图片处理上传model
<?php
/**
* 图片处理上传model
*/
class Upload_model extends CI_Model
{
private $path = 'attachment';
private $max_size = 0;
private $upFileType = '';
private $videotype = '';
private $videosize = 0;
private $audioSize = 0;
private $audioType = '';
public function __construct()
{
parent::__construct();
$this->upFileType = $this->config->item('imageType');
$this->videotype = $this->config->item('videoType');
$this->videosize = $this->config->item('videoSize');
$this->audioType = $this->config->item('audioType');
$this->audioSize = $this->config->item('audioSize');
//最大文件大小
$this->max_size = $this->config->item('imageSize');
//$this->load->library('Fastdfsclient');//fastdfs删除操作类
$this->load->library('Image');//图片处理类
}
/**
* 图片上传
* @param $filekey string POST请求中图片数据的key inputname
* @param $uploadfile string 图片文件
* @param $thumbconfig array 图片压缩配置
* */
public function uploadImage($filekey = '', $thumbFlag = FALSE)
{
$res = array(
'err' => 0,
'msg' => '上传成功',
'data' => array()
);
$filename = '';
// echo '<br />';
if (isset($_FILES[$filekey]["tmp_name"]) && $_FILES[$filekey]["tmp_name"] != "")
{
$name = explode(".", $_FILES[$filekey]["name"]); // 将上传前的文件以“.”分开取得文件类型
$imgCount = count($name); // 获得截取的数量
$imgType = $name[$imgCount - 1]; // 取得文件的类型
$imgType = strtolower($imgType);
if (strpos($this->upFileType, $imgType) === false)
{
return array(
'err' => 11,
'msg' => '上传文件类型仅支持' . $this->upFileType . ' 不支持 ' . $imgType
);
}
if (!empty($_FILES[$filekey]['error']))
{
$res = $this->_fileErr($_FILES[$filekey]['error']);
return $res;
}
$imgSize = $_FILES[$filekey]["size"];
$kSize = round($imgSize / 1024);
if (empty($kSize))
{
return array(
'err' => 12,
'msg' => '原文件已损坏,请重新上传'
);
}
if ($kSize > ($this->max_size * 1024))
{
return array(
'err' => 11,
'msg' => '上传文件超过' . $this->max_size . ' KB'
);
}
$mtime = explode(' ', microtime());
$new_filename = "$mtime[1]".rand(1,1000);
$new_filename_all = $new_filename.".$imgType";
$uploaddir = $this->path;
$uploadfile = $uploaddir . '/' . date('Ymd') . '/' . $new_filename_all;
// $a = $_FILES[$filekey]['tmp_name'];
// var_dump($a);
// echo '<br />';
// var_dump($uploadfile);
// echo '<br />';
$c = move_uploaded_file($_FILES[$filekey]['tmp_name'], $uploadfile);
if (@move_uploaded_file($_FILES[$filekey]['tmp_name'], $uploadfile)) // 移动到临时目录
{
$res['data']['filename'] = $uploadfile;
//var_dump($thumbFlag);exit;
if ($thumbFlag !== FALSE)
{
$imageInfo = pathinfo($uploadfile);
// $imageSize = $thumbFlag;
// foreach ($imageSize as $k => $v)
// {
// setDir($this->path . '/' . date('Ymd') . '/' . $k);
// $newimgname = $imageInfo['dirname'] . '/' . $k . '/' . $imageInfo['basename'];
// $thumbimg = $this->image->thumb($uploadfile, $v[0], $v[1], $newimgname);
// }
}
return $res;
}
else
{
return array(
'err' => 11,
'msg' => '上传时间超时'
);
}
}
else
{
if (!empty($uploadfile))
{
return array(
'err' => 11,
'msg' => '上传图片失败'
);
}
}
}
/**
* 上传文件和视频
* */
public function uploadfile($filekey='', $uploadfile='')
{
$res = array('err'=>0, 'msg'=>'上传成功', 'data'=>array());
$filename ='';
if (isset($_FILES[$filekey]["tmp_name"]) && $_FILES[$filekey]["tmp_name"] != "")
{
$name = explode(".",$_FILES[$filekey]["name"]);//将上传前的文件以“.”分开取得文件类型
$Count = count($name);//获得截取的数量
$Type = $name[$Count-1];//取得文件的类型
$Type = strtolower($Type);
if(strpos($this->videotype,$Type) === false){
return array('err'=>11, 'msg'=>'上传文件类型仅支持'.$this->videotype.' 不支持 '.$Type);
}
if (!empty($_FILES[$filekey]['error']))
{
$res = $this->_fileErr($_FILES[$filekey]['error']);
return $res;
}
$imgSize = $_FILES[$filekey]["size"];
$kSize = round($imgSize/1024);
if(empty($kSize))
{
return array('err'=>12, 'msg'=>'原文件已损坏,请重新上传');
}
if($kSize > ($this->videosize*1024)) {
return array('err'=>11, 'msg'=>'上传文件超过'.$this->videosize.' KB');
}
$uploaddir = $this->path;
$uploadfile = $uploaddir .'/'.date('YmdHis').'-'.md5(uniqid()).basename($_FILES[$filekey]['name']);
if (@move_uploaded_file($_FILES[$filekey]['tmp_name'], $uploadfile))//移动到临时目录
{
$filename = $uploadfile;
}
else
{
return array('err'=>11, 'msg'=>'上传失败,请确认你的上传文件不超过 '.$this->videosize.' KB 或上传时间超时');
}
}
else
{
if(!empty($uploadfile))
{
$filename = realpath($uploadfile);
}
}
if(!empty($filename))
{
$mainfile = $this->fastdfsclient->fdfs_upload($filename);
$res['data'] = $mainfile;
@unlink($filename);
}
else
{
return array('err'=>11, 'msg'=>'没有上传信息,请确认');
}
return $res;
}
/**
* 上传音频
* */
public function uploadAudio($filekey='', $uploadfile='')
{
$res = array('err'=>0, 'msg'=>'上传成功', 'data'=>array());
$filename ='';
if (isset($_FILES[$filekey]["tmp_name"]) && $_FILES[$filekey]["tmp_name"] != "")
{
$name = explode(".",$_FILES[$filekey]["name"]);//将上传前的文件以“.”分开取得文件类型
$Count = count($name);//获得截取的数量
$Type = $name[$Count-1];//取得文件的类型
$Type = strtolower($Type);
if(strpos($this->audioType,$Type) === false){
return array('err'=>11, 'msg'=>'上传文件类型仅支持'.$this->audioType.' 不支持 '.$Type);
}
if (!empty($_FILES[$filekey]['error']))
{
$res = $this->_fileErr($_FILES[$filekey]['error']);
return $res;
}
$imgSize = $_FILES[$filekey]["size"];
$kSize = round($imgSize/1024);
if(empty($kSize))
{
return array('err'=>12, 'msg'=>'原文件已损坏,请重新上传');
}
if($kSize > ($this->audioSize*1024)) {
return array('err'=>11, 'msg'=>'上传文件超过'.$this->audioSize.' KB');
}
$uploaddir = $this->path;
$uploadfile = $uploaddir .'/'.date('YmdHis').'-'.md5(uniqid()).basename($_FILES[$filekey]['name']);
if (@move_uploaded_file($_FILES[$filekey]['tmp_name'], $uploadfile))//移动到临时目录
{
$filename = $uploadfile;
}
else
{
return array('err'=>11, 'msg'=>'上传失败,请确认你的上传文件不超过 '.$this->audioSize.' KB 或上传时间超时');
}
}
else
{
if(!empty($uploadfile))
{
$filename = realpath($uploadfile);
}
}
if(!empty($filename))
{
$mainfile = $this->fastdfsclient->fdfs_upload($filename);
$res['data'] = $mainfile;
@unlink($filename);
}
else
{
return array('err'=>11, 'msg'=>'没有上传信息,请确认');
}
return $res;
}
/**
* 剪切图片
* */
public function cropimage($thumbconfig=array())
{
$avatar_src = isset($_POST['avatar_src']) ? $_POST['avatar_src'] : null;
$avatar_data = isset($_POST['avatar_data']) ? $_POST['avatar_data'] : null;
$avatar_file = isset($_FILES['avatar_file']) ? $_FILES['avatar_file'] : null;
$this->load->library('Cropimage');
$this->cropimage->cropimg($avatar_src, $avatar_data, $avatar_file);
/* $response = array(
'state' => 200,
'message' => $this->cropimage->getMsg(),
'result' => $this->cropimage->getResult()
); */
$filename = $this->cropimage->getResult();
$filename = realpath($filename);
$res = array();
if(!empty($filename))
{
$mainfile = $this->fastdfsclient->fdfs_upload($filename);
$imginfo = getimagesize($filename);
$res['data']['original'] = $mainfile;
$res['data']['originalwidth'] = !empty($imginfo[0]) ? $imginfo[0] : 1;
$res['data']['originalheight'] = !empty($imginfo[1]) ? $imginfo[1] : 1;
if(!empty($thumbconfig) && $mainfile)
{
//sleep(5);
foreach ($thumbconfig as $k=>$v)
{
$imageType = pathinfo($filename, PATHINFO_EXTENSION);
$newimgname = $k.date('YmdHis').'-'.md5(uniqid()).'.'. $imageType;
$thumbimg = $this->image->thumb($filename, $v[0], $v[1],$newimgname);
$thumbimgurl = $this->path.'/'.$thumbimg;
$thumbimgurl = realpath($thumbimgurl);
if(!empty($thumbimgurl))
{
$fdfsthum = $this->fastdfsclient->fdfs_upload_slave($thumbimgurl,$mainfile['group_name'],$mainfile['sourcefile'],'_'.$k);
if(!$fdfsthum)
{
$fdfsthum = $this->fastdfsclient->fdfs_upload_slave($thumbimgurl,$mainfile['group_name'],$mainfile['sourcefile'],'_'.$k);
}
}
else
{
$fdfsthum = $this->fastdfsclient->fdfs_upload_slave($filename,$mainfile['group_name'],$mainfile['sourcefile'],'_'.$k);
}
$res['data'][$k] = $fdfsthum;
@unlink($this->path.'/'.$thumbimg);
}
//exit;
}
@unlink($filename);
}
$res['state'] = 200;
$res['message'] = $this->cropimage->getMsg();
return $res;
//echo json_encode($response);
}
private function _fileErr($fileerr=0)
{
$res = array('err'=>0, 'msg'=>'成功');
switch($fileerr){
case '1':
$res['err'] = 1;
$res['msg'] = '超过php.ini允许的大小。';
break;
case '2':
$res['err'] = 2;
$res['msg'] = '超过表单允许的大小。';
break;
case '3':
$res['err'] = 3;
$res['msg'] = '只有部分被上传。';
break;
case '4':
$res['err'] = 4;
$res['msg'] = '请选择文件。';
break;
case '6':
$res['err'] = 6;
$res['msg'] = '找不到临时目录。';
break;
case '7':
$res['err'] = 7;
$res['msg'] = '写文件到硬盘出错。';
break;
case '8':
$res['err'] = 8;
$res['msg'] = 'File upload stopped by extension。';
break;
case '999':
default:
$res['err'] = 999;
$res['msg'] = '未知错误。';
}
return $res;
}
/**
* 获取图片路径
* */
public function picUrl($path ,$k='')
{
$url = '';
$strpos = strpos($path,"attachment");
if ($strpos === FALSE)
{
$imgurl = $this->config->item('img_url').'/';
}
else
{
$imgurl = $this->config->item('base_url').'/';
}
if(!empty($path) && !empty($k) && !$strpos)
{
$_filehz = pathinfo($path, PATHINFO_EXTENSION);
$pic = rtrim($path,'.'.$_filehz);
$path = $pic.'_'.$k.'.'.$_filehz;
}
if(!empty($path))
{
$url = $imgurl.$path;
}
return $url;
}
}
/* End of file Upload_model.php */