码迷,mamicode.com
首页 > Web开发 > 详细

HTML5 图片本地压缩上传插件

时间:2018-08-08 15:41:34      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:html5   style   htm   ext   directory   字段   attr   files   tor   

======================前端代码=========================

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>localResizeIMG</title>
    <!--引入JQuery 用于异步上传图片-->
    <script type="text/javascript" src="dist/jquery-3.1.1.min.js"></script>
    <!--引入 lrz 插件 用于压缩图片-->
    <script type="text/javascript" src="dist/lrz.bundle.js"></script>
</head>
<body>
<input type="file" accept="image/jpeg" capture="camera">
</body>
<script>
    $("input[type=file]").change(function () {
        /* 压缩图片 */
        lrz(this.files[0], {
            width: 300 //设置压缩参数
        }).then(function (rst) {
            /* 处理成功后执行 */
            rst.formData.append(base64img, rst.base64); // 添加额外参数
            $.ajax({
                url: "upload.php",
                type: "POST",
                data: rst.formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    $("<img />").attr("src", data).appendTo("body");
                }
            });
        }).catch(function (err) {
            /* 处理失败后执行 */
        }).always(function () {
            /* 必然执行 */
        })
    })
</script>
</html>
引入插件:<script type="text/javascript" src="dist/lrz.bundle.js"></script>

绑定事件:$("input[type=file]").change(function () {/* 压缩上传处理 */});

压缩图片:lrz(file, [options]);

  file 通过 input:file 得到的文件,或者直接传入图片路径

  [options] 这个参数允许忽略

    width {Number} 图片最大不超过的宽度,默认为原图宽度,高度不设定时会适应宽度

    height {Number} 图片的最大高度,默认为原图高度

    quality {Number} 图片压缩质量,取值 0 - 1,默认为 0.7

    fieldName {String} 后端接收的字段名,默认:file

返回结果:promise 对象

  then(rst) 处理成功后执行

    rst.formData 后端可处理的数据

    rst.file 压缩后的file对象,如果压缩率太低,将会是原始file对象

    rst.fileLen 生成后的图片的大小,后端可以通过此值来校验是否传输完整

    rst.base64 生成后的图片base64,后端可以处理此字符串为图片,也可以直接用于 img.src = base64

    rst.base64Len 生成后的base64的大小,后端可以通过此值来校验是否传输完整

    rst.origin 也就是原始的file对象,里面存放了一些原始文件的信息,例如大小、日期等

异步上传:processData 和 contentType 必须设为 false,否则服务端不会响应

======================后端代码=========================

<?php
$base64_image_content = $_POST[base64img];
$output_directory = ./image; //设置图片存放路径

/* 检查并创建图片存放目录 */
if (!file_exists($output_directory)) {
    mkdir($output_directory, 0777);
}

/* 根据base64编码获取图片类型 */
if (preg_match(/^(data:\s*image\/(\w+);base64,)/, $base64_image_content, $result)) {
    $image_type = $result[2]; //data:image/jpeg;base64,
    $output_file = $output_directory . / . md5(time()) . . . $image_type;
}

/* 将base64编码转换为图片编码写入文件 */
$image_binary = base64_decode(str_replace($result[1], ‘‘, $base64_image_content));
if (file_put_contents($output_file, $image_binary)) {
    echo $output_file; //写入成功输出图片路径
}

 

HTML5 图片本地压缩上传插件

标签:html5   style   htm   ext   directory   字段   attr   files   tor   

原文地址:https://www.cnblogs.com/lujiang/p/9443027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!