//思路比较简单,用户填写自己的信息,并上传图像(后台生成缩略图),然后图像可以立即无刷新的回显
下面这个事上传和缩略图的类,比较简单:
<?php
require_once "sql.tool.php";
class Picture
{
//上传文件
private $upfile;
//保存后的源文件
private $img;
//上传文件的类型
private $fileType;
//上传图片保存的路径
private $path;
//上传图片的名字
private $fname;
//缩略图的宽度
private $width;
//缩略图的高度
private $height;
//错误信息
private $noticeMsg=null;
private $sql;
public function __construct($upfile)
{
$config = require "config.php";
$this->width=$config['image']['width'];
$this->height=$config['image']['height'];
if(is_uploaded_file($upfile['tmp_name']))
{
$this->upfile = $upfile;
}
else
{
$this->noticeMsg = '非法文件';
}
$this->sql = new Sql();
$this->moveFile();
$this->zoomInPic();
echo "<script> parent.document.getElementById('feedback').innerHTML='{$this->noticeMsg}'</script>";
}
private function checkFilesStatus()
{
//上传失败,分析原因
if($this->upfile['error']>0)
{
switch($this->upfile['error'])
{
case 1:
//超过表单定义的MAX_FILE_SIZE
$this->noticeMsg = '文件太大';
break;
case 2:
//超过系统定义,php.ini中设定的
$this->noticeMsg = '文件太大';
break;
case 3:
$this->noticeMsg = '文件不完整';
break;
case 4:
$this->noticeMsg = '文件为空';
break;
}
return 0;
}
else
{//上传成功
$this->noticeMsg = '上传成功';
return 1;
}
}
//检测上传文件类型
private function checkFileType()
{
$fileType = strtolower(substr(strchr($this->upfile['name'],'.'),1));
$this->fileType = $fileType;
$defaultType=array('jpg', 'jpeg', 'png', 'gif');
if(!in_array($fileType, $defaultType))
{
$this->noticeMsg='文件类型不对';
return 0;
}
return 1;
}
//保存上传文件
private function moveFile()
{
if($this->checkFilesStatus() && $this->checkFileType())
{
$dirName = './upload/'.date("Ymd");
if(!is_dir($dirName))
{
mkdir($dirName,0777, true);
}
$toFileName = $dirName."/".time().mt_rand(1,500).$this->upfile['name'];
$this->path = $dirName;
if(!move_uploaded_file($this->upfile['tmp_name'], $toFileName))
{
die('移动文件失败');//系统错误
}
else
{
$this->img = $toFileName;
}
}
}
private function fileName()
{
$fname = basename($this->upfile['name'], strchr($this->upfile['name'], '.'));
$this->fname = time().mt_rand(1,500).$fname.'small';
}
//生成图片缩略图
private function zoomInPic()
{
$src;
$this->fileName();
switch($this->fileType)
{
case 'jpg':
case 'jpeg':
$src = imagecreatefromjpeg($this->img);
break;
case 'gif':
$src = imagecreatefromgif($this->img);
break;
case 'png':
$src = imagecreatefrompng($this->img);
break;
}
//获取上传图片的宽和高
$src_w = imagesx($src);
$src_h = imagesy($src);
//生成缩略图
//在属性里定义缩略图的宽和高,并将原图按照比例缩放
//缩放算法是: 新宽/原宽 = 新高/原高,这样就能等比例缩放了.....
/* if($src_w > $src_h)
{
$new_w = $this->width;
$new_h = ceil(($this->width/$src_w)*$src_h);
}
else
{
$new_h = $this->height;
$new_w = ceil(($this->height/$src_h)*$src_w);
} */
$new_h = $this->height;
$new_w = ceil(($this->height/$src_h)*$src_w);
$paint = imagecreatetruecolor($new_w, $new_h);
imagecopyresampled($paint, $src, 0,0,0,0, $new_w, $new_h, $src_w, $src_h);
//在原图上保存缩略图
switch($this->fileType)
{
case 'jpg':
case 'jpeg':
$src = imagejpeg($paint, $this->path.'/'.$this->fname.'.'.$this->fileType);
break;
case 'gif':
$src = imagegif($paint, $this->path.'/'.$this->fname.'.'.$this->fileType);
break;
case 'png':
$src = imagepng($paint, $this->path.'/'.$this->fname.'.'.$this->fileType);
break;
}
//释放资源
imagedestroy($paint);
sleep(2);
unlink($this->img);
//因为返回的是img链接资源,所以可以省略HEADER头部
$this->noticeMsg="<img src=".$this->path.'/'.$this->fname.'.'.$this->fileType.">";
$sources = $this->path.'/'.$this->fname.'.'.$this->fileType;
$this->sql->setImage("$sources");
}
}
?>
下面这个事前段页面:
<html>
<head>
<style type="text/css">
*{margin:0; padding:0;}
div#formarea{margin-left: 400px;}
div#upload{position:relative ; left:590px; top:5px;}
div#uparea{border:2px solid #826858; width:120px; height:160px;background-color:#f2eada;}
input#pic{margin-top:60px; border: 0px; margin-left:10px; width:60px; font-size:12px; background-color:#f2eada; filter:Alpha(opacity=10);}
span#feedback{position:absolute; top:0px; left:5px; color:#a00000;z-index:5;}
input#submit{position:relative ; left:70px; top:-20px;}
div#userinfo{position:absolute;left: 405px;top:110px;width:710px; height:300px; border:2px solid #f2eada;}
p#username{margin-top:5px;}
input#username{width:300px; height:30px;}
p#sex{margin-top:5px;}
p#hobby{margin-top:8px;}
input#hobby{width:300px; height:30px;}
p#jpb{margin-top:10px;}
input#job{width:300px; height:30px;}
p#intro{margin-top:20px;}
span#intro{position:relative; top:-100px;}
input#pub{width:60px; height:30px; position:relative; left:530px; top:-32px; }
</style>
<script type="text/javascript">
function process()
{
document.getElementById("feedback").innerHTML="<img src='../image/loading.gif' width='30px' />";
}
</script>
<head>
<body bgcolor="#RGB(67,65,65)">
<img src="../image/friend.jpg" style='margin-left:400px;'>
<div id="formarea">
<div id="userinfo">
<form action="upfile.process.php" method="post">
<input type="hidden" name="uploadtype" value="userinfo" >
<p id="username" >姓名: <input type="text" id="username" name="username"></p>
<p id="sex">性别: <input type="radio" name="sex" value="男" checked="checked">男<input type="radio" name="sex" value="女">女<input type="radio" name="sex" value="*">保密</p>
<p id="hobby">爱好: <input type="text" id="hobby" name="hobby"></p>
<p id="jpb">职业: <input type="text" id="job" name="job"></p>
<p id="intro"><span id="intro">个人简介</span><textarea cols="60" rows="8" id="intro" name="intro"></textarea></p>
<input type="submit" id="pub" value="发布">
</form>
</div><!-- 用户信息-->
<div id="upload">
<form action="upfile.process.php" method="post" enctype="multipart/form-data" target='frameworks' οnsubmit="return process();" >
<div id="uparea">
<input type="hidden" name="MAX_FILE_SIZE" value="2100000">
<input type="file" name="pic" id="pic" /><br />
<span id="feedback"></span>
<input type="hidden" name="uploadtype" value="userpic" >
<input type="submit" value="上传" id="submit"><br />
</div><!--uparea-->
</form>
<div><!-- 上传区域-->
</div><!--表单区域-->
<iframe name='frameworks' style='border:0; frameborder:0; display:hidden;'>
</iframe>
</body>
</html>
//这个类功能比较杂,因为我写的一个意见反馈的数据库操作也封装在这里了。。对于这个交友模块,这个类主要负责把用户上传的图像的路径保存到数据库里
<?php
class Sql
{
private $host;
private $username;
private $passwd;
private $dbname;
private $tablename;
private $conn;
public $errMsg=0;
public function __construct()
{
$config = require "config.php";
$this->host = $config['db']['host'];
$this->username = $config['db']['username'];
$this->passwd = $config['db']['passwd'];
$this->dbname = $config['db']['dbname'];
$this->tablename = $config['db']['tablename'];
$this->conn = new mysqli("{$this->host}", "{$this->username}", "{$this->passwd}");
if(!$this->conn) $this->errMsg="链接数据库失败";
$this->conn->select_db("{$this->dbname}");
$this->conn->query("SET NAMES utf8");
}
public function setInfo($name='', $sex='', $hobby='', $job='', $intro='')
{
session_start();
$last_insert_id = $_SESSION['uid'][0];
$sql = "UPDATE {$this->tablename} SET name='$name' , sex ='$sex', hobby='$hobby', job='$job', intro='$intro' WHERE id = {$last_insert_id} ";
//file_put_contents("info.txt", $sql."\r\n",FILE_APPEND);exit();
$this->conn->query($sql);
if($this->conn->affected_rows <=0)
{
$this->errMsg = "用户信息插入数据库失败";
}
}
public function setImage($image='')
{
$sql = "INSERT INTO {$this->tablename}(image) VALUES('$image')";
$this->conn->query($sql);
if($this->conn->affected_rows > 0)
{
$result = $this->conn->query("SELECT LAST_INSERT_ID() FROM $this->tablename");
$last_insert_id = $result->fetch_row();
session_start();
$_SESSION['uid']= $last_insert_id ;
$result->free();
}
else
{
$this->errMsg = "用户图片插入数据库失败";
}
}
public function setAdvice($advice)
{
$sql = "INSERT INTO advice(adv) VALUES('$advice')";
$this->conn->query($sql);
if(!($this->conn->affected_rows>0)) $this->errMsg='用户建议插入失败';
}
public function getAdvice()
{
$sql = "SELECT adv FROM advice";
$result = $this->conn->query($sql);
$advices;
while($row = $result->fetch_row())
{
$advices[]=$row;
}
$result->free();
return $advices;
}
public function getInfo()
{
$sql = "SELECT * FROM {$this->tablename}";
$result = $this->conn->query($sql);
$info;
if(!($this->conn->affected_rows > 0))
{
$this->errMsg = '获取用户信息失败';
}
while($row = $result->fetch_assoc())
{
$info[]=$row;
}
$result->free();
return $info;
}
}
?>
//下面这个事控制器
<?php
require_once "upfile.class.php";
require_once "sql.tool.php";
if($_POST['uploadtype']=="userpic")
{
if(!empty($_FILES['pic']['name']))
{
$picture = new Picture($_FILES['pic']);
}
}
else if($_POST['uploadtype']=="userinfo")
{
$sql = new Sql();
if(!empty($_POST['username']) && !empty($_POST['sex']) && !empty($_POST['hobby']) && !empty($_POST['job']) && !empty($_POST['intro']))
{
$username = $_POST['username'];
$sex = $_POST['sex'];
$hobby = $_POST['hobby'];
$job = $_POST['job'];
$intro = $_POST['intro'];
$sql->setInfo("$username", "$sex", "$hobby", "$job", "$intro");
if($sql->errMsg ==0)
{
header("Location: homepage.php");
exit();
}
else
{
echo $sql->errMsg;
}
}
else
{
echo "<script>alert('输入不能为空'); history.go(-1)</script>";
}
}
?>
//下面这个是显示交友信息的页面
<html>
<head>
<style type="text/css">
*{margin:0px; padding:0px;}
div#layout{margin-left:415px; margin-top:5px; width:700px; }
div#user{width:680px; height: 162px; border:1px solid #cde6c7;background-color:#918597;}
div#info{position:relative; top:-146px; left: 200px;}
p#name{margin-top:-3px;}
p{margin-top: 12px; color:#56452d; }
p#intro{margin-bottom:12px;}
span{color:#6950a1;margin-left:30px;}
span#image{margin-left:1px; margin-bottom: 0px; height:162px;}
</style>
<head>
<body bgcolor="#RGB(67,65,65)">
<img src="../image/addfriend.jpg" style='margin-left: 400px; margin-top:1px;'>
<div id="layout">
<?php
require_once "sql.tool.php";
$sql = new Sql();
$items = $sql->getInfo();
foreach($items as $item)
{
echo "<div id='user'>
<span id='image'><img src=\"{$item['image']}\"></span>
<div id='info'>
<p id='name'>姓 名: <span id='name'>{$item['name']}</span></p>
<p id='sex'>性 别: <span id='sex'>{$item['sex']}</span></p>
<p id='hobby'>爱 好: <span id='hobby'>{$item['hobby']}</span></p>
<p id='job'>职 业: <span id='job'>{$item['job']}</span></p>
<p id='intro'>简 介: <span id='intro'>{$item['intro']}</span></p>
</div></div><br />";
}
?>
</div>
</body>
</html>
//本人的前段代码写的是在是一般般,也没有夸浏览器调试,主要是在chrome下试过,界面可能在不同的浏览器下显示会存在一些差异