PHP上传图片类显示缩略图功能


Posted in PHP onJune 30, 2016

有缩略图功能 但是 感觉不全面,而且有点问题,继续学习,将来以后修改下

<form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post" ><input type="text" name="name" /><input type="file" name="file" /><input type="submit" name='submit' value="提交" ></form>
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/6/28
* Time: 21:04
*/
class upload{
protected $fileMine;//文件上传类型
protected $filepath;//文件上传路径
protected $filemax;//文件上传大小
protected $fileExt;//文件上传格式
protected $filename;//文件名
protected $fileerror;//文件出错设置
protected $fileflag;//文件检测
protected $fileinfo; //FILES
protected $ext; //文件扩展
protected $path;
//文件上传
public function __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);
}
//错误判断
public function UpError(){
if($this->fileinfo['error']>0){
switch($this->fileinfo['error'])
{
case 1:
$this->fileerror="上传文件大小超过服务器允许上传的最大值,php.ini中设置upload_max_filesize选项限制的值 ";
break;
case 2:
$this->fileerror="上传文件大小超过HTML表单中隐藏域MAX_FILE_SIZE选项指定的值";
break;
case 3:
$this->fileerror="文件部分被上传";
break;
case 4:
$this->fileerror="没有选择上传文件";
break;
case 5:
$this->fileerror="未找到临时目录";
break;
case 6:
$this->fileerror="文件写入失败";
break;
case 7:
$this->fileerror="php文件上传扩展没有打开 ";
break;
case 8:
$this->fileerror="";
break;
}
return false;
}
return true;
}
//检测文件类型
public function UpMine(){
if(!in_array($this->fileinfo['type'],$this->fileMine)) {
$this->error="文件上传类型不对";
return false;
}
return true;
}
//检测文件格式
public function UpExt(){
$this->ext=pathinfo($this->fileinfo['name'],PATHINFO_EXTENSION);
//var_dump($ext);
if(!in_array($this->ext,$this->fileExt)){
$this->fileerror="文件格式不对";
return false;
}
return true;
}
//检测文件路径
public function UpPath(){
if(!file_exists($this->filepath)){
mkdir($this->filepath,0777,true);
}
}
//检测文件大小
public function UpSize(){
$max=$this->fileinfo['size'];
if($max>$this->filemax){
$this->fileerror="文件过大";
return false;
}
return true;
}
//检测文件是否HTTP
public function UpPost(){
if(!is_uploaded_file($this->fileinfo['tmp_name'])){
$this->fileerror="恶意上偿还";
return false;
}
return true;
}
//文件名防止重复
public function Upname(){
return md5(uniqid(microtime(true),true));
}
//图片缩略图
public function Smallimg($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){
case 1:
$imageout=imagecreatefromgif($this->path);
break;
case 2:
$imageout=imagecreatefromjpeg($this->path);
break;
case 3:
$imageout=imagecreatefromgif($this->path);
break;
}
$im=imagejpeg($images,$this->path);
}
//文件双传
public function uploads()
{
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=new upload();
$a=$u->uploads();
$c=$u->Smallimg();
echo "<img src={$a} />";
echo "<img src={$c} />";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<body>
<form action="ce.php" enctype="multipart/form-data" method="post" >
<input type="text" name="name" /><input type="file" name="file" />
<input type="submit" name='submit' value="提交" >
</form>
</body>
</html>

以上所述是小编给大家介绍的PHP上传图片类显示缩略图功能的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

PHP 相关文章推荐
打造计数器DIY三步曲(下)
Oct 09 PHP
php mssql 数据库分页SQL语句
Dec 16 PHP
php db类库进行数据库操作
Mar 19 PHP
支持中文的php加密解密类代码
Nov 27 PHP
基于PHP文件操作的详解
Jun 05 PHP
php数组(array)输出的三种形式详解
Jun 05 PHP
一个PHP实现的轻量级简单爬虫
Jul 08 PHP
PHP通过反射动态加载第三方类和获得类源码的实例
Nov 27 PHP
PHP实现二维数组去重功能示例
Jan 12 PHP
php中请求url的五种方法总结
Jul 13 PHP
PHP基础之输出缓冲区基本概念、原理分析
Jun 19 PHP
Yii 框架使用数据库(databases)的方法示例
May 19 PHP
PHP使用php-resque库配合Redis实现MQ消息队列的教程
Jun 29 #PHP
Thinkphp批量更新数据的方法汇总
Jun 29 #PHP
ThinkPHP实现更新数据实例详解(demo)
Jun 29 #PHP
php结合mysql与mysqli扩展处理事务的方法
Jun 29 #PHP
php简单解析mysqli查询结果的方法(2种方法)
Jun 29 #PHP
php mysqli查询语句返回值类型实例分析
Jun 29 #PHP
thinkphp框架实现数据添加和显示功能
Jun 29 #PHP
You might like
模拟xcopy的函数
2006/10/09 PHP
用sql命令修改数据表中的一个字段为非空(not null)的语句
2010/06/04 PHP
解决nginx不支持thinkphp中pathinfo的问题
2015/07/21 PHP
js 链式延迟执行DOME
2012/01/04 Javascript
Prototype源码浅析 String部分(三)之HTML字符串处理
2012/01/15 Javascript
ajax异步刷新实现更新数据库
2012/12/03 Javascript
jq实现酷炫的鼠标经过图片翻滚效果
2014/03/12 Javascript
Bootstrap组合上、下拉框简单实现代码
2017/03/06 Javascript
基于vue的短信验证码倒计时demo
2017/09/13 Javascript
vue综合组件间的通信详解
2017/11/06 Javascript
bootstrap select下拉搜索插件使用方法详解
2017/11/23 Javascript
Nodejs使用Mongodb存储与提供后端CRD服务详解
2018/09/04 NodeJs
关于IDEA中的.VUE文件报错 Export declarations are not supported by current JavaScript version
2020/10/17 Javascript
决策树的python实现方法
2014/11/18 Python
Python实现telnet服务器的方法
2015/07/10 Python
实例讲解Python的函数闭包使用中应注意的问题
2016/06/20 Python
pyhton列表转换为数组的实例
2018/04/04 Python
Python3使用turtle绘制超立方体图形示例
2018/06/19 Python
示例详解Python3 or Python2 两者之间的差异
2018/08/23 Python
浅谈Django中view对数据库的调用方法
2019/07/18 Python
python之列表推导式的用法
2019/11/29 Python
浅谈keras中Dropout在预测过程中是否仍要起作用
2020/07/09 Python
python中二分查找法的实现方法
2020/12/06 Python
matplotlib实现数据实时刷新的示例代码
2021/01/05 Python
CSS3实现多重边框的方法总结
2016/05/31 HTML / CSS
html5使用canvas画一条线
2014/12/15 HTML / CSS
美国电视购物HSN官网:HSN
2016/09/07 全球购物
如何保障Web服务器安全
2014/05/05 面试题
JAVA招聘远程笔试题
2015/07/23 面试题
应届生财务管理求职信
2013/11/06 职场文书
小学兴趣小组活动总结
2014/07/07 职场文书
高三复习计划
2015/01/19 职场文书
2015年老干部工作总结
2015/04/23 职场文书
房贷收入证明范本
2015/06/12 职场文书
100句拼搏进取的名言警句,值得一读!
2019/10/07 职场文书
Java Optional<Foo>转换成List<Bar>的实例方法
2021/06/20 Java/Android