基于layui+cropper.js实现上传图片的裁剪功能的方法(3)
cropper-jquery-js
备注:cropper默认裁剪后的图片格式为png格式,如果需要自定义上传图片的格式,可以参考下面这段代码,如果不需要请直接忽略
本文使用的是canvas这种方式,借助的是cropper.js实现图片的裁剪。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <meta name="renderer" content="webkit"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <link rel="stylesheet" href="{{static_url('layui/css/layui.css')}}" media="all"> <link rel="stylesheet" href="{{static_url('cropper/cropper.css')}}"> <style> #choice-btn { display: inline-block; height: 38px; line-height: 38px; padding: 0 18px; background-color: #009688; color: #fff; white-space: nowrap; text-align: center; font-size: 14px; border: none; border-radius: 2px; cursor: pointer; } </style> </head> <body> <div style="margin: 20px;"> <div style="margin-bottom:30px;"> <label for="cropper_imgUpload"> <span id="choice-btn"><i></i>选择图片</span> </label> <input type="file" id="cropper_imgUpload" name="file" style="display:none" accept="image/*"> </div> <div class="layui-row layui-col-space15"> <div> <div id="readyimg" style="height: 360px;width: 100%;border: 1px dashed black;background-color: rgb(247, 247, 247);"> <img id="img" src=https://www.ym97.com/wenku/cssm"" alt=https://www.ym97.com/wenku/cssm""> </div> </div> <div> <div>预览:</div> <div id="img-preview" style="width: 180px;height: 120px;overflow: hidden;border: 1px dashed black;"> </div> </div> </div> <div class="layui-row layui-hide oper-btn" style="margin-top: 20px;"> <div> <button type="button" class="layui-btn layui-icon layui-icon-left" cropper-event="rotate" data-option="-15" title="Rotate -90 degrees"> 向左旋转 </button> <button type="button" class="layui-btn layui-icon layui-icon-right" cropper-event="rotate" data-option="15" title="Rotate 90 degrees"> 向右旋转 </button> <button type="button" class="layui-btn layui-icon layui-icon-refresh" cropper-event="reset" title="reset-image">重置图片 </button> </div> <div class="layui-col-xs2 layui-col-xs-offset1"> <button type="button" class="layui-btn layui-btn-fluid" id="keep-save" cropper-event="confirmSave">保存修改 </button> </div> </div> </div> <script src="{{static_url('layui/layui.js')}}" charset="utf-8"></script> <script> layui.config({ base: '/static/cropper/' }).extend({ cropper: 'cropper' }).use(['element', 'layer', 'cropper', 'jquery', 'jqcropper'], function () { var element = layui.element; var $ = layui.jquery; var layer = layui.layer; var cropper = layui.cropper; var imageEle = $("#img") , preview = '#img-preview' , file = $("input[name='file']") , uploadImageMaxSize = 2048 //文件上传大小限制 , options = { aspectRatio: 3 / 2, viewMode: 1, preview: preview, dragMode: 'none', responsive: false, restore: false // cropBoxMovable:false, // cropBoxResizable:false, }; // 找到上传图片的input标签绑定change事件 $("#cropper_imgUpload").change(function () { $(".oper-btn").removeClass("layui-hide"); // 1. 创建一个读取文件的对象 var fileReader = new FileReader(); fileReader.readAsDataURL(this.files[0]); fileReader.onload = function () { // 2. 等上一步读完文件之后才 把图片加载到img标签中 imageEle.attr("src", fileReader.result); //图片链接(base64) imageEle.cropper(options); }; }); $(".layui-btn").on('click', function () { var event = $(this).attr("cropper-event"); //监听确认保存图像 if (event === 'confirmSave') { imageEle.cropper("getCroppedCanvas").toBlob(function (blob) { var filesize = (blob["size"] / 1024).toFixed(2); if (filesize < uploadImageMaxSize) { var formData = new FormData(); formData.append('file', blob); $.ajax({ method: "post", url: '/web/api/upload/upload?option={{option}}', //用于文件上传的服务器端请求地址 data: formData, processData: false, contentType: false, success: function (response) { if (response.code === 0) { // 代开裁剪页面的回调函数 parent.cropperCallback(response.data, '{{index}}'); layer.msg("上传成功", { icon: 1, time: 2500 // 默认三秒 }, function () { // 回调关闭弹出层 var index = parent.layer.getFrameIndex(window.name); parent.layer.close(index); }); $(".canvanslog").attr("src", response.data[0].fp_show); } else { layer.alert(response.msg, {icon: 2}); } }, error: function (response) { layer.alert("网络异常", {icon: 2}); } }); } else { layer.alert("上传图片大小不超过" + uploadImageMaxSize + "KB", {icon: 2}) } }); } else if (event === 'rotate') { //监听旋转 var option = $(this).attr('data-option'); imageEle.cropper('rotate', option); } else if (event === 'reset') { //重设图片 imageEle.cropper('reset'); } }); }); </script> </body> </html> cropper-html父页面的回调
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/cssm/5251.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
其中border-left决定了底部直
时间:2021-01-23
-
当你自己回头来看你写的
时间:2021-01-23
-
④格式标签 粗体:b/b 斜
时间:2021-01-23
-
我们直接看代码: !DOCTY
时间:2021-01-23
-
这里就是吐槽的IE6!) 图
时间:2021-01-23
-
假设我们的HTML代码如下:
时间:2021-01-23
-
那么使用 CSS3 新增的选择
时间:2021-01-23
-
scaleGlassRectangle.y
时间:2021-01-23
热门文章
-
可以加我的HTML5前端交流群111645711 CSS源码
时间:2021-01-15
-
就可以对子元素进行 3D 变形操作了
时间:2021-01-12
-
用css让一个容器水平垂直
时间:2021-01-12
-
而没有设置高度
时间:2021-01-19
-
canvas与html5实现视频截图成果
时间:2021-01-19
-
所以通常不需要发送
时间:2021-01-19
-
我们尝试一下更新一下HTML结构
时间:2021-01-23
-
scaleGlassRectangle.y
时间:2021-01-23
-
HTML5生拖放实例分析
时间:2021-01-12
-
在全局:root{ }伪类中定义了一个 CSS 变量
时间:2021-01-21
