vue+图片裁剪vue-cropper以及api

前言:

        因为项目需要,需要实现一个上传完图片,对图片进行二次处理的需求,就使用了vue-cropper来实现这个功能,总的来说还是感觉非常不错的软件,这里分享下我的使用方法,以及vue-cropper的api(见最下)

实现效果:

实现步骤:

步骤一:项目中安装 vue-cropper

npm install vue-cropper
 
 

步骤二:在main.js中注册(经测试,单独页面注册可能会出不来)


 
 
  1. import VueCropper from 'vue-cropper'
  2. Vue.use(VueCropper)

步骤三:在页面使用,这里直接提供封装好的组件 vue-cropper.vue(源代码在下面)

1、template部分

2、js部分

源代码:


 
 
  1. <template>
  2. <div class="cropper-component">
  3. <!-- 工具箱部分 -->
  4. <div class="btn-box">
  5. <label class="btn" for="uploads">选择图片 </label>
  6. <input type="file" id="uploads" :value="imgFile"
  7. style= "position:absolute; clip:rect(0 0 0 0);width: 1px;"
  8. accept= "image/png, image/jpeg, image/gif, image/jpg"
  9. @ change= "uploadImg($event,'blob', 1)">
  10. <Button type="primary" icon="ios-add" title="放大" @click="changeScale(1)"/>
  11. <Button type="primary" icon="ios-remove" title="缩小" @click="changeScale(-1)"/>
  12. <Button type="primary" title="左旋转" @click="rotateLeft"> <span style="font-weight: 500;"></span> </Button>
  13. <Button type="primary" title="右旋转" @click="rotateRight"> <span style="font-weight: 500;"></span> </Button>
  14. <!-- <Button type="primary" title="下载" @click="down('blob')"><span style="font-weight: 500;">↓</span></Button> -->
  15. <!-- <div class="btn" @click="finish('blob')">上传头像</div> -->
  16. </div>
  17. <!-- 展示内容部分 -->
  18. <div class="show_box">
  19. <!-- 展示选中图片 -->
  20. <div class="cropper">
  21. <vueCropper
  22. ref= "cropper"
  23. :img= "option.img"
  24. :outputSize= "option.size"
  25. :outputType= "option.outputType"
  26. :info= "option.info"
  27. :full= "option.full"
  28. :canMove= "option.canMove"
  29. :canMoveBox= "option.canMoveBox"
  30. :original= "option.original"
  31. :autoCrop= "option.autoCrop"
  32. :autoCropWidth= "option.autoCropWidth"
  33. :autoCropHeight= "option.autoCropHeight"
  34. :fixedBox= "option.fixedBox"
  35. @ realTime= "realTime"
  36. @ imgLoad= "imgLoad"
  37. > </vueCropper>
  38. </div>
  39. <!-- 展示缩略图 -->
  40. <div class="preview-box" v-if="previews.url">
  41. <div>预览: </div>
  42. <div :style="previews.div" class="preview">
  43. <img :src="previews.url" :style="previews.img"/>
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. </template>
  49. <script>
  50. export default {
  51. name: 'cropper',
  52. data() {
  53. return {
  54. //剪切图片上传
  55. crap: false,
  56. previews: {},
  57. option: {
  58. img: '', // 裁剪图片的地址
  59. info: true, // 裁剪框的大小信息
  60. outputSize: 1, // 剪切后的图片质量(0.1-1)
  61. full: true, // 输出原图比例截图 props名full
  62. outputType: 'jpg', // 裁剪生成额图片的格式
  63. canMove: true, // 能否拖动图片
  64. original: false, // 上传图片是否显示原始宽高
  65. canMoveBox: true, // 能否拖动截图框
  66. autoCrop: true, // 是否默认生成截图框
  67. autoCropWidth: 150,
  68. autoCropHeight: 150,
  69. fixedBox: true // 截图框固定大小
  70. },
  71. fileName: '', // 本机文件地址
  72. downImg: '#',
  73. imgFile: '',
  74. uploadImgRelaPath: '', // 上传后的图片的地址(不带服务器域名)
  75. }
  76. },
  77. methods: {
  78. // 放大/缩小
  79. changeScale(num) {
  80. num = num || 1;
  81. this.$refs.cropper.changeScale(num);
  82. },
  83. // 坐旋转
  84. rotateLeft() {
  85. this.$refs.cropper.rotateLeft();
  86. },
  87. // 右旋转
  88. rotateRight() {
  89. this.$refs.cropper.rotateRight();
  90. },
  91. // 上传图片(点击上传按钮)
  92. finish(type) {
  93. let formData = new FormData();
  94. // 输出
  95. if (type === 'blob') {
  96. this.$refs.cropper.getCropBlob( (data) => {
  97. let img = window.URL.createObjectURL(data);
  98. formData.append( 'images', data);
  99. this.$emit( 'postFile',formData);
  100. })
  101. } else {
  102. this.$refs.cropper.getCropData( (data) => {
  103. formData.append( 'images', data);
  104. this.$emit( 'postFile',formData);
  105. })
  106. }
  107. },
  108. // 实时预览函数
  109. realTime(data) {
  110. this.previews = data;
  111. },
  112. // 下载图片
  113. down(type) {
  114. var aLink = document.createElement( 'a');
  115. aLink.download = 'author-img'; //文件名
  116. if (type === 'blob') {
  117. // 获取截图的blob数据
  118. this.$refs.cropper.getCropBlob( (data) => {
  119. this.downImg = window.URL.createObjectURL(data); //生成blob格式图片路径
  120. aLink.href = window.URL.createObjectURL(data);
  121. aLink.click();
  122. })
  123. } else {
  124. // 获取截图的base64 数据
  125. this.$refs.cropper.getCropData( (data) => {
  126. this.downImg = data;
  127. aLink.href = data;
  128. })
  129. }
  130. },
  131. // 选择本地图片
  132. uploadImg(e, type, num) { //num代表第几个
  133. // 上传图片
  134. var file = e.target.files[ 0];
  135. this.fileName = file.name;
  136. if (! /\.(jpg)$/.test(e.target.value)) {
  137. this.$Message.info( '证件照图片必须是jpg格式');
  138. return false;
  139. }
  140. let reader = new FileReader();
  141. reader.onload = (e) => {
  142. let data = ''; //生成图片地址
  143. if ( typeof e.target.result === 'object') {
  144. if(type == 'blob'){
  145. // 把Array Buffer转化为blob 如果是base64不需要
  146. data = window.URL.createObjectURL( new Blob([e.target.result]));
  147. }
  148. }
  149. else {
  150. data = e.target.result;
  151. }
  152. if (num === 1) {
  153. this.option.img = data;
  154. }
  155. }
  156. if(type == 'blob'){
  157. // 转化为blob
  158. reader.readAsArrayBuffer(file);
  159. } else{
  160. // 转化为base64
  161. reader.readAsDataURL(file);
  162. }
  163. },
  164. //图片加载的回调 imgLoad 返回结果success, error
  165. imgLoad (msg) {
  166. console.log( 'imgLoad')
  167. console.log(msg)
  168. },
  169. //刷新-清除截图-目前尚未用到
  170. refeshImg(type){
  171. if(type == 'start'){
  172. this.$refs.cropper.startCrop() //开始截图
  173. } else if(type == 'end'){
  174. this.$refs.cropper.stopCrop() //停止截图
  175. } else if(type == 'clear'){
  176. this.$refs.cropper.clearCrop() //清除截图
  177. }
  178. }
  179. }
  180. }
  181. </script>
  182. <style lang="less">
  183. .cropper-component {
  184. width: 100%;
  185. margin: 0 auto;
  186. position: relative;
  187. //工具箱部分
  188. .btn-box {
  189. margin: 20px 0;
  190. .btn {
  191. padding:0 10px;
  192. height:32px;
  193. line-height:32px;
  194. text-align: center;
  195. border-radius: 4px;
  196. background-color: #0d8b8e ;
  197. color: #fff;
  198. cursor: pointer;
  199. display: inline-block;
  200. vertical-align: bottom;
  201. }
  202. }
  203. //展示内容部分
  204. .show_box{
  205. display: flex;
  206. justify-content: space-between;
  207. // 展示选中图片
  208. .cropper {
  209. width: 260px;
  210. height: 260px;
  211. }
  212. // 展示缩略图
  213. .preview-box {
  214. top: 60px;
  215. right: 10px;
  216. .preview {
  217. width: 150px;
  218. height: 150px;
  219. // border-radius: 50%;//这里预览是否需要变成圆的
  220. border: 1px solid #ccc;
  221. background-color: #ccc;
  222. margin: 5px;
  223. overflow: hidden;
  224. }
  225. }
  226. }
  227. }
  228. </style>

API文档:

这里内置方法的话,我是可以提供可供拷贝的代码:


 
 
  1. 内置方法 通过 this.$refs.cropper 调用
  2. this.$refs.cropper.startCrop() 开始截图
  3. this.$refs.cropper.stopCrop() 停止截图
  4. this.$refs.cropper.clearCrop() 清除截图
  5. this.$refs.cropper.changeScale() 修改图片大小 正数为变大 负数变小
  6. this.$refs.cropper.getImgAxis() 获取图片基于容器的坐标点
  7. this.$refs.cropper.getCropAxis() 获取截图框基于容器的坐标点
  8. this.$refs.cropper.goAutoCrop 自动生成截图框函数
  9. this.$refs.cropper.rotateRight() 向右边旋转 90
  10. this.$refs.cropper.rotateLeft() 向左边旋转 90
  11. 图片加载的回调 imgLoad 返回结果success, error
  12. 获取截图信息
  13. this.$refs.cropper.cropW 截图框宽度
  14. this.$refs.cropper.cropH 截图框高度

完整api入口

到这里就结束了,有兴趣的朋友可以留言一起交流哈

  •                     <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count"></span>
                        </a></li>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox">
                <div class="left-message"><a href="https://blog.csdn.net/qq_41619796">
                    <img src="https://profile.csdnimg.cn/E/6/4/3_qq_41619796" class="avatar_pic" username="qq_41619796">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/qq_41619796" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">浩星</a></span>
                                            </div>
                    <div class="text"><span>发布了132 篇原创文章</span> · <span>获赞 52</span> · <span>访问量 14万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://im.csdn.net/im/main.html?userName=qq_41619796" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                        </a>
                                                            <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值