欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS实现异步上传压缩图片

程序员文章站 2023-08-21 10:36:05
摘要: 使用iframe来处理异步上传图片,在现在这个时代来说,多多少少都有点落后了!单单就凭ajax和js就不能做到异步上传图片了吗? 感谢 think2011 这...

摘要: 使用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过来的图片码出来看看。

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