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 pack与unpack 摸板字符字符含义
Oct 29 PHP
PHP chmod 函数与批量修改文件目录权限
May 10 PHP
php判断文件上传类型及过滤不安全数据的方法
Dec 17 PHP
PHP中使用array函数新建一个数组
Nov 19 PHP
PHP7正式版测试,性能惊艳!
Dec 08 PHP
yii2分页之实现跳转到具体某页的实例代码
Jun 02 PHP
PHP多维数组元素操作类的方法
Nov 14 PHP
php字符集转换
Jan 23 PHP
php实现XML和数组的相互转化功能示例
Feb 08 PHP
php中上传文件的的解决方案
Sep 25 PHP
php微信公众号开发之微信企业付款给个人
Oct 04 PHP
php实现数组中出现次数超过一半的数字的统计方法
Oct 14 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 获取页面中指定内容的实现类
2014/01/23 PHP
详解yii2使用多个数据库的案例
2017/06/16 PHP
Laravel统一错误处理为JSON的方法介绍
2020/10/18 PHP
javascript 数组的方法集合
2008/06/05 Javascript
javascript中callee与caller的用法和应用场景
2010/12/08 Javascript
JqGrid web打印实现代码
2011/05/31 Javascript
Js 时间间隔计算的函数(间隔天数)
2011/11/15 Javascript
简约JS日历控件 实例代码
2013/07/12 Javascript
Agularjs妙用双向数据绑定实现手风琴效果
2017/05/26 Javascript
JavaScript实现body内任意节点的自定义属性功能示例
2017/09/18 Javascript
Nodejs调用WebService的示例代码
2017/09/29 NodeJs
详解vue 组件之间使用eventbus传值
2017/10/25 Javascript
vue按需加载组件webpack require.ensure的方法
2017/12/13 Javascript
vue.js中引入vuex储存接口数据及调用的详细流程
2017/12/14 Javascript
nodejs中密码加密处理操作详解
2018/03/20 NodeJs
Vue 让元素抖动/摆动起来的实现代码
2018/05/31 Javascript
对angular 监控数据模型变化的事件方法$watch详解
2018/10/09 Javascript
用node开发并发布一个cli工具的方法步骤
2019/01/03 Javascript
Vue组件模板的几种书写形式(3种)
2020/02/19 Javascript
js实现轮播图特效
2020/05/28 Javascript
记录一次websocket封装的过程
2020/11/23 Javascript
Python使用 Beanstalkd 做异步任务处理的方法
2018/04/24 Python
python3实现windows下同名进程监控
2018/06/21 Python
解决Djang2.0.1中的reverse导入失败的问题
2019/08/16 Python
Python使用Turtle库绘制一棵西兰花
2019/11/23 Python
使用jupyter notebook运行python和R的步骤
2020/08/13 Python
Python读写Excel表格的方法
2021/03/02 Python
CSS3中:nth-child和:nth-of-type的区别深入理解
2014/03/10 HTML / CSS
使用CSS媒体查询(Media Queries)和JavaScript判断浏览器设备类型的方法
2014/04/03 HTML / CSS
利用纯CSS3实现tab选项卡切换示例代码
2016/09/21 HTML / CSS
CSS3贝塞尔曲线示例:创建链接悬停动画效果
2020/11/19 HTML / CSS
学前教育专业毕业生自荐信
2013/10/03 职场文书
行政内勤岗位职责
2014/04/07 职场文书
孝老爱亲模范事迹材料
2014/05/25 职场文书
原生JS实现飞机大战小游戏
2021/06/09 Javascript
教你如何使用Python实现二叉树结构及三种遍历
2021/06/18 Python