剪裁正方形图片cropper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | <!DOCTYPE html> <html lang= "zh-cn" > <head> <meta charset= "UTF-8" > <title>裁剪图片</title> <link href= "https://cdn.bootcss.com/cropper/3.1.3/cropper.min.css" rel= "stylesheet" > <!--<link href= "https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel= "stylesheet" >--> <style> .cropper-view-box{ border-radius: 50%; } #photo { max-width: 100%; } .img-preview { width: 100px; height: 100px; overflow: hidden;border-radius: 50%; } button { margin-top:10px; } #result { width: 150px; height: 150px; } </style> </head> <body> <div class = "container" > <div class = "row" > <div class = "col-sm-12 text-center" > <label for = "input" class = "btn btn-danger" id= "" > <span>选择图片</span> <input type= "file" id= "input" class = "sr-only" > </label> </div> </div> <div class = "" > <div class = "" style= "width:50%" > <img src= "" id= "photo" > </div> <div class = "" > <div> <p> 预览(100*100): </p> <div class = "img-preview" > </div> </div> <button class = "" onclick= "crop()" >裁剪图片</button> <div> <br/> <p> 结果: </p> <img src= "" alt= "裁剪结果" id= "result" > </div> </div> </div> </div> <!-- Scripts --> <script src= "https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" ></script> <script src= "https://cdn.bootcss.com/cropper/3.1.3/cropper.min.js" ></script> <!--<script src= "https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" ></script>--> <script> // 修改自官方demo的js var initCropper = function (img, input){ var $image = img; var options = { aspectRatio: 1, // 纵横比 viewMode: 2, preview: '.img-preview' // 预览图的class名 }; $image.cropper(options); var $inputImage = input; var uploadedImageURL; if (URL) { // 给input添加监听 $inputImage.change(function () { var files = this .files; var file; if (!$image.data( 'cropper' )) { return ; } if (files && files.length) { file = files[0]; // 判断是否是图像文件 if (/^image\/\w+$/.test(file.type)) { // 如果URL已存在就先释放 if (uploadedImageURL) { URL.revokeObjectURL(uploadedImageURL); } uploadedImageURL = URL.createObjectURL(file); // 销毁cropper后更改src属性再重新创建cropper $image.cropper( 'destroy' ).attr( 'src' , uploadedImageURL).cropper(options); $inputImage.val( '' ); } else { window.alert( '请选择一个图像文件!' ); } } }); } else { $inputImage.prop( 'disabled' , true ).addClass( 'disabled' ); } } var crop = function(){ var $image = $( '#photo' ); var $target = $( '#result' ); $image.cropper( 'getCroppedCanvas' ,{ width:300, // 裁剪后的长宽 height:300 }).toBlob(function(blob){ // 裁剪后将图片放到指定标签 $target.attr( 'src' , URL.createObjectURL(blob)); }); } $(function(){ initCropper($( '#photo' ),$( '#input' )); }); </script> </body> </html> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类