您的位置:首页 > 编程语言 > PHP开发

php将图片压缩成缩略图(大,中,小)上传到数据库中

2017-10-15 17:03 288 查看
1.思路

先用单文件上传将图片压缩成三个存放在本地,再用多文件上传,上传到数据库中

代码:(这里的配置文件就不写了前面有);

单文件上传压缩到本地:

//封装一个上传单个文件的方法
/*
*@prame string key
*@prame string path
*@prame String maxSize
*@prame array  allowMime
*@prame array allowFiletype
*@prame bool true
*
*auther wulei
*/
include 'suolue.php';
function upload($key,$path,$maxSzie,$imageSize,$allowMime,$allowFiletype,$isfileName = true){
//判断错误信息
if($_FILES[$key]['error']){
switch($_FILES[$key]['error']){
case 1:
$str = "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。";
break;
case 2:
$str = "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值.";
case 3:
$str = ",文件只有部分被上传。 ";
break;
case 4:
$str = "没有文件被上传。";
break;
case 6:
$str = "找不到临时文件夹";
break;
case 7:
$str = "文件写入失败。";
break;
}
return [0,$str];
}
//判读大小
if($_FILES[$key]['size']>$maxSzie){
return [0,'超过最大文件大小'];
}
//判断mime类型
if(!in_array($_FILES[$key]['type'],$allowMime)){
return [0,'没有符合的mime类型'];
}
//判断文件的后缀
$info = pathinfo($_FILES[$key]['name']);
var_dump($info);
$sub = $info['extension'];
if(!in_array($sub,$allowFiletype)){
return [0,'没有符合的文件名后缀'];
}
//文件名是否为随机
if($isfileName){
$name = uniqid().'.'.$sub;
}else{
$name = $_FILES[$key]['name'];
}
//拼接路径
$path = rtrim($path,'/').'/'.date('Y/m/d').'/';

//判断不存在就创建文件
if(!file_exists($path)){
mkdir($path,0777,true);
}
//判断文件是否上传成功
if(is_uploaded_file($_FILES[$key]['tmp_name'])){
if(move_uploaded_file($_FILES[$key]['tmp_name'],$path.$name)){
header("refresh:3;url=http://localhost/day24/3/loader.html");
}else{
return [0,'上传失败'];
}
}else{
return [0,'文件不存在'];
}
//这里进行缩略图

for($i = 0;$i<count($imageSize);$i++){
suolue($path.$name,$imageSize[$i][0],$imageSize[$i][1],$path);
}
return[1,$path.$name];
}


调用这个方法:

upload(‘file’,’image’,pow(1024,2)*2,[

[500,500],

[100,100],

],[

‘image/png’,’image/jpeg’,’image/gif’,’image/wbmp’

],[‘png’,’jpg’,’jpeg’,’jpe’,’pjpeg’,’gif’,’wbmp’,’bmp’]);

三秒回调到多文件上传 (这里就不写了,和上一个多文件上传代码一样);

html 页面

<html>
<head>
<title>文件上传</title>
<meta charset = "utf-8"/>
</head>
<body>
<form action = "onUpload.php" method = "post" enctype ="multipart/form-data">
<!--<input type = "text" name = "username"/><br/>-->
<input type = "file" name = "file"/><br/>
<input type = "submit" value ="提交"/>
</form>
</body>


html

<html>
<head>
<title>文件上传</title>
<meta charset = "utf-8"/>
</head>
<body>
<form action = "linkUploade.php" method = "post" enctype ="multipart/form-data">
<input type = "file" name = "file[]"/><br/>
<input type = "file" name = "file[]"/><br/>
<input type = "file" name = "file[]"/><br/>
<input type = "submit" value ="提交"/>
</form>
</body>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: