首页 > 编程 > JavaScript > 正文

JS实现异步上传压缩图片

2019-11-19 16:46:12
字体:
来源:转载
供稿:网友

摘要: 使用iframe来处理异步上传图片,在现在这个时代来说,多多少少都有点落后了!单单就凭AJAX和JS就不能做到异步上传图片了吗?

感谢 think2011 这位兄台的JS库:https://github.com/think2011/LocalResizeIMG

先看调用页面:

<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">    <script type="text/javascript" src="./js/lrz.mobile.min.js"></script>    <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script></head><body class="upload"><form id="form">    <div id="img_show"></div>    <div id="upload">      <div id="img_file"><input type="file" accept="image/*" ><div class="btn">选择图片</div></div>    </div>    <input type="submit" class="tijiao" value="提交">  </form></body><script type="text/javascript">  var img;  $("input:file").change(function (){    //console.log(this.files[0]);    lrz(this.files[0],{width:640,quality:0.9},function(rst){      img = rst.base64;      var html = [];      var show_img = new Image();      show_img.src = rst.base64;      $("#img_show").html("<div class='upimg'></div>");      $(".upimg").html(show_img);    });  });  $("#form").submit(function (){    var phone = $("input[name='phone']").val();    var month = $("input[name='month']").val();    $.post("upload.php",{img:img,phone:phone,month:month},function(data){      img = null;      alert(data.msg);    },'json');    return false;  });</script></html>

1.首先你要载入JS类库:

<script type="text/javascript" src="./js/lrz.mobile.min.js"></script>

2.然后就是写好form

3.准备处理图片以及图片异步提交的JS。

<script type="text/javascript">  var img;  $("input:file").change(function (){    //console.log(this.files[0]);    lrz(this.files[0],{width:640,quality:0.9},function(rst){      img = rst.base64;      var html = [];      var show_img = new Image();      show_img.src = rst.base64;      $("#img_show").html("<div class='upimg'></div>");      $(".upimg").html(show_img);    });  });  $("#form").submit(function (){    var phone = $("input[name='phone']").val();    var month = $("input[name='month']").val();    $.post("upload.php",{img:img},function(data){      img = null;      alert(data.msg);    },'json');    return false;  });</script>

从代码中可以看出,这个JS库是把图片转成码,然后用变量存起来,然后在用异步POST到服务器中在处理。

看起来貌似没有什么特别的地方,的确实在也没有什么特别的地方.......

后台处理程序PHP:

function error($msg=''){  $return = array('msg'=>$msg);  echo json_encode($return);  exit();}function main(){  if(!$_POST['img']){    error('请上传图片!');  }    $img = $_POST['img'];    $path = './upload/';    $type_limit = array('jpg','jpeg','png');  if(preg_match('/data:/s*image//(/w+);base64,/iu',$img,$tmp)){    if(!in_array($tmp[1],$type_limit)){      error('图片格式不正确,只支持jpg,jpeg,png!');    }  }else{    error('抱歉!上传失败,请重新再试!');  }    $img = str_replace(' ','+',$img);    $img = str_replace($tmp[0], '', $img);  $img = base64_decode($img);    $file = $path.time().'.'.$tmp[1];  if(!file_put_contents($file,$img)){    error('上传图片失败!');  }else{    error('恭喜您!上传成功!');  }}main();

上述代码如果有错误欢迎指出。

如上诉代码,正如你看到的那样,经过BASE64加密过的图片码经过JS异步的POST过来后端后,我们要把代码还原。但是JS库加密的时候会带有一些标签,所以还原前需要处理掉这些本来不属于图片的东西。

$img = str_replace(' ','+',$img);  $img = str_replace($tmp[0], '', $img);$img = base64_decode($img);

最后把代码塞进文件,设置好相应的文件名和扩展名,图片就成功上传到了服务器了。

注意:

前后端包括JS编码要要一致,建议UTF-8

如果图片还原不会来的话,那肯定是数据问题,打印POST过来的图片码出来看看。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表