实例介绍
【实例简介】
【实例截图】
【核心代码】
<?php class upimg{ public $uploadFolder = 'upload'; // 图片存放目录 public $thumbFolder = 'upload/thumb'; // 缩略图存放目录 public $thumbWidth = ''; // 缩略图宽度 public $thumbHeight = ''; // 缩略图高度 public $autoThumb = ''; // 是否自动生成缩略图 public $error = ''; // 错误信息 public $imgPath = ''; // 上传成功后的图片位置 public $thumbPath = ''; // 上传成功后的缩略图位置 public $maxsize=''; // 说明:初始化,创建存放目录 function __construct($uploadFolder = 'upload', $thumbFolder = 'upload/thumb'){ $this->uploadFolder = $uploadFolder; $this->thumbFolder = $thumbFolder; $this-> _mkdir(); } /* function __construct($options=array()){ if(!empty($options)){ $options_values = array_values($options); foreach(array_keys($options) as $index => $key){ $this->$key = $options_values[$index]; } } $this->_mkdir(); }*/ // 说明:上传图片,参数是<input />的name属性值;成功返回图片的相对URL,失败返回FALSE和错误信息(在$this->error里) // bool/sting upload(string $html_tags_input_attrib_name); function upload($inputName){ // 上传操作,参数是input标签的name属性。 if ($this->error){ // 如果有错,直接返回(例如_mkdir) return FALSE; } if(!$_FILES[$inputName]["name"]){ $this->error = '没有上传图片'; return FALSE; } //检测文件大小 if($_FILES[$inputName]["size"] > ($this->maxsize*1024)){ $this->error = '上传文件'.$inputName.'太大,最大支持'.ceil($this->maxsize/1024).'kb的文件'; 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; } } //是否删除原图 if(TRUE==$this->srcDel) { @unlink($location); } $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; } } // end if $uploadError } // end if is_uploaded_file else } // end if $_FILES[$inputName]["name"] } // 说明:获取图片信息,参数是上传后的临时文件,成功返回数组,失败返回FALSE和错误信息 // array/bool _getinfo(string $upload_tmp_file) private function _getinfo($img){ if (!file_exists($img)){ $this->error = '找不到图片,无法获取其信息'; return FALSE; } $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 '6677': $fileType = 'bmp'; break; case '7173': $fileType = 'gif'; break; case '13780': $fileType = 'png'; break; default: $fileType = 'unknown'; } if ($fileType == 'jpg' || $fileType == 'gif' || $fileType == 'png' || $fileType == 'bmp'){ $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,$i); if(FALSE == $imgInfo) { return FALSE; } $imgType = $imgInfo['type']; switch ($imgType) { // 创建一个图,并给出扩展名 case "jpg" : $img = imagecreatefromjpeg($img); $extName = 'jpg'; break; case 'gif' : $img = imagecreatefromgif($img); $extName = 'gif'; break; case 'bmp' : $img = imagecreatefromgif($img); $extName = 'bmp'; break; case 'png' : $img = imagecreatefrompng($img); $extName = 'png'; break; default : // 如果类型错误,生成一张空白图 $img = imagecreate($thumbWidth,$thumbHeight); imagecolorallocate($img,0x00,0x00,0x00); $extName = '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,0x00,0x00,0x00); imagefill($bgimg,0,0,$bg); $sampled = imagecopyresampled($bgimg,$img,0,0,0,0,$newWidth,$newHeight,$imgWidth,$imgHeight); if(!$sampled ) { $this->error = '缩略图生成失败'; $this->path=$this->uploadFolder . '/' . $filename; return FALSE; } $filename = basename($filename); $newFileName = substr($filename, 0, strrpos($filename, ".")) . $thumbTail . '.' . $extName ; // 新名字 $thumbPath = $this->thumbFolder . '/' . $newFileName; switch ($extName){ 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; $this->path=$this->uploadFolder . '/' . $filename; return $thumbPath; } else { $this->error = '缩略图创建失败'; $this->path=$this->uploadFolder . '/' . $filename; return FALSE; } } // end thumb // 说明:创建图片的存放目录 private function _mkdir() { // 创建图片上传目录和缩略图目录 if(!is_dir($this->uploadFolder)) { $dir = explode('/', $this->uploadFolder); foreach($dir as $v) { if($v) { $d .= $v . '/'; if(!is_dir($d)) { $state = mkdir($d); if(!$state) { $this->error = '在创建目录' . $d . '时出错!'; } } } } } if(!is_dir($this->thumbFolder) && TRUE == $this->autoThumb) { $dir = explode('/', $this->thumbFolder); foreach($dir as $v) { if($v) { $d .= $v . '/'; if(!is_dir($d)) { $state = mkdir($d); if(!$state) { $this->error = '在创建目录' . $d . '时出错!'; } } } } } } } ?>
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论