formname = $_formname;
$this->directory = $_directory;
$this->maxsize = $_maxsize;
$this->canupload = true;
$this->doupfile = '';
$this->sm_file = '';
}
//判断图片是否属于允许格式内
static public function type($_formname='file'){
$_type = $_files[$_formname]['type'];
switch ($_type){
case 'image/gif':
if (self::$a==null)
self::$a = new imgupload($_formname);
break;
case 'image/pjpeg':
if (self::$a==null)
self::$a = new imgupload($_formname);
break;
case 'image/x-png':
if (self::$a==null)
self::$a = new imgupload($_formname);
break;
default:
self::$a = false;
}
return self::$a;
}
//获取文件大小
public function getsize($_format='k'){
if ($this->canupload) {
if (0 == $_files[$this->formname]['size']) {
$this->canupload = false;
return $this->canupload;
break;
}
switch ($_format){
case 'b':
return $_files[$this->formname]['size'];
break;
case 'k':
return round($_files[$this->formname]['size'] / 1024);
break;
case 'm':
return round($_files[$this->formname]['size'] / (1024*1024),2);
break;
}
}
}
//获取文件类型
public function getext(){
if ($this->canupload) {
$_name = $_files[$this->formname]['name'];
$_namearr = explode('.',$_name);
$_count = count($_namearr)-1;
}
return $_namearr[$_count];
}
//获取文件名称
public function getname(){
if ($this->canupload) {
return $_files[$this->formname]['name'];
}
}
//新建文件名
public function newname(){
return date('ymdhis').rand(0,9);
}
//上传文件
public function upload(){
if ($this->canupload)
{
$_getsize = $this->getsize('b');
if (!$_getsize)
{
return $_getsize;
break;
}
else
{
$_newname = $this->newname();
$_ext = $this->getext();
$_doupload = move_uploaded_file($_files[$this->formname]['tmp_name'], $this->directory.$_newname.".".$_ext);
if ($_doupload)
{
$this->doupfile = $_newname;
}
return $_doupload;
}
}
}
//创建缩略图
public function thumb($_dstchar='_m', $_max_len=320){ //$_dstchar:_m , _s
if ($this->canupload && $this->doupfile != "") {
$_ext = $this->getext();
$_srcimage = $this->directory.$this->doupfile.".".$_ext;
//得到图片信息数组
$_date = getimagesize($_srcimage, &$info);
$src_w = $_date[0]; //源图片宽
$src_h = $_date[1]; //源图片高
$src_max_len = max($src_w, $src_h); //求得长边
$src_min_len = min($src_w, $src_h); //求得短边
$dst_w = ''; //目标图片宽
$dst_h = ''; //目标图片高
//宽高按比例缩放,最长边不大于$_max_len
if ($src_max_len>$_max_len)
{
$percent = $src_min_len / $src_max_len;
if ($src_w == $src_max_len)
{
$dst_w = $_max_len;
$dst_h = $percent * $dst_w;
}
else
{
$dst_h = $_max_len;
$dst_w = $percent * $dst_h;
}
}
else
{
$dst_w = $src_w;
$dst_h = $src_h;
}
//建立缩略图时,源图片的位置
$src_x = '';
$src_y = '';
//判断如果缩略图用于logo,将对其进行裁减
if ('s_' == $_dstchar) {
$src_x = $src_w * 0.10;
$src_y = $src_h * 0.10;
$src_w *= 0.8;
$src_h *= 0.8;
}
//判断图片类型并创建对应新图片
switch ($_date[2]){
case 1:
$src_im = imagecreatefromgif($_srcimage);
break;
case 2:
$src_im = imagecreatefromjpeg($_srcimage);
break;
case 3:
$src_im = imagecreatefrompng($_srcimage);
break;
case 8:
$src_im = imagecreatefromwbmp($_srcimage);
break;
}
//创建一幅新图像
if ($_date[2]==1) { //gif无法应用imagecreatetruecolor
$dst_im = imagecreate($dst_w, $dst_h);
}else {
$dst_im = imagecreatetruecolor($dst_w, $dst_h);
}
//对这副图像进行缩略图copy
// $bg = imagecolorallocate($dst_im,255,255,0);
imagecopyresized($dst_im,$src_im, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
//对图片进行抗锯齿操作
imageantialias($dst_im, true);
switch ($_date[2]) {
case 1:
$cr = imagegif($dst_im, $this->directory.$this->doupfile.$_dstchar.".".$_ext, 100);
break;
case 2:
$cr = imagejpeg($dst_im, $this->directory.$this->doupfile.$_dstchar.".".$_ext, 100);
break;
case 3://imagepng有问题,所以在这里用imagejpg代替
$cr = imagejpeg($dst_im, $this->directory.$this->doupfile.$_dstchar.".".$_ext, 100);
break;
}
// $cr = imagejpeg($dst_im, $this->directory.$_dstchar.$this->doupfile, 90);
if ($cr) {
$this->sm_file = $this->directory.$this->doupfile.$_dstchar.".".$_ext;
return $this->sm_file;
}else {
return false;
}
}
imagedestroy($dst_im);
imagedestroy($cr);
}
//得到上传后的文件名
public function getupfile(){
if ($this->doupfile!='') {
$_ext = $this->getext();
return $this->doupfile.".".$_ext;
}else {
return false;
}
}
//得到上传后的文件全路径
public function getfilepatch(){
if ($this->doupfile!='') {
$_ext = $this->getext();
return $this->directory.$this->doupfile.".".$_ext;
}else {
return false;
}
}
//得到缩略图文件全路径
public function getthumb(){
if ($this->sm_file!='') {
return $this->sm_file;
}else {
return false;
}
}
//得到上传文件的路径
public function getdirectory(){
if ($this->directory!='') {
return $this->directory;
}else {
return false;
}
}
}
?>
用户登录
还没有账号?立即注册
用户注册
投稿取消
| 文章分类: |
|
还能输入300字
上传中....
我可是巴拉巴拉小魔仙呢