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 相关文章推荐
第八节 访问方式 [8]
Oct 09 PHP
常用的PHP数据库操作方法(MYSQL版)
Jun 08 PHP
PHP 数组和字符串互相转换实现方法
Mar 26 PHP
让codeigniter与swfupload整合的最佳解决方案
Jun 12 PHP
php 5.6版本中编写一个PHP扩展的简单示例
Jan 20 PHP
PHP中使用hidef扩展代替define提高性能
Apr 09 PHP
php获取指定(访客)IP所有信息(地址、邮政编码、国家、经纬度等)的方法
Jul 06 PHP
深入解析WordPress中加载模板的get_template_part函数
Jan 11 PHP
PHP中的密码加密的解决方案总结
Oct 26 PHP
PHP基于自定义类随机生成姓名的方法示例
Aug 05 PHP
php获取是星期几的的一些常用姿势
Dec 15 PHP
PHP 实现base64编码文件上传出现问题详解
Sep 01 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
Discuz板块横排显示图片的实现方法
2007/05/28 PHP
用mysql_fetch_array()获取当前行数据的方法详解
2013/06/05 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(八)
2014/06/23 PHP
朋友网关于QQ相关的PHP代码(研究QQ的绝佳资料)
2015/01/26 PHP
phpMyAdmin安装并配置允许空密码登录
2015/07/04 PHP
jQuery中add实现同时选择两个id对象
2010/10/22 Javascript
使用Nodejs开发微信公众号后台服务实例
2014/09/03 NodeJs
React组件的三种写法总结
2017/01/12 Javascript
vue better scroll 无法滚动的解决方法
2018/06/07 Javascript
angularJs select绑定的model取不到值的解决方法
2018/10/08 Javascript
vue 使用饿了么UI仿写teambition的筛选功能
2021/03/01 Vue.js
[48:56]2018DOTA2亚洲邀请赛 3.31 小组赛 A组 VG vs KG
2018/03/31 DOTA
[59:35]DOTA2-DPC中国联赛定级赛 Aster vs DLG BO3第一场 1月8日
2021/03/11 DOTA
python简单图片操作:打开\显示\保存图像方法介绍
2017/11/23 Python
FFrpc python客户端lib使用解析
2019/08/24 Python
浅析python redis的连接及相关操作
2019/11/07 Python
pytorch 彩色图像转灰度图像实例
2020/01/13 Python
python3光学字符识别模块tesserocr与pytesseract的使用详解
2020/02/26 Python
python实现简单的购物程序代码实例
2020/03/03 Python
英国最大的笔记本电脑直销专家:Laptops Direct
2019/07/20 全球购物
JAVA和C++区别都有哪些
2015/03/30 面试题
求职推荐信
2013/10/28 职场文书
八年级数学教学反思
2014/01/31 职场文书
业务总经理岗位职责
2014/02/03 职场文书
个人借款担保书
2014/04/02 职场文书
红色故事演讲稿
2014/05/22 职场文书
旷课检讨书范文
2014/10/30 职场文书
自荐信模板大全
2015/03/27 职场文书
一文带你理解vue创建一个后台管理系统流程(Vue+Element)
2021/05/18 Vue.js
mysql中varchar类型的日期进行比较、排序等操作的实现
2021/11/17 MySQL
MySQL 中如何归档数据的实现方法
2022/03/16 SQL Server
Spring Security使用单点登录的权限功能
2022/04/03 Java/Android
聊聊CSS粘性定位sticky案例解析
2022/06/01 HTML / CSS
java实现自定义时钟并实现走时功能
2022/06/21 Java/Android
类和原型的设计模式之复制与委托差异
2022/07/07 Javascript