19 Star 56 Fork 13

pumelotea / WebHeadPicker

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

#WebHeadPicker #Web头像拾取器插件

在网上找了一段时间,总的来说,cropper比较好。本项目基于cropper,以及一些互联网资源。 您是否应该使用本项目?如果您需要把头像直接上传到阿里云OSS服务中,那么可以直接使用本项目。如果是上传到自己的应用服务器,也可以使用本项目,但是上传功能需要您自己配合后端实现。

支持的功能

  • 支持前端直接上传头像到阿里云OSS中
  • 支持头像裁剪预览功能
  • 支持获取头像裁剪数据,DataUrl(base64),Blob(二进制)
  • 支持UI界面和逻辑层分离,可定制UI界面
  • 支持图片比例的设置,格式为x/y
  • 支持正圆图片裁剪框
  • 支持裁剪后图像质量的设置
  • 支持图片大小的最大值限制设置
  • 支持图片选择回调
  • 支持授权请求回调
  • 支持上传图片回调
  • 支持剪贴板图片直接粘贴进入裁剪头像模式

实现后端的OSS授权接口

这里提供的是java版本,需要其他语言的请移步阿里云官方文档。

注意:阿里云OSS要开启POST跨域支持

 public JSONObject getOSSToken()
    {
        String endpoint = "oss-cn-shanghai.aliyuncs.com";
        String accessId = "accessId ";
        String accessKey = "accessKey ";
        String bucket = "bucket ";
        String dir = "picture/";
        String host = "http://" + bucket + "." + endpoint;
        OSSClient client = new OSSClient(endpoint, accessId, accessKey);
        try {
            long expireTime = 30;
            long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
            Date expiration = new Date(expireEndTime);
            PolicyConditions policyConds = new PolicyConditions();
            policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
            policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);

            String postPolicy = client.generatePostPolicy(expiration, policyConds);
            byte[] binaryData = postPolicy.getBytes("utf-8");
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            String postSignature = client.calculatePostSignature(postPolicy);

            Map<String, String> respMap = new LinkedHashMap<String, String>();
            respMap.put("accessid", accessId);
            respMap.put("policy", encodedPolicy);
            respMap.put("signature", postSignature);
            //respMap.put("expire", formatISO8601Date(expiration));
            respMap.put("dir", dir);
            respMap.put("host", host);
            respMap.put("expire", String.valueOf(expireEndTime / 1000));
            JSONObject ja1 = JSONObject.fromObject(respMap);
            return ja1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

开始使用

引入文件

//CSS
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="cropper/cropper.min.css" rel="stylesheet">
<link href="headpicker/headpicker.css" rel="stylesheet">
//Js
<script src="cropper/cropper.min.js"></script>
<script src="headpicker/headpicker.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>

Js脚本

	var picker = $('#avatar-modal');//头像弹出层
        
        //弹出层
	$('#head').click(function (e) {
		picker.modal('show');
	});
        
	var headpicker = new HeadPicker();
	var options = {
	    aspectRatio:1,//图片比例
	    circular:true,//是否开启原型遮罩
	    guides:false,//是否显示虚线
		accessApi:"http://192.168.1.102:8080/oss/access",//授权接口
		host:"http://data.yueare.com/",//OSS访问地址,这里我绑定了自己的域名,可以直接使用阿里云提供的
		maxFileSize:1024*1024*2,//设置文件的最大值
		imageQuality:0.9,//设置图像的质量
		fileChooseInput:$('#avatarInput'),//设置图片选择input
		orignImgContainer:$('#orignImg'),//设置原始图像的img容器
		previewImgContainers:$('.avatar-preview'),//设置预览图片的容器,支持多个容器
		rotateBtns:$('.avatar-btns'),//设置图片旋转按钮
		completeBtn:$('.avatar-save'),//设置保存按钮
        clipboardListenerContainerID:"cp",//对一个容器设置剪贴板粘贴监听事件(可选)
                //文件选择消息回调
		picChooseCallBack:function (msg) {
			alert(msg);
		},
                //授权消息回调
		accessCallBack:function (msg) {
			alert(msg);
		},
                //上传回调
		uploadCallBack:function (msg,backImgurl) {
			$('#head').attr('src',backImgurl);
			picker.modal('hide');
		}
	};
	headpicker.initHeadPicker(options);

获取DataURl

headpicker.initHeadPicker(options);初始化后,调用headpicker.getImgDataUrl();

var dataurl = headpicker.getImgDataUrl();
console.log(dataurl);

获取Blob

headpicker.initHeadPicker(options);初始化后,调用headpicker.getImgBlob();

headpicker.getImgBlob(function(blob) {
   console.log(blob);
});
The MIT License (MIT) Copyright (c) 2017 WebHeadPicker Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Web头像拾取裁剪,并且上传到阿里云OSS 展开 收起
JavaScript
MIT
取消

发行版 (4)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/pumelotea/WebHeadPicker.git
git@gitee.com:pumelotea/WebHeadPicker.git
pumelotea
WebHeadPicker
WebHeadPicker
master

搜索帮助