通常情况,图片在传递给服务器前时,需要提前进行剪切等基本处理,以便节省服务器资源和流量,

这里基于插件cropBox,实现浏览器端浏览图片、缩放、剪切等功能,

其中,浏览图片基于filereader,具体如下:

1、cropbox本地读取图片并完成剪切,通过DataURL,将图片转化成base64形式的字符串,保存在URL中,

2、通过getDataURL获取含图片信息的base64字符串,并赋予img标签的src,可在前端图片显示,

 注意:base64格式图片信息,只适合在前端显示,传给服务器后解码,显示图片无法打来

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery头像裁剪插件DEMO演示</title>

    <link rel="stylesheet" href="static/css/style.css" type="text/css">

    <script rel="stylesheet" src="static/js/jquery-1.12.4.min.js" ></script>
    <script rel="stylesheet" src="static/js/index.js"></script>
    <script type="text/javascript" src="static/js/cropbox.js"></script>
</head>
<body>
    <div class="container">
      <div class="imageBox">
        <div class="thumbBox"></div>
        <div class="spinner" style="display: none">Loading...</div>
      </div>

      <div class="action">
        <div class="new-contentarea tc">
            <a href="javascript:void(0)" class="upload-img">
                <label for="upload-file">上传图像</label>
            </a>
            <input type="file" class="" name="upload-file" id="upload-file" />
        </div>
        <input type="button" id="btnCrop"  class="Btnsty_peyton" value="裁切">
        <input type="button" id="btnZoomIn" class="Btnsty_peyton" value="+"  >
        <input type="button" id="btnZoomOut" class="Btnsty_peyton" value="-" >
      </div>

      <div class="cropped"></div>
    </div>
</body>
</html>
html
@charset "utf-8";
.container {
    width: 400px;
    margin: 40px auto 0 auto;
    position: relative;
    font-family: 微软雅黑;
    font-size: 12px;
}
.container p {
    line-height: 12px;
    line-height: 0px;
    height: 0px;
    margin: 10px;
    color: #bbb
}
.action {
    width: 400px;
    height: 30px;
    margin: 10px 0;
}
.cropped {
    position: absolute;
    right: -230px;
    top: 0;
    width: 200px;
    border: 1px #ddd solid;
    height: 460px;
    padding: 4px;
    box-shadow: 0px 0px 12px #ddd;
    text-align: center;
}
.imageBox {
    position: relative;
    height: 400px;
    width: 400px;
    border: 1px solid #aaa;
    background: #fff;
    overflow: hidden;
    background-repeat: no-repeat;
    cursor: move;
    box-shadow: 4px 4px 12px #B0B0B0; 
}
.imageBox .thumbBox {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 200px;
    height: 200px;
    margin-top: -100px;
    margin-left: -100px;
    box-sizing: border-box;
    border: 1px solid rgb(102,102,102);
    box-shadow: 0 0 0 1000px rgba(0, 0, 0, 0.5);
    background: none repeat scroll 0% 0% transparent;
}
.imageBox .spinner {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    text-align: center;
    line-height: 400px;
    background: rgba(0,0,0,0.7);
}

.Btnsty_peyton{
  float: right;
  width: 66px;
  display: inline-block;
  margin-bottom: 10px;
  height: 57px;
  line-height: 57px;
  font-size: 20px;
  color: #FFFFFF;
  margin:0px 2px;
  background-color: #f38e81;
  border-radius: 3px;
  text-decoration: none;
  cursor: pointer;
  box-shadow: 0px 0px 5px #B0B0B0;
  border: 0px #fff solid;}

/*选择文件上传*/
.new-contentarea {
    width: 165px;
    overflow:hidden;
    margin: 0 auto;
    position:relative;
    float:left;
}
.new-contentarea label {
    width:100%;
    height:100%;
    display:block;
}
.new-contentarea input[type=file] {
    width:188px;
    height:60px;
    background:#333;
    margin: 0 auto;
    position:absolute;
    right:50%;
    margin-right:-94px;
    top:0;
    right/*\**/:0px\9;
    margin-right/*\**/:0px\9;
    width/*\**/:10px\9;
    opacity:0;
    filter:alpha(opacity=0);
    z-index:2;
}

a.upload-img{
    width:165px;
    display: inline-block;
    margin-bottom: 10px;
    height:57px;
    line-height: 57px;
    font-size: 20px;
    color: #FFFFFF;
    background-color: #f38e81;
    border-radius: 3px;
    text-decoration:none;
    cursor:pointer;
    border: 0px #fff solid;
    box-shadow: 0px 0px 5px #B0B0B0;
}
a.upload-img:hover{
    background-color: #ec7e70;
}

.tc{text-align:center;}
css
$(window).load(function() {
    //上传头像
    var options =
    {
        thumbBox: '.thumbBox',
        spinner: '.spinner',
        imgSrc: 'static/img/5.jpg'
    };
    var cropper = $('.imageBox').cropbox(options);

    $('#upload-file').on('change', function(){
        var reader = new FileReader();
        reader.onload = function(e) {
            options.imgSrc = e.target.result;
            cropper = $('.imageBox').cropbox(options);
        }
        reader.readAsDataURL(this.files[0]);
        this.files = [];
    });

    //裁剪
    $('#btnCrop').on('click', function(){
        var img = cropper.getDataURL();
        $('.cropped').html('');
        $('.cropped').append('<img src="'+img+'" align="absmiddle" style="width:64px;margin-top:4px;border-radius:64px;box-shadow:0px 0px 12px #7E7E7E;" ><p>64px*64px</p>');
        $('.cropped').append('<img src="'+img+'" align="absmiddle" style="width:128px;margin-top:4px;border-radius:128px;box-shadow:0px 0px 12px #7E7E7E;"><p>128px*128px</p>');
        $('.cropped').append('<img src="'+img+'" align="absmiddle" style="width:180px;margin-top:4px;border-radius:180px;box-shadow:0px 0px 12px #7E7E7E;"><p>180px*180px</p>');
    });

    //缩放
    $('#btnZoomIn').on('click', function(){
        cropper.zoomIn();
    })
    $('#btnZoomOut').on('click', function(){
        cropper.zoomOut();
    });
});
js

 

# 将base64 图片写入数据库
        image = post_info['img_src']

        strs = re.match('^data:image/(jpeg|png|gif);base64,', image) # 正则匹配出前面的文件类型去掉

        image = image.replace(strs.group(), '')
        imgdata = base64.b64decode(image)   #转换成图片对象

        path = os.path.join(STATIC_URL,'pic/profile/1.jpg')

        file = open(path, 'wb')
        file.write(imgdata)   # 保存图片
        file.close()