首页 » 后端 » PHP » 正文

图片上传类upimg.class.php

发布者:站点默认
2011/01/8 浏览数(2,694) 分类:PHP 图片上传类upimg.class.php已关闭评论

说明:

PHP图片上传类,通过文件头验证是否图片,可生成缩略图(缩略图有简单投影、阴影功能)。

预览:

PHP图片上传类

用法:

<?php
include('upimg.class.php');
if (isset($_GET['up'])){
 $up = new upimg("upload", "upload/thumb");
 $up->thumbWidth = 160;
 $up->thumbHeight = 120;
 $up->autoShandow = TRUE;
 $up->shandowX = 4;
 $up->shandowY = 4;
 $up->shondowColor = 'CCCCCC';
 $result = $up->upload('upfile'); // HTML中<input />的name属性值
 $thumb = $up->thumbPath;
 if (!$up->error){
 echo '成功';
 }else{
 echo '失败,错误信息:' . $up->error;
 }
}
echo '结果:<img src="' . $thumb . '" />'; // 如果成功返回图片地址,否则返回FALSE,错误信息保存在"$up->error"。
?>
<form method="post" action="?up&<?php echo rand();?>" enctype="multipart/form-data">
<input type="file" name="upfile"><br><br>
<input type="submit" value="上传">
</form>

下载:

upimg.class.php.tar.gz (为什么此附件的地址不能分享给朋友们下载?)

代码:

<?php
// 说明:图片上传类
// 日期:12:21 2011年1月8日 星期六
class upimg{
	public $uploadFolder = 'upload'; // 图片存放目录(末尾不要加“/”)
	public $thumbFolder = 'upload/thumb'; // 缩略图(末尾不要加“/”)
	public $autoSubfolder = TRUE; // 是否自动创建下级目录
	public $SubfolderFormat = 'Y-m'; // 子目录格式(使用DATE函数的格式)
	public $autoThumb = TRUE; // 是否自动生成缩略图
	public $thumbWidth = 160; // 缩略图最大宽度
	public $thumbHeight = 120; // 缩略图最大高度
	public $autoShandow = FALSE; // 是否自动对缩略图生成阴影
	public $shandowX = 5; // 投影相对于原图的左边距
	public $shandowY = 5; // 投影相对于原图的底边距
	public $shandowLength = 3; // 投影长度
	public $shondowColor = 'CCC'; // 四种投影颜色格式:#1FCC2B、#EEE、1FCC2B、EEE
	public $error = ''; // 错误信息
	public $imgPath = ''; // 上传成功后的图片位置
	public $thumbPath = ''; // 上传成功后的缩略图位置

	// 说明:初始化
	function __construct($uploadFolder = 'upload', $thumbFolder = 'upload/thumb'){
		empty($uploadFolder) ? '' : $this->uploadFolder = $uploadFolder;
		empty($thumbFolder) ? '' : $this->thumbFolder = $thumbFolder;
	}

	// 说明:上传图片,参数是<input />的name属性值;成功返回图片的相对URL,失败返回FALSE和错误信息(在$this->error里)
	// bool/sting upload(string $html_tags_input_attrib_name);
	function upload($inputName){ // 上传操作,参数是input标签的name属性。
		$this->_mkdir(); // 创建存放目录
		if ($this->error){ // 如果有错,直接返回(例如_mkdir)
			return FALSE;
		}
		if(!$_FILES[$inputName]["name"]){
			$this->error = '没有上传图片';
			return FALSE;
		}
		if($_FILES[$inputName]["name"]){
			$isUpFile = $_FILES[$inputName]['tmp_name'];
			if (is_uploaded_file($isUpFile)){
				$imgInfo = $this->_getinfo($isUpFile);
				if (FALSE == $imgInfo){
					return FALSE;
				}
				$extName = $imgInfo['type'];
				$microSenond = floor(microtime()*10000);// 取一个毫秒级数字,4位。
				$newFileName = $uploadFolder . '/' . date('YmdHis') . $microSenond . '.' . $extName ; // 所上传图片的新名字。
				$location = $this->uploadFolder . $newFileName;
				$result = move_uploaded_file($isUpFile, $location);
				if ($result){
					if (TRUE == $this->autoThumb){ // 是否生成缩略图
						$thumb = $this->thumb($location, $this->thumbWidth, $this->thumbHeight);
						if (FALSE == $thumb){
							return FALSE;
						}
					}
					$this->imgPath = $location;
					return $location;
				}else{
					$this->error = '移动临时文件时出错';
					return FALSE;
				}
			}else{
				$uploadError = $_FILES[$inputName]['error'];
				if (1 == $uploadError){ // 文件大小超过了php.ini中的upload_max_filesize
					$this->error = '文件太大,服务器拒绝接收大于' . ini_get('upload_max_filesize') . '的文件';
					return FALSE;
				}elseif (3 == $uploadError){ // 上传了部分文件
					$this->error = '上传中断,请重试';
					return FALSE;
				}elseif (4 == $uploadError){
					$this->error = '没有文件被上传';
					return FALSE;
				}elseif (6 == $uploadError){
					$this->error = '找不到临时文件夹,请联系您的服务器管理员';
					return FALSE;
				}elseif (7 == $uploadError){
					$this->error = '文件写入失败,请联系您的服务器管理员';
					return FALSE;
				}else{
					if (0 != $uploadError){
						$this->error = '未知上传错误,请联系您的服务器管理员';
						return FALSE;
					}
				}
			}
		}

	}

	// 说明:获取图片信息,参数是上传后的临时文件,成功返回数组,失败返回FALSE和错误信息
	// array/bool _getinfo(string $upload_tmp_file)
	private function _getinfo($img){
		$tempfile = @fopen($img, "rb");
		$bin = fread($tempfile, 2); //只读2字节
		fclose($tempfile);
		$strInfo = @unpack("C2chars", $bin);
		$typeCode = intval($strInfo['chars1'] . $strInfo['chars2']);
		$fileType = '';
		switch ($typeCode){ // 6677:bmp 255216:jpg 7173:gif 13780:png 7790:exe 8297:rar 8075:zip tar:109121 7z:55122 gz 31139
			case '255216':
				$fileType = 'jpg';
				break;
			case '7173':
				$fileType = 'gif';
				break;
			case '13780':
				$fileType = 'png';
				break;
			default:
				$fileType = 'unknown';
		}
		if ($fileType == 'jpg' || $fileType == 'gif' || $fileType == 'png'){
			$imageInfo = getimagesize($img);
			$imgInfo['size'] = $imageInfo['bits'];
			$imgInfo["type"] = $fileType;
			$imgInfo["width"] = $imageInfo[0];
			$imgInfo["height"] = $imageInfo[1];
			return $imgInfo;
		}else{ // 非图片类文件信息
			$this->error = '图片类型错误';
			return FALSE;
		}
	} // end _getinfo

	// 说明:生成缩略图,等比例绽放或拉伸
	// bool/string thumb(string $uploaded_file, int $thumbWidth, int $thumbHeight, string $thumbTail);
	function thumb($img, $thumbWidth = 300, $thumbHeight = 200, $thumbTail = '_thumb'){
		$filename = $img; // 保留一个名字供新的缩略图名字使用
		$imgInfo = $this->_getinfo($img);
		if (FALSE == $imgInfo){
			return FALSE;
		}
		$imgType = $imgInfo['type'];
		switch ($imgType){ // 创建一个图,并给出扩展名
			case "jpg" :
				$img = imagecreatefromjpeg($img);
				$ext_name = 'jpg';
				break;
			case 'gif' :
				$img = imagecreatefromgif($img);
				$ext_name = 'gif';
				break;
			case 'png' :
				$img = imagecreatefrompng($img);
				$ext_name = 'png';
				break;
			default : // 如果类型错误,生成一张空白图
				$img = imagecreate($thumbWidth,$thumbHeight);
				imagecolorallocate($img,0x00,0x00,0x00);
				$ext_name = 'jpg';
		}
		// 缩放后的图片尺寸(小则拉伸,大就缩放)
		$imgWidth = $imgInfo['width'];
		$imgHeight = $imgInfo['height'];
		if ($imgHeight > $imgWidth){ // 竖图
			$newHeight = $thumbHeight;
			$newWidth = ceil($imgWidth / ($imgHeight / $thumbHeight ));
		}else if($imgHeight < $imgWidth){ // 横图
			$newHeight = ceil($imgHeight / ($imgWidth / $thumbWidth ));
			$newWidth = $thumbWidth;
		}else if($imgHeight == $imgWidth){ // 等比例图
			$newHeight = $thumbWidth;
			$newWidth = $thumbWidth;
		}
		// 生成图层
		$bgimg = imagecreatetruecolor($newWidth,$newHeight);
		$bg = imagecolorallocate($bgimg,0xFF,0xFF,0xFF);
		imagefill($bgimg,0,0,$bg);
		// 投影
		if (TRUE == $this->autoShandow){
			// 处理投影长度
			$this->shandowX = abs($this->shandowX);
			$this->shandowX = abs($this->shandowX);
			// 处理投影颜色
			$shandowColor = strtoupper($this->shondowColor);
			if (empty($shandowColor)){
				$shandowColor = '#CCCCCC';
			}else{
				$shandowColorValueLength = strlen($shandowColor);
				if (7 == $shandowColorValueLength || 6 == $shandowColorValueLength){
					$rgbB = substr($this->shondowColor,-2,2);
					$rgbG = substr($this->shondowColor,-4,2);
					$rgbR = substr($this->shondowColor,-6,2);
				}else if (4 == $shandowColorValueLength || 3 == $shandowColorValueLength){
					$rgbB = substr($this->shondowColor,-1,1);
					$rgbG = substr($this->shondowColor,-2,1);
					$rgbR = substr($this->shondowColor,-3,1);
					$rgbB .= $rgbB; // C->CC
					$rgbG .= $rgbG; // G->GG
					$rgbR .= $rgbR;
				}else{
					$rgbB = $rgbG = $rgbR = 'CC';
				}
			}
			$rgbR = hexdec($rgbR);
			$rgbG = hexdec($rgbG);
			$rgbB = hexdec($rgbB);
			// 生成投影
			$newWidth = $newWidth - $this->shandowX;
			$newHeight = $newHeight - $this->shandowY;
			$shandowImg = imagecreatetruecolor($newWidth,$newHeight);
			$shandowBg = imagecolorallocate($shandowImg,$rgbR,$rgbG,$rgbB);
			@imagefill($shandowImg,0,0,$shandowBg);
			$shandowXOffset = round($this->shandowX / 2); // 偏移后留出模糊区域
			$shandowYOffset = round($this->shandowY / 2);
			$sampled = imagecopy ($bgimg, $shandowImg, $shandowXOffset, $shandowYOffset, 0, 0, $newWidth, $newHeight);
			$shandow = imagefilter($bgimg,IMG_FILTER_GAUSSIAN_BLUR); // 用高斯算法模糊图像
			$shandow = imagefilter($bgimg,IMG_FILTER_SELECTIVE_BLUR); // 模糊图像
		}
		// 生成图片
		$sampled = imagecopyresampled($bgimg,$img,0,0,0,0,$newWidth,$newHeight,$imgWidth,$imgHeight);

		if(!$sampled ){
			$this->error = '缩略图生成失败';
			@unlink($this->uploadFolder . '/' . $filename); // 删除上传的图片
			return FALSE;
		}
		$filename = basename($filename);
		$newFileName = substr($filename, 0, strrpos($filename, ".")) . $thumbTail . '.' . $ext_name ; // 新名字
		$thumbPath = $this->thumbFolder . '/' . $newFileName;
		switch ($ext_name){
			case 'jpg':
				$result = imagejpeg($bgimg, $thumbPath);
				break;
			case 'gif':
				$result = imagegif($bgimg, $thumbPath);
				break;
			case 'png':
				$result = imagepng($bgimg, $thumbPath);
				break;
			default: // 上边判断类型出错时会创建一张空白图,并给出扩展名为jpg
				$result = imagejpeg($bgimg, $thumbPath);
		}
		if ($result){
			$this->thumbPath = $thumbPath;
			return $thumbPath;
		}else{
			$this->error = '缩略图创建失败';
			@unlink($this->uploadFolder . '/' . $filename); // 删除上传的图片
			return FALSE;
		}
	} // end thumb

	// 说明:创建图片的存放目录
	private function _mkdir(){
		$baseDir = dirname($this->uploadFolder);
		if(!is_writable($baseDir)){
			$this->error = '基目录不可写';
			return FALSE;
		}
		// 创建图片上传目录
		if(!is_dir($this->uploadFolder)){
			$mkdirResutlt = @mkdir($this->uploadFolder);
			if (!$mkdirResutlt){
				$this->error = '存放图片的目录创建失败';
				return FALSE;
			}
		}
		// 创建缩略图目录
		if(!is_dir($this->thumbFolder) && TRUE == $this->autoThumb){
			$mkdirResutlt = @mkdir($this->thumbFolder);
			if (!$mkdirResutlt){
				$this->error = '存放缩略图的目录创建失败';
				return FALSE;
			}
		}
		// 创建子目录
		if($this->autoSubfolder){
			$subFolder = date($this->SubfolderFormat);
			$newUploadFolder = $this->uploadFolder . '/' . $subFolder;
			$newThumbFolder = $this->thumbFolder . '/' . $subFolder;
			if(!is_dir($newUploadFolder)){
				$mkdirResutlt = @mkdir($newUploadFolder);
				if(!$mkdirResutlt){
					$this->error = '下级图片目录创建失败';
					return FALSE;
				}
			}
			if(!is_dir($newThumbFolder)){
				$mkdirResutlt = @mkdir($newThumbFolder);
				if(!$mkdirResutlt){
					$this->error = '下级缩略图目录创建失败';
					return FALSE;
				}
			}
			$this->uploadFolder = $newUploadFolder;
			$this->thumbFolder = $newThumbFolder;
		}
	}
	// 调取图片的缩略图名字
	// 效果:/upload/20100506.jpg -> /upload/20100506_thumb.jpg
	public function getThumbPath($filename, $tail = '_thumb'){
		if(empty($filename)){
			return '';
		}
		$extstr=explode('.', $filename);
		if (2 <= count($extstr)){ // 有扩展名(可有多个,比如“filebasename.b.c.d.kkk”)
			$count=count($extstr)-1;
		}else{
			return $filename . $tail; // 没有扩展名的话,直接加个“尾巴”后输出
		}
		$extname = $extstr[$count];
		$newname = substr($filename, 0, strrpos($filename, ".")) . $tail . "." . $extname;
		return str_replace($this->uploadFolder, $this->thumbFolder, $newname);
	}
}
?>

相关阅读

<完>

点击返回顶部
  1. 留言
  2. 联系方式