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 相关文章推荐
php5 pdo新改动加载注意事项
Sep 11 PHP
php入门教程 精简版
Dec 13 PHP
PHP spl_autoload_register实现自动加载研究
Dec 06 PHP
PDO版本问题 Invalid parameter number: no parameters were bound
Jan 06 PHP
深入分析使用mysql_fetch_object()以对象的形式返回查询结果
Jun 05 PHP
PHP 通过Socket收发十六进制数据的实现代码
Aug 16 PHP
PHP防范SQL注入的具体方法详解(测试通过)
May 09 PHP
php实现的SESSION类
Dec 02 PHP
IIS 7.5 asp Session超时时间设置方法
Apr 17 PHP
laravel 5.4中实现无限级分类的方法示例
Jul 27 PHP
php变量与字符串的增删改查操作示例
May 07 PHP
PHP xpath提取网页数据内容代码解析
Jul 16 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/10/09 PHP
mantis安装、配置和使用中的问题小结
2014/07/14 PHP
PHP获取一段文本显示点阵宽度和高度的方法
2015/03/12 PHP
PHP实现根据密码长度显示安全条
2017/07/04 PHP
PHP递归算法的简单实例
2019/02/28 PHP
收集的10个免费的jQuery相册
2011/02/26 Javascript
javascript获取鼠标位置部分的实例代码(兼容IE,FF)
2013/08/05 Javascript
JavaScript异步编程:异步数据收集的具体方法
2013/08/19 Javascript
div浮层,滚动条移动,位置保持不变的4种方法汇总
2013/12/11 Javascript
用js代码改变单选框选中状态的简单实例
2013/12/18 Javascript
JavaScript获取浏览器信息的方法
2015/11/20 Javascript
jQuery同步提交示例代码
2015/12/12 Javascript
jQuery实现下拉框功能实例代码
2016/05/06 Javascript
从重置input file标签中看jQuery的 .val() 和 .attr(“value”) 区别
2016/06/12 Javascript
AngularJs基本特性解析(一)
2016/07/21 Javascript
vue中当图片地址无效的时候,显示默认图片的方法
2018/09/18 Javascript
说说如何在Vue.js中实现数字输入组件的方法
2019/01/08 Javascript
解决Vue中使用keepAlive不缓存问题
2020/08/04 Javascript
jQuery实现tab栏切换效果
2020/12/22 jQuery
python爬虫headers设置后无效的解决方法
2017/10/21 Python
Python中import机制详解
2017/11/14 Python
使用PyCharm创建Django项目及基本配置详解
2018/10/24 Python
一百行python代码将图片转成字符画
2021/02/19 Python
python 常见字符串与函数的用法详解
2018/11/23 Python
Python中模块(Module)和包(Package)的区别详解
2019/08/07 Python
python 批量下载bilibili视频的gui程序
2020/11/20 Python
用Python自动清理电脑内重复文件,只要10行代码(自动脚本)
2021/01/09 Python
canvas 下载二维码和图片加水印的方法
2018/03/21 HTML / CSS
为智能设备设计个性化保护套网站:caseable
2017/01/05 全球购物
服装设计专业毕业生推荐信
2013/11/09 职场文书
物控部经理职务说明书
2014/02/25 职场文书
天地会口号
2014/06/17 职场文书
党的生日演讲稿
2014/09/10 职场文书
2014年教师节座谈会发言稿
2014/09/10 职场文书
MySQL主从复制断开的常用修复方法
2021/04/07 MySQL
5人制售《绝地求生》游戏外挂获利500多万元 被判刑
2022/03/31 其他游戏