欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)異步上傳壓縮圖片

 更新時間:2017年04月22日 09:03:14   作者:zerodeng  
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)異步上傳壓縮圖片,并立即顯示圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下

摘要: 使用iframe來處理異步上傳圖片,在現(xiàn)在這個時代來說,多多少少都有點(diǎn)落后了!單單就憑AJAX和JS就不能做到異步上傳圖片了嗎?

感謝 think2011 這位兄臺的JS庫:https://github.com/think2011/LocalResizeIMG

先看調(diào)用頁面:

<!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.準(zhǔn)備處理圖片以及圖片異步提交的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庫是把圖片轉(zhuǎn)成碼,然后用變量存起來,然后在用異步POST到服務(wù)器中在處理。

看起來貌似沒有什么特別的地方,的確實(shí)在也沒有什么特別的地方.......

后臺處理程序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();

上述代碼如果有錯誤歡迎指出。

如上訴代碼,正如你看到的那樣,經(jīng)過BASE64加密過的圖片碼經(jīng)過JS異步的POST過來后端后,我們要把代碼還原。但是JS庫加密的時候會帶有一些標(biāo)簽,所以還原前需要處理掉這些本來不屬于圖片的東西。

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


最后把代碼塞進(jìn)文件,設(shè)置好相應(yīng)的文件名和擴(kuò)展名,圖片就成功上傳到了服務(wù)器了。

注意:

前后端包括JS編碼要要一致,建議UTF-8

如果圖片還原不會來的話,那肯定是數(shù)據(jù)問題,打印POST過來的圖片碼出來看看。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論