网站首页 > 技术文章 正文
一个简单的 jQuery 图像裁剪插件。从 v4.0.0 开始,Cropper 的核心代码已替换为Cropper.js。
安装
npm install jquery-cropper jquery cropperjs
包含文件:
<script src="/path/to/jquery.js"></script><!-- jQuery is required -->
<script src="/path/to/cropper.js"></script><!-- Cropper.js is required -->
<link href="/path/to/cropper.css" rel="stylesheet">
<script src="/path/to/jquery-cropper.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>基于cropper.js的图片裁剪</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="css/cropper.min.css">
<link rel="stylesheet" href="css/ImgCropping.css">
</head>
<body style="background: #eee">
<button id="replaceImg" class="l-btn">更换图片</button>
<div style="width: 320px;height: 320px;border: solid 1px #555;padding: 5px;margin-top: 10px">
<img id="finalImg" src="" width="100%">
</div>
<!--图片裁剪框 start-->
<div style="display: none" class="tailoring-container">
<div class="black-cloth" onclick="closeTailor(this)"></div>
<div class="tailoring-content">
<div class="tailoring-content-one">
<label title="上传图片" for="chooseImg" class="l-btn choose-btn">
<input type="file" accept="image/jpg,image/jpeg,image/png" name="file" id="chooseImg" class="hidden" onchange="selectImg(this)">
选择图片
</label>
<div class="close-tailoring" onclick="closeTailor(this)">×</div>
</div>
<div class="tailoring-content-two">
<div class="tailoring-box-parcel">
<img id="tailoringImg">
</div>
<div class="preview-box-parcel">
<p>图片预览:</p>
<div class="square previewImg"></div>
<div class="circular previewImg"></div>
</div>
</div>
<div class="tailoring-content-three">
<button class="l-btn cropper-reset-btn">复位</button>
<button class="l-btn cropper-rotate-btn">旋转</button>
<button class="l-btn cropper-scaleX-btn">换向</button>
<button class="l-btn sureCut" id="sureCut">确定</button>
</div>
</div>
</div>
<!--图片裁剪框 end-->
<script src="js/jquery-3.1.0.min.js"></script>
<script src="js/cropper.min.js"></script>
<script>
//弹出框水平垂直居中
(window.onresize = function () {
var win_height = $(window).height();
var win_width = $(window).width();
if (win_width <= 768){
$(".tailoring-content").css({
"top": (win_height - $(".tailoring-content").outerHeight())/2,
"left": 0
});
}else{
$(".tailoring-content").css({
"top": (win_height - $(".tailoring-content").outerHeight())/2,
"left": (win_width - $(".tailoring-content").outerWidth())/2
});
}
})();
//弹出图片裁剪框
$("#replaceImg").on("click",function () {
$(".tailoring-container").toggle();
});
//图像上传
function selectImg(file) {
if (!file.files || !file.files[0]){
return;
}
var reader = new FileReader();
reader.onload = function (evt) {
var replaceSrc = evt.target.result;
//更换cropper的图片
$('#tailoringImg').cropper('replace', replaceSrc,false);//默认false,适应高度,不失真
}
reader.readAsDataURL(file.files[0]);
}
//cropper图片裁剪
$('#tailoringImg').cropper({
aspectRatio: 1/1,//默认比例
preview: '.previewImg',//预览视图
guides: false, //裁剪框的虚线(九宫格)
autoCropArea: 0.5, //0-1之间的数值,定义自动剪裁区域的大小,默认0.8
movable: false, //是否允许移动图片
dragCrop: true, //是否允许移除当前的剪裁框,并通过拖动来新建一个剪裁框区域
movable: true, //是否允许移动剪裁框
resizable: true, //是否允许改变裁剪框的大小
zoomable: false, //是否允许缩放图片大小
mouseWheelZoom: false, //是否允许通过鼠标滚轮来缩放图片
touchDragZoom: true, //是否允许通过触摸移动来缩放图片
rotatable: true, //是否允许旋转图片
crop: function(e) {
// 输出结果数据裁剪图像。
}
});
//旋转
$(".cropper-rotate-btn").on("click",function () {
$('#tailoringImg').cropper("rotate", 45);
});
//复位
$(".cropper-reset-btn").on("click",function () {
$('#tailoringImg').cropper("reset");
});
//换向
var flagX = true;
$(".cropper-scaleX-btn").on("click",function () {
if(flagX){
$('#tailoringImg').cropper("scaleX", -1);
flagX = false;
}else{
$('#tailoringImg').cropper("scaleX", 1);
flagX = true;
}
flagX != flagX;
});
//裁剪后的处理
$("#sureCut").on("click",function () {
if ($("#tailoringImg").attr("src") == null ){
return false;
}else{
var cas = $('#tailoringImg').cropper('getCroppedCanvas');//获取被裁剪后的canvas
var base64url = cas.toDataURL('image/png'); //转换为base64地址形式
$("#finalImg").prop("src",base64url);//显示为图片的形式
//关闭裁剪框
closeTailor();
}
});
//关闭裁剪框
function closeTailor() {
$(".tailoring-container").toggle();
}
</script>
</body>
</html>
猜你喜欢
- 2024-09-10 Python奇淫技巧之自动登录哔哩哔哩(解决滑块验证)
- 2024-09-10 html转image 保存到zip(html保存成图片)
- 2024-09-10 前端上传图片被旋转的处理方案(nba常规赛录像高清回放)
- 2024-09-10 Day 3 学习Canvas这一篇文章就够了
- 2024-09-10 JavaScript实现浏览器本地的图像移动、大小调整和裁剪
- 2024-09-10 微信内置浏览器动态生成二维码并长按识别
- 2024-09-10 Python奇淫技巧之自动登录哔哩哔哩
- 2024-09-10 JSPDF + html2canvas A4分页截断(html做分页)
- 2024-09-10 vue手把手教学~搭建web聊天室(vue简单聊天室)
- 2024-09-10 前端:从零实现一款可视化图片编辑器
- 1509℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 526℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 492℃MySQL service启动脚本浅析(r12笔记第59天)
- 472℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 469℃启用MySQL查询缓存(mysql8.0查询缓存)
- 449℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 429℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 426℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)