流浪男
流浪男
  • 发布:2015-03-29 02:29
  • 更新:2021-02-09 10:18
  • 阅读:25867

【分享】头像类裁剪上传

分类:HTML5+

夜深了,分享个头像类的裁剪上传,代码写的比较啰嗦。
虽然说用jquery渲染力差,没办法,还是用了。然后借助cropper来完成裁剪

<script src="plugin/avatar/js/jquery.min.js"></script>  
<script src="plugin/avatar/js/cropper.js"></script>  
<link href="plugin/avatar/css/cropper.css" rel="stylesheet">

照片容器

<a href="#picture"><div id="changeAvatar"></div></a>

裁剪照片的容器(样式就自己写了)

<div id="showEdit">  
        <header class="mui-bar mui-bar-nav mui-nav-bg" style="background:none; position:absolute; top:0;">  
            <a class="mui-icon iconfont icon-roundclosefill mui-pull-left" style="color:#ffffff; font-size:36px;" onclick="closeEdit();"></a>  
            <a class="mui-icon iconfont icon-roundcheckfill mui-pull-right" style="color:#ffffff; font-size:36px;" onclick="confirm();"></a>  
        </header>  
        <div id="report"></div>  
    </div>

下面就很简单了,调用下相机和相册选择图片。然后加入cropper的几行代码就搞定了

//拍照  
function getImage() {  
    var cmr = plus.camera.getCamera();  
    cmr.captureImage( function (p) {  
        plus.io.resolveLocalFileSystemURL( p, function ( entry ) {      
            var localurl = entry.toLocalURL();//  
            $("#report").html('<img src="'+localurl+'">');  
            cutImg();  
            mui('#picture').popover('toggle');  
        });  
    });  
}  
//相册选取  
function galleryImgs(){  
    plus.gallery.pick( function(e){  
        $("#report").html('<img src="'+e.files[0]+'">');  
        cutImg();  
        mui('#picture').popover('toggle');  
    }, function ( e ) {  
        //outSet( "取消选择图片" );  
    },{filter:"image",multiple:true});  
}

照片裁剪类:

function cutImg(){    
    $("#showEdit").fadeIn();  
    var $image = $('#report > img');  
    $image.cropper({  
      aspectRatio: 1 / 1,  
      autoCropArea: 0.8  
    });  
}

确定裁剪并上传(base64)

function confirm(){  

    $("#showEdit").fadeOut();  
    var $image = $('#report > img');  
    var dataURL = $image.cropper("getDataURL");  
    $("#changeAvatar").html('<img src="'+dataURL+'" />');  
}  

function postAvatar() {  

    var $image = $('#report > img');  
    var dataURL = $image.cropper("getDataURL");  
    var data = {  
            base64: dataURL  
    };  
    $.post(url,data,function(data){  
        //这里就自己写了哈  
    });  
};

效果

有高手可以补充下的谢谢啦哈

23 关注 分享
raogang aaaa DCloud_heavensoft bober 尘岳two shuoS j豆豆 Snoopy 小莫骚麦 Oscar wind gaus 建东 suci 豆花饭 Trust 1***@qq.com taozijiayou jayhou 1***@qq.com 2***@qq.com 小明子 t***@protonmail.com

要回复文章请先登录注册

y***@126.com

y***@126.com

楼主没有列出相关资源,补充下:https://github.com/fengyuanchen/cropper;这个开源插件是国内大牛写的,另外支持图片旋转;具体可以参照其api说明。
2016-08-03 17:39
qixi

qixi

mark
2016-07-19 09:41
trueto

trueto

将图片压缩一下后再裁剪,比较快
2016-06-02 18:23
suci

suci

给我个demo好不好呀?
2016-03-29 16:45
陈影

陈影

我用你这个报这错是怎么回事是我的cropper没引用对?
[ERROR] : TypeError: $image.cropper is not a function. (In '$image.cropper({
aspectRatio: 1 / 1,
autoCropArea: 0.8
})', '$image.cropper' is undefined)
2016-02-18 14:55
张静

张静

回复 云钦 :
你好,你上传成功了吗,为什么我使用这个方法,拍照截取一直是空的。你能帮我看一下问题吗
2015-12-07 12:44
张静

张静

回复 小龙哥 :
我这边使用这个方法,还是截不到图。相册拍照图片。截图是空的。你能帮我看看吗
2015-12-07 12:07
水御双氛

水御双氛

能通过截取框不变,变区域大小来进行截图吗?
2015-10-12 11:10
东翌学院

东翌学院

错了。IOS截取有问题,使用cropper.js不行
2015-09-29 14:35
东翌学院

东翌学院

IOS截取没问题。
2015-09-29 14:34