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:风雨欲来 路在何方?
Oct 09 PHP
PHP COOKIE设置为浏览器进程
Jun 21 PHP
关于UEditor编辑器远程图片上传失败的解决办法
Aug 31 PHP
PHP引用(&amp;)各种使用方法实例详解
Mar 20 PHP
PHP实现图片压缩的两则实例
Jul 19 PHP
PHP对文件进行加锁、解锁实例
Jan 23 PHP
php准确获取文件MIME类型的方法
Jun 17 PHP
在WordPress中使用PHP脚本来判断访客来自什么国家
Dec 10 PHP
php图片上传类 附调用方法
May 15 PHP
关于php unset对json_encode的影响详解
Nov 14 PHP
ThinkPHP3.2.3框架邮件发送功能图文实例详解
Apr 23 PHP
PHP实现创建一个RPC服务操作示例
Feb 23 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
一个程序下载的管理程序(四)
2006/10/09 PHP
PHP中使用foreach和引用导致程序BUG的问题介绍
2012/09/05 PHP
php根据isbn书号查询amazon网站上的图书信息的示例
2014/02/13 PHP
php is_writable判断文件是否可写实例代码
2016/10/13 PHP
php实现二叉树中和为某一值的路径方法
2018/10/14 PHP
在 Laravel 项目中使用 webpack-encore的方法
2019/07/21 PHP
JQuery开发的数独游戏代码
2010/10/29 Javascript
jQuery对表单元素的取值和赋值操作代码
2011/05/19 Javascript
关于eval 与new Function 到底该选哪个?
2013/04/17 Javascript
ExtJS4 动态生成的grid导出为excel示例
2014/05/02 Javascript
用html+css+js实现的一个简单的图片切换特效
2014/05/28 Javascript
JSON格式化输出
2014/11/10 Javascript
javascript实现密码强度显示
2015/03/18 Javascript
两种简单的跨域方法(jsonp、php)
2017/01/02 Javascript
微信小程序 两种滑动方式(横向滑动,竖向滑动)详细及实例代码
2017/01/13 Javascript
angular+bootstrap的双向数据绑定实例
2017/03/03 Javascript
vue vuex vue-rouert后台项目——权限路由(适合初学)
2017/12/29 Javascript
vue中如何实现后台管理系统的权限控制的方法示例
2018/09/19 Javascript
微信小程序实现Session功能及无法获取session问题的解决方法
2019/05/07 Javascript
JSON stringify方法原理及实例解析
2020/10/23 Javascript
[01:33]DOTA2上海特级锦标赛 LIQUID战队完整宣传片
2016/03/16 DOTA
[44:40]Spirit vs Navi Supermajor小组赛 A组败者组第一轮 BO3 第一场 6.2
2018/06/03 DOTA
Python中的defaultdict模块和namedtuple模块的简单入门指南
2015/04/01 Python
python删除列表内容
2015/08/04 Python
Python实现的栈、队列、文件目录遍历操作示例
2019/05/06 Python
让IE下支持Html5的placeholder属性的插件
2014/09/02 HTML / CSS
物业管理求职自荐信
2013/09/25 职场文书
和平主题的演讲稿
2014/01/12 职场文书
标准离婚协议书范文下载
2014/11/30 职场文书
2014年城市管理工作总结
2014/12/02 职场文书
工人先锋号事迹材料
2014/12/24 职场文书
先进班组材料范文
2014/12/25 职场文书
家长对孩子的寄语
2015/02/26 职场文书
工作经历证明范本
2015/06/15 职场文书
2016年安全月活动总结
2016/04/06 职场文书
解决MySQL报“too many connections“错误
2022/04/19 MySQL