HTML5+Canvas手机拍摄,本地压缩上传图片

思路:在前端把图片压缩,然后转换成为Base64的编码,再把Base64的编码使用AJAX来POST到服务器,然后在PHP解开Base64,写入到一个文件去。

<!DOCTYPE HTML>
<html lang="zh-CN">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0,user-scalable=no" />  
<head>
	<meta charset="UTF-8">
	<title>LocalResizeIMG</title>
</head>
<style>
    body {
        margin: 20px 20%;
        color:#777;
        text-align: center;
    }
</style>
<body>
    <h1 class="text-center">LocalResizeIMG-本地压缩 1.0</h1>
    <hr/>
    <input type="file" />
    <hr/>

	<!-- javascript
		================================================== -->
	<script src="/api/localResizeIMG-gh-pages/patch/jquery-2.1.1.min.js" type="text/javascript"></script>
	<script src="/api/localResizeIMG-gh-pages/LocalResizeIMG.js" type="text/javascript"></script>
	
    <!-- mobileBUGFix.js 兼容修复移动设备 -->
	<script src="/api/localResizeIMG-gh-pages/patch/mobileBUGFix.mini.js" type="text/javascript"></script>
	<script type="text/javascript">
        $('input:file').localResizeIMG({
             width: 500,
             quality: 0.8,
             success: function (result) {
	             var img = new Image();
	             img.src = result.base64;
	
	             $('body').append(img);
	             //console.log(result);
	            $.ajax({
	            	 url: './uploads.php',
	            	 type: 'POST',
	            	 data:{formFile:result.clearBase64},
	            	 dataType: 'HTML',
	            	 timeout: 1000,
	            	 error: function(){
	            		 alert('Error loading PHP document');
	            	},
	            	 success: function(result){
	            		 //console.log(result);
						alert("Uploads success~")
	            	}
	             }); 
             }
         });
	</script>
</body>
</html>


<?php
	$base64 = $_POST['formFile'];
	$IMG = base64_decode($base64);
	$path = './';
	file_put_contents($path.time().'.jpg',$IMG);
?>


插件地址: https://github.com/think2011/localResizeIMG3/
demo下载:

猜你喜欢

转载自mangon.iteye.com/blog/2213779