最近在折腾移动站的开发,涉及到了一个手机里面上传图片。于是经过N久的折腾,找到一个插件localResizeIMG,这个插件也使用与微信里面的图片上传.

插件名称叫localResizeIMG ,去看了一下WIKI文档,整理了一下用法,如下:

HTML代码

<!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);
            $.ajax({
                url: './uploads.php',
                type: 'POST',
                data: {formFile: result.clearBase64},
                dataType: 'HTML',
                timeout: 1000,
                error: function () {
                    alert('Error loading PHP document');
                },
                success: function (result) {
                }
            });
        }
    });    </script>
</body>
</html>

PHP代码:

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

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

原插件地址:http://github.com/think2011/LocalResizeIMG

然后发现我朋友也写有一篇这个插件的使用的文章,地址在这里:http://a3147972.blog.51cto.com/2366547/1551066

最后,欢迎加Q群: 252799167

2015年04月11日12:23:10 Update:

这插件的作者已经对插件进行了升级,推荐使用新的插件:https://github.com/think2011/localResizeIMG3/

2015年8月12日 10:59:31 Update:

作者又对插件进行了版本更新:https://github.com/think2011/localResizeIMG4

2015年11月27日21:07:31 Update:

插件原作者去除了旧版本的支持,现在只维护最新版的localResizeIMG插件了,地址是:localResizeIMG

2016年05月14日20:18:11 更新: 这个插件作者更新好像减少了。