首页 > 代码库 > PHP后台上传图片

PHP后台上传图片

<?php
/*
    by 的雨
*/
date_default_timezone_set(‘Asia/Chongqing‘);
header("content-type:text/html;charset=utf-8");
//这是上传图片的类
class Updateclass
{
	public $pathall;
	public $picname;
	public function update($file,$kaiqi=false,$w=0,$H=0)
	{
		//return $file;die;
		foreach($file as $key=>$val)
		{
		$updatename="{$key}";
		}
		$file=$file;
		$name=$file[$updatename][‘name‘]; //文件名称
		$size=$file[$updatename][‘size‘]; //文件大小
		$type=$file[$updatename][‘type‘]; //文件类型
		$tmp_name=$file[$updatename][‘tmp_name‘]; //文件临时文件
		$error=$file[$updatename][‘error‘]; //文件错误
		if(($error))
		{
			switch($error)
			{
				case 1:
				$s="超过了配置的大小";
				break;
				case 2:
				$s="超过了表单的大小";
				break;
				case 3:
				$s="部分文件上传";
				break;
				case 4:
				$s="没有文件";
				break;
				case 6:
				$s="找不到临时文件";
				break;
				case 7:
				$s="文件不可写";
				break;
				case 8:
				$s="由于扩展PHP程序中断文件上传";
				break;
			}
			$re=‘上传的错误提示‘;
			return $s.‘>‘.$re;
			
		}
		else
		{
			
			//判断是否是数组中的类型
			$arr=array(‘image/jpeg‘,‘image/jpg‘,‘image/png‘,‘image/gif‘);
			if(!in_array($type,$arr))
			{
				$re=‘上传的错误提示‘;
				$s=‘上传的格式不对‘;
				return $s.‘>‘.$re;
				exit;
			}
			//判断是否是http post提交
			if(!is_uploaded_file($tmp_name))
			{
				$re=‘上传的错误提示‘;
				$s=‘此文件不是通过post提交‘;
				return $s.‘>‘.$re;
				exit;
			}
			else
			{
				//获取真实的名字,以时间为命名
				$NAME=date(‘Ymd‘,time()).rand(0,999999999);
				$hou=explode(‘.‘,$name);
				$houzui=$hou[count($hou)-1];
				$p=date(‘Ym‘);
				
				$path=‘uploade/‘.$p;
				$this->pathall=$path;
				$this->picname=$NAME.‘.‘.$houzui;
				if(!is_dir($path))
				{
					mkdir($path,0777,true);
					$path=$path.‘/‘.$NAME.‘.‘.$houzui;
					
					if(move_uploaded_file($tmp_name,$path))
					{
						if($kaiqi==true)
						{
							$ys=$this->yasuo($path,$w,$H);
						}
						if($kaiqi==true)
						{
						return $path.‘*‘.$ys."@";	
						}
						else
						{
							return $path.‘*‘;
						}
						
						
					}
					else
					{
						return "上传失败";
					}
				}
				else
				{
					$path=$path.‘/‘.$NAME.‘.‘.$houzui;
					
					if(move_uploaded_file($tmp_name,$path))
					{
						if($kaiqi==true)
						{
							$yasuo=$this->yasuo($path,$w,$H);
						}
						
						///echo "<div style=\"width:400px\"><img width=\"400px\" src=http://www.mamicode.com/"$path\"></div>";
						if($kaiqi==true)
						{
						return $path.‘*‘.$ys."@";	
						}
						else
						{
							return $path.‘*‘;
						}
						
					}
					else
					{
						return "上传失败";
					}
				}
				
			}
		}
		}

	public function yasuo($from,$W,$H){	
	$yasuo_path=$this->pathall.‘/‘.date(‘Ym‘);
	if(!is_dir($yasuo_path))
	{
		mkdir($yasuo_path,0777,true);
	}
	$from="$from";	
	list($width,$height,$imagestype)=getimagesize($from);    //获得原来照片的宽,高,文件类型										
	$get_style=image_type_to_mime_type($imagestype);          //这是获取图片类型 image/jpeg
	//等同于 $get_style=getimagesize($from)[mine]; 								
	$imagecreatefrom=str_replace(‘/‘,‘createfrom‘,$get_style); //替换成 imagecreatefromjpeg										
	$imagejpeg=str_replace(‘/‘,null,$get_style);    //替换成 imagejpeg
	$yuanben_image=$imagecreatefrom($from);  //imagecreatefromjpeg()  由文件或 URL 创建一个新图象,原来的图片
	$yasuo_image=imagecreatetruecolor($W,$H);  //这是创建一个新的画布,存放目标图片
	imagecopyresampled($yasuo_image,$yuanben_image,0,0,0,0,$W,$H,$width,$height); //imagecopyresampled 重采样拷贝部分图像并调整大小
	//imagecopyresampled(目标图象连接资源,源图象连接资源,目标 X 坐标点,目标 Y 坐标点,源的 X 坐标点,源的 Y 坐标点,目标宽度,目标高度,源图象的宽度,源图象的高度);
	//header(‘content-type: $get_style‘);   //头部输出图片的类型  header(‘Content-Type: image/jpeg‘);
	$imagejpeg($yasuo_image,"$yasuo_path/".‘yasuo‘.$this->picname); //网页中输出的图片类型,压缩图保存的位置
	return "$yasuo_path/".‘yasuo‘.$this->picname;
	imagedestroy($yasuo_image);              //销毁压缩后的图片
	imagedestroy($yuanben_image);	//销毁原来的图片
	}	
}

?>

    PHP后台上传图片加压缩图片,个人是新手,若有错误,请多多包涵》

本文出自 “11037915” 博客,请务必保留此出处http://11047915.blog.51cto.com/11037915/1874042

PHP后台上传图片