H5+ 图片压缩上传

转载自:http://www.scscms.com/html/article/20150319-1607191.html

index.html

  1. <!DOCTYPE html>   
  2. <html>   
  3.     <head>   
  4.         <meta charset="utf-8">   
  5.         <title>H5+ 图片压缩上传</title>   
  6.         <script type="text/javascript">   
  7.             var imgData = null;   
  8.             function appendFile(path){   
  9.                   var img = new Image();   
  10.                     img.onload = function () {   
  11.                         var that = this;   
  12.                         var w = that.width,h = that.height,scale = w / h;   
  13.                             w = Math.min(200,w);//自己定义一个高度或者使用默认值   
  14.                             h = w / scale;   
  15.                         var canvas = document.createElement('canvas');   
  16.                         canvas.setAttribute("width",w);   
  17.                         canvas.setAttribute("height",h);   
  18.                         canvas.getContext('2d').drawImage(that, 0, 0, w, h);   
  19.                         imgData = canvas.toDataURL("image/jpeg",1);//1是清晰度[0.1~1]   
  20.                         var pic = document.createElement("img");   
  21.                         pic.width = w;   
  22.                         pic.height = h;   
  23.                         pic.src = imgData;   
  24.                         document.body.appendChild(pic);//把图片插入到页面中显示   
  25.                         plus.nativeUI.confirm( "您确定要上传图片?"function(e){   
  26.                             e.index == 0 && upload();//确定后开始上传   
  27.                         }, "系统提醒", ["确定","取消"] );   
  28.                 }   
  29.                 img.src = path;              
  30.             }   
  31.             // 上传文件   
  32.             function upload(){    
  33.                 plus.nativeUI.showWaiting();   
  34.                 var xhr = new plus.net.XMLHttpRequest();   
  35.                 xhr.onreadystatechange = function(){   
  36.                     if(xhr.readyState == 4 && xhr.status == 200){   
  37.                         plus.nativeUI.closeWaiting();   
  38.                         plus.nativeUI.alert(xhr.responseText == "ok" ? "上传成功!" :"上传失败!");   
  39.                     }   
  40.                 };   
  41.                 xhr.open( "POST""http://www.????.com/test.php");    //设置你的域名地址   
  42.                 xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");   
  43.                 xhr.send("imgfile="+encodeURIComponent(imgData)); //参数必须编码(坑)   
  44.             }   
  45.             //弹出系统确认对话框   
  46.             function actionSheet(){   
  47.                 plus.nativeUI.actionSheet( {title:"请选择图片",cancel:"取消",buttons:[{title:"直接拍照"},{title:"选择相册"}]}, function(e){   
  48.                     if(1 == e.index){   
  49.                         //plus.nativeUI.alert("直接拍照");   
  50.                         var cmr = plus.camera.getCamera();   
  51.                             cmr.captureImage(function(p) {   
  52.                                 plus.io.resolveLocalFileSystemURL(p, function(entry) {   
  53.                                     appendFile(entry.fullPath);   
  54.                                 }, function(e) {   
  55.                                     plus.nativeUI.alert(e.message);   
  56.                                 });   
  57.                             }, function(e) {}, {   
  58.                                 filename: "_doc/camera/"  
  59.                             });   
  60.                     }else if(2 == e.index){   
  61.                         //plus.nativeUI.alert("选择相册");   
  62.                         plus.gallery.pick(function(path){   
  63.                              appendFile(path);   
  64.                         });   
  65.                     }   
  66.                 });   
  67.             }   
  68.             function plusReady(){   
  69.                 var mainActivity = plus.android.runtimeMainActivity();   
  70.                 actionSheet();//如果不想打开页面就弹出菜单就删除此行   
  71.                 document.querySelector("button").addEventListener("click",actionSheet,false);   
  72.                 var back = false;//退出应用   
  73.                 plus.key.addEventListener( "backbutton"function(){   
  74.                     back ? plus.runtime.quit() : (back = true);   
  75.                     plus.nativeUI.toast( "再按一次退出应用" );   
  76.                     setTimeout(function(){back=false},1000);   
  77.                     /*  var main = plus.android.runtimeMainActivity();  
  78.                         main.moveTaskToBack(false);退到后台运行   */  
  79.                 }, false );   
  80.             }   
  81.             document.addEventListener("plusready",plusReady,false);   
  82.         </script>   
  83.         <style type="text/css">   
  84.             html {font-family: sans-serif;-webkit-text-size-adjust: 100%;text-align: center;}   
  85.             body{margin: 0;}   
  86.             h1{-webkit-box-shadow: 0 1px 6px #ccc;box-shadow: 0 1px 6px #ccc;margin-top: -2px;font-size: 17px;   
  87.             font-weight: 500;line-height: 44px;color: #000;text-align: center;white-space: nowrap;}   
  88.         </style>   
  89.     </head>   
  90.     <body>   
  91.         <h1>H5+ 图片压缩上传</h1>   
  92.         <p><button type="button">上传图片</button></p>        
  93.     </body>   
  94. </html>  

test.php

  1. <?php   
  2.     $files = $_POST["imgfile"];   
  3.     $tmp = base64_decode(substr($files,22));   
  4.     $fp = dirname(__FILE__)."/images/".time().".jpg";   
  5.     file_put_contents$fp$tmp);   
  6.     echo "ok";   
  7. ?>  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值