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 相关文章推荐
网络资源
Oct 09 PHP
关于Zend Studio 配色方案插件的介绍
Jun 24 PHP
PHP中strlen()和mb_strlen()的区别浅析
Jun 19 PHP
PHP分页初探 一个最简单的PHP分页代码的简单实现
Jun 21 PHP
php is_writable判断文件是否可写实例代码
Oct 13 PHP
Linux下快速搭建php开发环境
Mar 13 PHP
基于ThinkPHP实现的日历功能实例详解
Apr 15 PHP
PHP 实现人民币小写转换成大写的方法及大小写转换函数
Nov 17 PHP
关于 Laravel Redis 多个进程同时取队列问题详解
Dec 25 PHP
PHP设计模式之简单工厂和工厂模式实例分析
Mar 25 PHP
ThinkPHP5.0框架使用build 自动生成模块操作示例
Apr 11 PHP
ThinkPHP5&amp;5.1框架关联模型分页操作示例
Aug 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程序61条面向对象分析设计的经验小结
2008/11/12 PHP
解析php mysql 事务处理回滚操作(附实例)
2013/08/05 PHP
使用PHP强制下载PDF文件示例
2014/01/17 PHP
详解php用curl调用接口方法,get和post两种方式
2017/01/13 PHP
php中的单引号、双引号和转义字符详解
2017/02/16 PHP
phpQuery采集网页实现代码实例
2020/04/02 PHP
javascript 设置某DIV区域内的checkbox复选框
2009/11/30 Javascript
js 实现打印网页中定义的部分内容的代码
2010/04/01 Javascript
javascript中使用css需要注意的地方小结
2010/09/01 Javascript
js中eval()函数和trim()去掉字符串左右空格应用
2013/02/02 Javascript
js去字符串前后空格5种实现方法及比较
2013/04/03 Javascript
JS实现可改变列宽的table实例
2013/07/02 Javascript
js实现日历可获得指定日期周数及星期几示例分享(js获取星期几)
2014/03/14 Javascript
jQuery中element选择器用法实例
2014/12/29 Javascript
谈谈js中的prototype及prototype属性解释和常用方法
2015/11/25 Javascript
jQuery+canvas实现简单的球体斜抛及颜色动态变换效果
2016/01/28 Javascript
实践中学习AngularJS表单
2016/03/21 Javascript
微信小程序 教程之WXSS
2016/10/18 Javascript
vue router路由嵌套不显示问题的解决方法
2017/06/17 Javascript
vue实现搜索过滤效果
2019/05/28 Javascript
vue-cli3访问public文件夹静态资源报错的解决方式
2020/09/02 Javascript
openlayers实现地图测距测面
2020/09/25 Javascript
[13:16]INFAMOUS vs VGJ T BO3
2018/06/07 DOTA
python实现socket客户端和服务端简单示例
2014/02/24 Python
Python常用随机数与随机字符串方法实例
2015/04/09 Python
编写Python脚本批量下载DesktopNexus壁纸的教程
2015/05/06 Python
Python3 ID3决策树判断申请贷款是否成功的实现代码
2020/05/21 Python
python程序实现BTC(比特币)挖矿的完整代码
2021/01/20 Python
HTML5 body设置自适应全屏
2020/05/07 HTML / CSS
俄罗斯美容和健康网上商店:Созвездие Красоты
2019/07/23 全球购物
欧洲最大的预定车位市场:JustPark
2020/01/06 全球购物
生物化学研究助理员求职信
2013/10/09 职场文书
升旗仪式主持词
2014/03/19 职场文书
2019西餐厅创业计划书范文!
2019/07/12 职场文书
用python画城市轮播地图
2021/05/28 Python
AndroidStudio图片压缩工具ImgCompressPlugin使用实例
2022/08/05 Java/Android