<!DOCTYPE html>
<html>
<head>
<meta charset=
"UTF-8"
>
<meta name=
"viewport"
content=
"width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui"
>
<title>实名验证</title>
<script type=
"text/javascript"
>
function
photoCompress(file,w,objp){
var
ready=
new
FileReader();
ready.readAsDataURL(file);
ready.onload=
function
(){
var
re=this.result;
canvasDataURL(re,w,objp)
}
}
function
canvasDataURL(path, obj, callback){
var
img =
new
Image();
img.src = path;
img.onload =
function
(){
var
that = this;
var
w = that.width,
h = that.height,
scale = w / h;
w = obj.width || w;
h = obj.height || (w / scale);
var
quality = 0.7;
var
canvas = document.createElement('canvas');
var
ctx = canvas.getContext('2d');
var
anw = document.createAttribute(
"width"
);
anw.nodeValue = w;
var
anh = document.createAttribute(
"height"
);
anh.nodeValue = h;
canvas.setAttributeNode(anw);
canvas.setAttributeNode(anh);
ctx.drawImage(that, 0, 0, w, h);
if
(obj.quality && obj.quality <= 1 && obj.quality > 0){
quality = obj.quality;
}
var
base64 = canvas.toDataURL('image/jpeg', quality);
callback(base64);
}
}
function
convertBase64UrlToBlob(urlData){
var
arr = urlData.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr =
new
Uint8Array(n);
while
(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return
new
Blob([u8arr], {type:mime});
}
var
xhr;
function
uploadClick() {
document.getElementsByClassName(
"uploadbtn"
)[0].value = '上传中...';
document.getElementsByClassName(
"uploadbtn"
)[0].disabled=true;
var
obj = document.getElementsByClassName(
"myfile"
);
for
(
var
i=0;i<2;i++){
UploadFile(obj[i].files[0],'file'+i);
}
}
function
UploadFile(fileObj,filed){
var
shopid = document.getElementById('shopid').value;
var
adminid = document.getElementById('adminid').value;
var
url =
"newshimingupload.php"
;
var
form =
new
FormData();
if
(fileObj.size/1024 > 100) {
photoCompress(fileObj, {
quality: 0.2
},
function
(base64Codes){
var
bl = convertBase64UrlToBlob(base64Codes);
form.append(
"file"
, bl,
"file_"
+
Date
.parse(
new
Date
())+
".jpg"
);
form.append(
"shopid"
, shopid);
form.append(
"adminid"
, adminid);
form.append(
"filed"
, filed);
xhr =
new
XMLHttpRequest();
xhr.open(
"post"
, url, false);
xhr.onload = uploadComplete;
xhr.onerror = uploadFailed;
xhr.upload.onloadstart =
function
(){
ot =
new
Date
().getTime();
oloaded = 0;
};
xhr.send(form);
});
}
else
{
form.append(
"file"
, fileObj);
form.append(
"shopid"
, shopid);
form.append(
"adminid"
, adminid);
form.append(
"filed"
, filed);
xhr =
new
XMLHttpRequest();
xhr.open(
"post"
, url, false);
xhr.onload = uploadComplete;
xhr.onerror = uploadFailed;
xhr.upload.onloadstart =
function
(){
ot =
new
Date
().getTime();
oloaded = 0;
};
xhr.send(form);
}
}
function
uploadComplete(evt) {
var
data = JSON.parse(evt.target.responseText);
console.log(data);
if
(data.status){
alert(data.msg);
if
(data.msg == '门店照上传成功'){
window.location.href =
"/dd_admin/index.php"
;
}
}
}
function
uploadFailed(evt) {
alert(
"网络不稳定,请重新上传!"
);
}
</script>
</head>
<body>
<style type=
"text/css"
>
.main{text-align: center;padding-top: 50px;}
.main .myfile{margin-bottom: 15px;}
</style>
<p
class
=
"main"
>
营业执照:
<input type=
"file"
class
=
"myfile"
id=
"file1"
name=
"file1"
accept=
"image/x-png, image/jpg, image/jpeg, image/gif"
/><br>
门店照:
<input type=
"file"
class
=
"myfile"
id=
"file2"
name=
"file2"
accept=
"image/x-png, image/jpg, image/jpeg, image/gif"
/><br>
<input type=
"hidden"
id=
"shopid"
name=
"shopid"
value=
"<?php echo $_GET['shopid']; ?>"
maxlength=
"15"
>
<input type=
"hidden"
id=
"adminid"
name=
"adminid"
value=
"<?php echo $_GET['adminid']; ?>"
maxlength=
"15"
>
<input style=
"margin-top: 25px;"
class
=
"uploadbtn"
type=
"button"
onclick=
"uploadClick()"
value=
"上传"
/><br>
</p>
</body>
</html>
2、前端图片压缩后,请求到自定义的接口upload_deal.php.代码如下
<?php
require_once
('public_func.php');
actionUpload('uploads/','file');
}
3、在第二部引入public_func.php,这块代码主要是对后端处理图片压缩
function
actionUpload(
$path
= '/uploads/',
$filename
='myFile')
{
$file
=
$_FILES
[
$filename
]['name'];
$folder
=
$path
.
date
('Ymd').
"/"
;
$pre
= rand(999,9999).time();
$ext
=
strrchr
(
$file
,'.');
$newName
=
$pre
.
$ext
;
$out
=
array
(
'msg'=>'',
'status'=>'error',
'img_url'=>''
);
if
(!
is_dir
(
$folder
))
{
if
(!
mkdir
(
$folder
, 0777, true)){
$out
['msg'] = '图片目录创建失败!';
echo
json_encode(
$out
);
exit
;
}
}
$im
=
$_FILES
[
$filename
]['tmp_name'];
$maxwidth
=
"1056"
;
$maxheight
=
"500"
;
$imgname
=
$folder
.
$newName
;
$filetype
=
$_FILES
[
$filename
]['type'];
$result
= thumbImage(
$im
,
$maxwidth
,
$maxheight
,
$imgname
,
$filetype
);
if
(
$result
){
$out
['msg'] = '图片上传成功';
$out
['status'] = 'success';
$out
['img_url'] =
$folder
.
$newName
;
}
else
{
$out
['msg'] = '图片上传失败';
}
return
json_encode(
$out
);
exit
;
}
function
thumbImage(
$im
,
$maxwidth
,
$maxheight
,
$name
,
$filetype
)
{
switch
(
$filetype
) {
case
'image/pjpeg':
case
'image/jpeg':
$im
= imagecreatefromjpeg(
$im
);
break
;
case
'image/gif':
$im
= imagecreatefromgif(
$im
);
break
;
case
'image/png':
$im
= imagecreatefrompng(
$im
);
break
;
case
'image/wbmp':
$im
= imagecreatefromwbmp(
$im
);
break
;
}
$resizewidth_tag
=
$resizeheight_tag
= false;
$pic_width
= imagesx(
$im
);
$pic_height
= imagesy(
$im
);
if
((
$maxwidth
&&
$pic_width
>
$maxwidth
) || (
$maxheight
&&
$pic_height
>
$maxheight
))
{
$resizewidth_tag
=
$resizeheight_tag
= false;
if
(
$maxwidth
&&
$pic_width
>
$maxwidth
)
{
$widthratio
=
$maxwidth
/
$pic_width
;
$resizewidth_tag
= true;
}
if
(
$maxheight
&&
$pic_height
>
$maxheight
)
{
$heightratio
=
$maxheight
/
$pic_height
;
$resizeheight_tag
= true;
}
if
(
$resizewidth_tag
&&
$resizeheight_tag
)
{
if
(
$widthratio
<
$heightratio
)
$ratio
=
$widthratio
;
else
$ratio
=
$heightratio
;
}
if
(
$resizewidth_tag
&& !
$resizeheight_tag
)
$ratio
=
$widthratio
;
if
(
$resizeheight_tag
&& !
$resizewidth_tag
)
$ratio
=
$heightratio
;
$newwidth
=
$pic_width
*
$ratio
;
$newheight
=
$pic_height
*
$ratio
;
if
(function_exists(
"imagecopyresampled"
))
{
$newim
= imagecreatetruecolor(
$newwidth
,
$newheight
);
imagecopyresampled(
$newim
,
$im
,0,0,0,0,
$newwidth
,
$newheight
,
$pic_width
,
$pic_height
);
}
else
{
$newim
= imagecreate(
$newwidth
,
$newheight
);
imagecopyresized(
$newim
,
$im
,0,0,0,0,
$newwidth
,
$newheight
,
$pic_width
,
$pic_height
);
}
switch
(
$filetype
) {
case
'image/pjpeg' :
case
'image/jpeg' :
$result
= imagejpeg(
$newim
,
$name
);
break
;
case
'image/gif' :
$result
= imagegif(
$newim
,
$name
);
break
;
case
'image/png' :
$result
= imagepng(
$newim
,
$name
);
break
;
case
'image/wbmp' :
$result
= imagewbmp(
$newim
,
$name
);
break
;
}
imagedestroy(
$newim
);
}
else
{
switch
(
$filetype
) {
case
'image/pjpeg' :
case
'image/jpeg' :
$result
= imagejpeg(
$im
,
$name
);
break
;
case
'image/gif' :
$result
= imagegif(
$im
,
$name
);
break
;
case
'image/png' :
$result
= imagepng(
$im
,
$name
);
break
;
case
'image/wbmp' :
$result
= imagewbmp(
$im
,
$name
);
break
;
}
}
return
$result
;
}