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 foreach循环使用详解与实例代码
May 08 PHP
php 模拟GMAIL,HOTMAIL(MSN),YAHOO,163,126邮箱登录的详细介绍
Jun 18 PHP
PHP无限分类(树形类)
Sep 28 PHP
php实现基于微信公众平台开发SDK(demo)扩展的方法
Dec 22 PHP
PHP中捕获超时事件的方法实例
Feb 12 PHP
WordPress中用于获取文章作者与分类信息的方法整理
Dec 17 PHP
PHP处理二进制数据的实现方法
Jun 13 PHP
基于ThinkPHP5.0实现图片上传插件
Sep 25 PHP
php使用imagecopymerge()函数创建半透明水印
Jan 25 PHP
在PHP中输出JS语句以及乱码问题的解决方案
Feb 13 PHP
PHP设计模式之单例模式定义与用法分析
Mar 26 PHP
Referer原理与图片防盗链实现方法详解
Jul 03 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的知识
2006/11/17 PHP
php 短链接算法收集与分析
2011/12/30 PHP
巧用php中的array_filter()函数去掉多维空值的代码分享
2012/09/07 PHP
php抓取并保存网站图片的实现代码
2015/10/28 PHP
php通过文件头判断格式的方法
2016/05/28 PHP
详解PHP如何更好的利用PHPstorm的自动提示
2017/08/18 PHP
php PDO属性设置与操作方法分析
2018/12/27 PHP
取键盘键位ASCII码的网页
2007/07/30 Javascript
bootstrap-treeview自定义双击事件实现方法
2016/01/09 Javascript
Javascript HTML5 Canvas实现的一个画板
2020/04/12 Javascript
判断是否存在子节点的实现代码
2016/05/18 Javascript
bootstrapValidator.min.js表单验证插件
2017/02/09 Javascript
js实现前面自动补全位数的方法
2018/10/10 Javascript
基于mpvue搭建微信小程序项目框架的教程详解
2019/04/10 Javascript
[03:20]次级联赛厮杀超职业 现超级兵对拆世纪大战
2014/10/30 DOTA
[01:01:24]DOTA2上海特级锦标赛A组败者赛 EHOME VS CDEC第三局
2016/02/25 DOTA
Python 正则表达式操作指南
2009/05/04 Python
在Python中使用判断语句和循环的教程
2015/04/25 Python
在Python中操作字典之fromkeys()方法的使用
2015/05/21 Python
python删除过期文件的方法
2015/05/29 Python
Python实现购物车购物小程序
2018/04/18 Python
python flask web服务实现更换默认端口和IP的方法
2019/07/26 Python
Python TCPServer 多线程多客户端通信的实现
2019/12/31 Python
Python selenium自动化测试模型图解
2020/04/15 Python
通过cmd进入python的步骤
2020/06/16 Python
CSS3弹性盒模型开发笔记(二)
2016/04/26 HTML / CSS
Superdry瑞典官网:英国日本街头风品牌
2017/05/17 全球购物
英国综合网上购物商城:The Hut
2018/07/03 全球购物
大学生护理专业自荐信
2013/10/03 职场文书
旅游文化节策划方案
2014/06/06 职场文书
2014年党员自我评议对照检查材料
2014/09/20 职场文书
中学生学习保证书
2015/02/26 职场文书
团委工作总结2015
2015/04/02 职场文书
python用tkinter开发的扫雷游戏
2021/06/01 Python
OpenCV-Python使用cv2实现傅里叶变换
2021/06/09 Python
蓝牙耳机怎么连接电脑win11? Win11蓝牙耳机连接电脑的技巧
2023/01/09 数码科技