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 相关文章推荐
PHP学习之数组的定义和填充
Apr 17 PHP
ThinkPHP采用GET方式获取中文参数查询无结果的解决方法
Jun 26 PHP
PHP魔术引号所带来的安全问题分析
Jul 15 PHP
ThinkPHP开发框架函数详解:C方法
Aug 14 PHP
PHP中两个float(浮点数)比较实例分析
Sep 27 PHP
php 实现进制相互转换
Apr 07 PHP
php使用curl详细解析及问题汇总
Aug 11 PHP
PHP 接入支付宝即时到账功能
Sep 18 PHP
php使用CURL模拟GET与POST向微信接口提交及获取数据的方法
Sep 23 PHP
PHP查询大量数据内存耗尽问题的解决方法
Oct 28 PHP
PHP PDOStatement::errorInfo讲解
Jan 31 PHP
PHP pthreads v3下worker和pool的使用方法示例
Feb 21 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
分享一个超好用的php header下载函数
2014/01/31 PHP
js小技巧--自动隐藏红叉叉
2007/08/13 Javascript
JavaScript的Cookies
2008/01/16 Javascript
S2SH整合JQuery+Ajax实现登录验证功能实现代码
2013/01/30 Javascript
JS实现动态移动层及拖动浮层关闭的方法
2015/04/30 Javascript
jQuery的end()方法使用详解
2015/07/15 Javascript
JavaScript数据类型学习笔记分享
2016/09/01 Javascript
JavaScript与ActionScript3两者的同性与差异性
2016/09/22 Javascript
Bootstrap modal 多弹窗之叠加引起的滚动条遮罩阴影问题
2017/02/27 Javascript
在react中使用vuex的示例代码
2018/07/30 Javascript
解决vue打包css文件中背景图片的路径问题
2018/09/03 Javascript
详解VUE调用本地json的使用方法
2019/05/15 Javascript
浅谈layui数据表格判断问题(加入表单元素),设置单元格样式
2019/10/26 Javascript
超详细小程序定位地图模块全系列开发教学
2020/11/24 Javascript
[06:24]DOTA2 2015国际邀请赛中国区预选赛第二日TOP10
2015/05/27 DOTA
Python发送Email方法实例
2014/08/21 Python
用Python中的__slots__缓存资源以节省内存开销的方法
2015/04/02 Python
python 调用win32pai 操作cmd的方法
2017/05/28 Python
Python中shapefile转换geojson的示例
2019/01/03 Python
在Python中预先初始化列表内容和长度的实现
2019/11/28 Python
python数据分析:关键字提取方式
2020/02/24 Python
使用phonegap查找联系人的实现方法
2017/03/31 HTML / CSS
ZINVO手表官网:男士和女士手表
2019/03/10 全球购物
如何用Java判断一个文件或目录是否存在
2012/11/19 面试题
历史学专业个人的自我评价
2013/10/13 职场文书
二年级班级文化建设方案
2014/05/10 职场文书
设计专业自荐信
2014/06/19 职场文书
2014年教师党员自我评价范文
2014/09/22 职场文书
乡镇领导班子四风对照检查材料
2014/09/27 职场文书
2014年信访工作总结
2014/11/17 职场文书
简单的个人租房协议书范本
2014/11/26 职场文书
佛光寺导游词
2015/02/10 职场文书
史上最全的军训拉歌口号
2015/12/25 职场文书
nginx 多个location转发任意请求或访问静态资源文件的实现
2021/03/31 Servers
如何用Python搭建gRPC服务
2021/06/30 Python
Pygame游戏开发之太空射击实战敌人精灵篇
2022/08/05 Python