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 相关文章推荐
动态网站web开发 PHP、ASP还是ASP.NET
Oct 09 PHP
PHP控制网页过期时间的代码
Sep 28 PHP
一些使用频率比较高的php函数
Oct 03 PHP
php利用cookie实现访问次数统计代码
May 19 PHP
php列出一个目录下的所有文件的代码
Oct 09 PHP
深入解析PHP中逗号与点号的区别
Aug 05 PHP
理解PHP中的Session及对Session有效期的控制
Jan 08 PHP
PHP类的声明与实例化及构造方法与析构方法详解
Jan 26 PHP
微信支付扫码支付php版
Jul 22 PHP
laravel中的错误与日志用法详解
Jul 26 PHP
php+Memcached实现简单留言板功能示例
Feb 15 PHP
PHP7 错误处理机制修改
Mar 09 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
递归删除一个节点以及该节点下的所有节点示例
2014/03/19 PHP
php基本函数汇总
2015/07/09 PHP
php注册和登录界面的实现案例(推荐)
2016/10/24 PHP
centos 7.2下搭建LNMP环境教程
2016/11/20 PHP
PHP使用curl_multi实现并发请求的方法示例
2018/04/29 PHP
jquery 鼠标滑动显示详情应用示例
2014/01/24 Javascript
javascript 处理null及null值示例
2014/06/09 Javascript
推荐9款炫酷的基于jquery的页面特效
2014/12/07 Javascript
javascript制作的滑动图片菜单
2015/05/15 Javascript
详解参数传递四种形式
2015/07/21 Javascript
Ajax清除浏览器js、css、图片缓存的方法
2015/08/06 Javascript
javascript封装 Cookie 应用接口
2015/08/07 Javascript
JavaScript仿网易选项卡制作代码
2016/10/06 Javascript
微信开发之微信jssdk录音功能开发示例
2018/10/22 Javascript
js获取form表单中name属性的值
2019/02/27 Javascript
Angular6使用forRoot() 注册单一实例服务问题
2019/08/27 Javascript
微信小程序纯文本实现@功能
2020/04/08 Javascript
jQuery实现移动端下拉展现新的内容回弹动画
2020/06/24 jQuery
解决vue 使用setTimeout,离开当前路由setTimeout未销毁的问题
2020/07/21 Javascript
基于原生JS封装的Modal对话框插件的示例代码
2020/09/09 Javascript
用python与文件进行交互的方法
2018/03/01 Python
JavaScript实现一维数组转化为二维数组
2018/04/17 Python
Python内存映射文件读写方式
2020/04/24 Python
如何写python的配置文件
2020/06/07 Python
什么是smarty? Smarty的优点是什么?
2013/08/11 面试题
儿科护士自我鉴定
2013/10/14 职场文书
证券期货行业个人的自我评价
2013/12/26 职场文书
大学生优秀自荐信范文
2014/02/25 职场文书
2014乡镇班子个人对照检查材料思想汇报
2014/09/26 职场文书
工作失职造成投诉的检讨书范文
2014/10/05 职场文书
毕业设计指导教师评语
2014/12/30 职场文书
销售经理岗位职责范本
2015/04/02 职场文书
师范生小学见习总结
2015/06/23 职场文书
理想国读书笔记
2015/06/25 职场文书
企业年会祝酒词
2015/08/11 职场文书
《悲惨世界》:比天空更广阔的是人的心灵
2020/01/16 职场文书