当前位置:网站首页>图片裁剪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>
边栏推荐
- As methods for viewing and excluding dependencies
- Install entityframework method
- 转载----如何阅读代码?
- 简易轮播图和打地鼠
- 006操作符简介
- LeetCode第 303 场周赛
- 45padding won't open the box
- dpdk 收发包问题案例:使用不匹配的收发包函数触发的不收包问题定位
- Login of MySQL [database system]
- IP address classification, which determines whether a network segment is a subnet supernetwork
猜你喜欢

45padding不会撑开盒子的情况

直播课堂系统05-后台管理系统

冈萨雷斯 数字图像处理 第一章绪论

Visual Studio 2022 查看类关系图

39 simple version of millet sidebar exercise

流程控制(上)

Live classroom system 05 background management system

Yarn: the file yarn.ps1 cannot be loaded because running scripts is prohibited on this system.

006操作符简介

String type time comparison method with error string.compareto
随机推荐
Splice a field of the list set into a single string
[C topic] Li Kou 88. merge two ordered arrays
How to realize a correct double check locking
Qt connect 中, SIGNAL,SLOT 与 lambda 对比
没错,请求DNS服务器还可以使用UDP协议
Realsense ROS installation configuration introduction and problem solving
Client error: invalid param endpoint is blank
关于RDBMS和非RDBMS【数据库系统】
Overview of cloud security technology development
【JS高级】js之正则相关函数以及正则对象_02
Thymeleaf notes
37 element mode (inline element, block element, inline block element)
CMake指定OpenCV版本
Add the jar package under lib directory to the project in idea
[C topic] the penultimate node in the Niuke linked list
The solution to the problem that the progress bar of ros2 installation connext RMW is stuck at 13%
云安全技术发展综述
pl/sql 创建并执行oralce存储过程,并返回结果集
IP address classification, which determines whether a network segment is a subnet supernetwork
006操作符简介