PHP上传图片类显示缩略图功能
有缩略图功能但是感觉不全面,而且有点问题,继续学习,将来以后修改下
<formaction="<?php$_SERVER['PHP_SELF'];?>"enctype="multipart/form-data"method="post"><inputtype="text"name="name"/><inputtype="file"name="file"/><inputtype="submit"name='submit'value="提交"></form>
<?php
/**
*CreatedbyPhpStorm.
*User:Administrator
*Date:2016/6/28
*Time:21:04
*/
classupload{
protected$fileMine;//文件上传类型
protected$filepath;//文件上传路径
protected$filemax;//文件上传大小
protected$fileExt;//文件上传格式
protected$filename;//文件名
protected$fileerror;//文件出错设置
protected$fileflag;//文件检测
protected$fileinfo;//FILES
protected$ext;//文件扩展
protected$path;
//文件上传
publicfunction__construct($filename="file",$filemax=20000000,$filepath="./Uploads",$fileflag=true,$fileExt=array('jpg','exe'),$fileMine=array('image/jpeg'))
{
$this->filename=$filename;
$this->fileinfo=$_FILES[$this->filename];
$this->filemax=$filemax;
$this->filepath=$filepath;
$this->fileflag=$fileflag;
$this->fileExt=$fileExt;
$this->fileMine=$fileMine;
//var_dump($this->filename);
}
//错误判断
publicfunctionUpError(){
if($this->fileinfo['error']>0){
switch($this->fileinfo['error'])
{
case1:
$this->fileerror="上传文件大小超过服务器允许上传的最大值,php.ini中设置upload_max_filesize选项限制的值";
break;
case2:
$this->fileerror="上传文件大小超过HTML表单中隐藏域MAX_FILE_SIZE选项指定的值";
break;
case3:
$this->fileerror="文件部分被上传";
break;
case4:
$this->fileerror="没有选择上传文件";
break;
case5:
$this->fileerror="未找到临时目录";
break;
case6:
$this->fileerror="文件写入失败";
break;
case7:
$this->fileerror="php文件上传扩展没有打开";
break;
case8:
$this->fileerror="";
break;
}
returnfalse;
}
returntrue;
}
//检测文件类型
publicfunctionUpMine(){
if(!in_array($this->fileinfo['type'],$this->fileMine)){
$this->error="文件上传类型不对";
returnfalse;
}
returntrue;
}
//检测文件格式
publicfunctionUpExt(){
$this->ext=pathinfo($this->fileinfo['name'],PATHINFO_EXTENSION);
//var_dump($ext);
if(!in_array($this->ext,$this->fileExt)){
$this->fileerror="文件格式不对";
returnfalse;
}
returntrue;
}
//检测文件路径
publicfunctionUpPath(){
if(!file_exists($this->filepath)){
mkdir($this->filepath,0777,true);
}
}
//检测文件大小
publicfunctionUpSize(){
$max=$this->fileinfo['size'];
if($max>$this->filemax){
$this->fileerror="文件过大";
returnfalse;
}
returntrue;
}
//检测文件是否HTTP
publicfunctionUpPost(){
if(!is_uploaded_file($this->fileinfo['tmp_name'])){
$this->fileerror="恶意上偿还";
returnfalse;
}
returntrue;
}
//文件名防止重复
publicfunctionUpname(){
returnmd5(uniqid(microtime(true),true));
}
//图片缩略图
publicfunctionSmallimg($x=100,$y=100){
$imgAtt=getimagesize($this->path);
//图像宽,高,类型
$imgWidth=$imgAtt[0];
$imgHeight=$imgAtt[1];
$imgext=$imgAtt[2];
//等比列缩放
if(($x/$imgWidth)>($y/$imgHeight)){
$bl=$y/$imgHeight;
}else{
$bl=$x/$imgWidth;
}
$x=floor($imgWidth*$bl);//缩放后
$y=floor($imgHeight*$bl);
$images=imagecreatetruecolor($x,$y);
$big=imagecreatefromjpeg($this->path);
imagecopyresized($images,$big,0,0,0,0,$x,$y,$imgWidth,$imgWidth);
switch($imgext){
case1:
$imageout=imagecreatefromgif($this->path);
break;
case2:
$imageout=imagecreatefromjpeg($this->path);
break;
case3:
$imageout=imagecreatefromgif($this->path);
break;
}
$im=imagejpeg($images,$this->path);
}
//文件双传
publicfunctionuploads()
{
if($this->UpError()&&$this->UpMine()&&$this->UpExt()&&$this->UpSize()&&$this->UpPost()){
$this->UpPath();
$names=$this->Upname();
$this->path=$this->filepath.'/'.$names.'.'.$this->ext;
if(move_uploaded_file($this->fileinfo['tmp_name'],$this->path)){
return$this->path;
}else{
$this->fileerror="上传失败";
}
}else{
exit("<b>".$this->fileerror."</b>");
}
}
}
<?php
header("content-type:imagejpeg");
header("Content-type:text/html;charset=utf-8");
require'list.php';
$u=newupload();
$a=$u->uploads();
$c=$u->Smallimg();
echo"<imgsrc={$a}/>";
echo"<imgsrc={$c}/>";
?>
<!DOCTYPEhtml>
<html>
<head>
<metacharset="utf-8">
<metahttp-equiv="X-UA-Compatible"content="IE=edge,chrome=1">
<title>Examples</title>
<metaname="description"content="">
<metaname="keywords"content="">
<linkhref=""rel="stylesheet">
</head>
<body>
<formaction="ce.php"enctype="multipart/form-data"method="post">
<inputtype="text"name="name"/><inputtype="file"name="file"/>
<inputtype="submit"name='submit'value="提交">
</form>
</body>
</html>
以上所述是小编给大家介绍的PHP上传图片类显示缩略图功能的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!