当前位置:网站首页>图片裁剪cropper 示例
图片裁剪cropper 示例
2022-07-25 15:06:00 【深海蓝山】
头像上传时需要进行裁剪上传,可使用<input type="file">进行图片选择,然后结合cropper进行裁剪,裁剪结果是图片的base64格式,然后把结果上传到服务器就可以了
详情参数可参考:https://github.com/fengyuanchen/cropperjs
需求引入相关js和CSS:
https://cdn.bootcdn.net/ajax/libs/cropperjs/1.5.9/cropper.min.css
https://cdn.bootcdn.net/ajax/libs/cropperjs/1.5.9/cropper.min.js效果:

代码示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link href="https://cdn.bootcdn.net/ajax/libs/cropperjs/1.5.9/cropper.min.css" rel="stylesheet">
<script src="https://cdn.bootcdn.net/ajax/libs/cropperjs/1.5.9/cropper.min.js"></script>
<title>Cropperjs</title>
<style>
.photo_cropper {
float: left;
width: 400px;
height: 370px;
margin-right: 20px;
}
.cropper_preview {
float: left;
width: 310px;
overflow: hidden;
}
.photo_preview {
float: left;
overflow: hidden;
margin-bottom: 10px;
margin-right: 10px;
}
.photo_cropper>img,
.photo_preview>img {
width: 100%;
height: 100%;
}
.preview-lg {
height: 240px;
width: 260px;
}
.preview-md {
height: 120px;
width: 120px;
}
.preview-sm {
height: 80px;
width: 80px;
}
.preview-xs {
height: 40px;
width: 40px;
}
.hint-content {
top: -18px;
font-size: 12px;
color: gray;
}
.btn-primary,
input[type="file"] {
padding: 5px 10px;
background-color: #00c176;
color: white;
border-radius: 4px;
border: none;
}
.btn-primary {
padding: 8px 10px;
}
</style>
</head>
<body>
<div class="photo_container">
<div class="photo_cropper">
<img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1604414071111&di=6681d440860b07f3c7a4fc2fcb224180&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F0dd7912397dda144dac4acc9b2b7d0a20df486f8.jpg"
id="cr_photoImg">
</div>
<div class="cropper_preview">
<div class="photo_preview preview-lg"></div>
<div class="photo_preview preview-md"></div>
<div class="photo_preview preview-sm"></div>
<div class="photo_preview preview-xs"></div>
</div>
</div>
<div class="cropper_actions" style="clear: both;">
<div class="actions">
<input type="file" accept=".gif,.jpg,.jpeg,.png" onchange="imgChange(event)" id="fileInput">
<input type="button" class="btn-primary cr_savePhotoBtn" value=" 保存">
</div>
<div class="hint-content">支持格式:png、jpg, 图片大小不超过5MB。</div>
</div>
</body>
</html>
<script>
let cropper = null;
$('.photo_preview').html($('.photo_cropper').html());
function imgChange(e) {
var fileObj = e.target.files[0];
var typeExtens = 'image/jpeg,image/png';
if (!typeExtens.includes(fileObj.type)) {
alert('只能上传png、jpg格式。');
return false;
}
if (fileObj.size > 5 * 1024 * 1024) {
alert('图片大小不超过5MB。');
return false;
}
console.info(fileObj);//图片文件
var reader = new FileReader();
reader.onload = (function (file) {
return function (e) {
$('#cr_photoImg').attr('src', this.result);//这个就是base64的数据了
cropper = initCropper(cropper);
};
})(e.target.files[0]);
reader.readAsDataURL(e.target.files[0]);
}
// 初始化图片裁剪
function initCropper(cropper) {
cropper && cropper.destroy();
cropper = new Cropper(document.getElementById('cr_photoImg'), {
viewMode: 1,
preview: '.photo_preview',
minCropBoxWidth: 150,
aspectRatio: 1 / 1,
autoCropArea: 1
});
return cropper;
}
$('.cr_selectPhoto').on('click', function () {
$('#fileInput').trigger("click");
})
$('.cr_savePhotoBtn').on('click', function () {
if ($('#fileInput').val().length == 0) {
alert('请选择图片');
return false;
}
const blobMimeType = 'image/png';
let croppedCanvas = cropper.getCroppedCanvas({
width: 200,
height: 200
});
let dataStr = croppedCanvas.toDataURL(blobMimeType, 0.9);
// addSave(dataStr)//将图片的base64的数据保存到服务器
});
</script>
边栏推荐
- ES5写继承的思路
- As methods for viewing and excluding dependencies
- 解决asp.net上传文件时文件太大导致的错误
- 6月产品升级观察站
- 37 element mode (inline element, block element, inline block element)
- dpdk 收发包问题案例:使用不匹配的收发包函数触发的不收包问题定位
- SQL优化的一些建议,希望可以帮到和我一样被SQL折磨的你
- API health status self inspection
- String type time comparison method with error string.compareto
- 万能通用智能JS表单验证
猜你喜欢

"How to use" agent mode

6线SPI传输模式探索

27 选择器的分类

06、类神经网络

ice 100G 网卡分片报文 hash 问题

VS2010 add WAP mobile form template

Realsense ROS installation configuration introduction and problem solving

Boosting之GBDT源码分析
[Android] recyclerview caching mechanism, is it really difficult to understand? What level of cache is it?

27 classification of selectors
随机推荐
I2C device driver hierarchy
L1和L2正则化
sql to linq 之存储过程偏
AS查看依赖关系和排除依赖关系的办法
"Ask every day" how locksupport realizes thread waiting and wakeup
MeanShift聚类-01原理分析
Share a department design method that avoids recursion
MySQL的登陆【数据库系统】
LeetCode-198-打家劫舍
The solution to the problem that the progress bar of ros2 installation connext RMW is stuck at 13%
Ssh server rejected password
[MySQL series] - how much do you know about the index
51 single chip microcomputer learning notes (2)
Stored procedure bias of SQL to LINQ
Splice a field of the list set into a single string
Visual Studio 2022 查看类关系图
Pl/sql creates and executes ORALCE stored procedures and returns the result set
ES5写继承的思路
Implementation of redis distributed lock
L1 and L2 regularization