项目中裁剪图片效果

代码部分:(将上传部分 封装成一个组件直接调用当前组件)

  1. <template>
  2. <div id="demo">
  3. <!-- 遮罩层 -->
  4. <div class="father" v-show="panel">
  5. <div class="container">
  6. <div id="cropper" style="margin-top:1%;margin-left: 1%;margin-right: 1%;height: 85%;margin-bottom: 1%">
  7. <img id="image" :src="url" alt="Picture">
  8. </div>
  9. <div>
  10. <div style="float: right;margin-right: 1%">
  11. <el-button icon="el-icon-zoom-out" @click="zoom(-1)" size="mini" circle></el-button>
  12. <el-button icon="el-icon-zoom-in" @click="zoom(1)" size="mini" circle></el-button>
  13. <el-button icon="el-icon-refresh" @click="rotate" size="mini" circle></el-button>
  14. <el-button type="success" icon="el-icon-check" @click="crop" size="mini" circle></el-button>
  15. <el-button type="danger" icon="el-icon-close" @click="panel=false" size="mini" circle></el-button>
  16. </div>
  17. </div>
  18. </div>
  19. </div>
  20. <div >
  21. <div >
  22. <input type="file" id="change" accept="image" @change="change" style="display:none;">
  23. <div class="show"
  24. v-on:mouseover="addClassload"
  25. v-on:mouseout="removeClassload"
  26. @click="upload"
  27. :style="'backgroundImage:url(http://d.fumaqi.com'+params.logo+');border: 1px dashed '+color">
  28. <i class="el-icon-plus i" :style="'color: '+color"></i>
  29. </div>
  30. </div>
  31. </div>
  32. </div>
  33.  
  34. </template>
  35.  
  36. <script>
  37. // import {test} from '@/api/psych'
  38. import { uploadImg } from '@/api/index'
  39. import Cropper from 'cropperjs'
  40. export default {
  41. components: {
  42.  
  43. },
  44. props:["params"],
  45. data () {
  46. return {
  47. headerImage: '',
  48. picValue:'',
  49. cropper:'',
  50. croppable:false,
  51. panel:false,
  52. url:'',
  53. color:"#d9d9d9",
  54. current:0,
  55.  
  56. }
  57. },
  58. mounted () {
  59. //初始化这个裁剪框
  60. var self = this;
  61. var image = document.getElementById('image');
  62. this.cropper = new Cropper(image, {
  63. aspectRatio: 1,
  64. viewMode: 1,
  65. zoomOnWheel:false,//是否允许通过鼠标滚轮来缩放图片
  66. background:true,//是否在容器上显示网格背景
  67. rotatable:true,//是否允许旋转图片
  68. ready: function () {
  69. self.croppable = true;
  70. }
  71. });
  72. console.log(133)
  73. console.log(this.params.logo)
  74. this.headerImage = this.params.logo
  75. },
  76. methods: {
  77. addClassload(){
  78. this. color="#1b95e0"
  79. },
  80. removeClassload(){
  81. this. color="#d9d9d9"
  82. },
  83. //点击按钮自动执行选择文件事件
  84. upload(){
  85. this.url='',
  86. this.current=0;
  87. document.getElementById("change").value=null;
  88. document.getElementById("change").click();
  89. },
  90. //旋转
  91. rotate(){
  92. //alert(this.cropper.image)
  93. this.current = (this.current+90)%360;
  94. this.cropper.rotate(this.current);
  95. },
  96. //缩放
  97. zoom(num){
  98. num = num || 1;
  99. this.cropper.zoom(num);
  100. },
  101.  
  102. getObjectURL (file) {
  103. var url = null ;
  104. if (window.createObjectURL!=undefined) { // basic
  105. url = window.createObjectURL(file) ;
  106. } else if (window.URL!=undefined) { // mozilla(firefox)
  107. url = window.URL.createObjectURL(file) ;
  108. } else if (window.webkitURL!=undefined) { // webkit or chrome
  109. url = window.webkitURL.createObjectURL(file) ;
  110. }
  111. return url ;
  112. },
  113. change (e) {
  114. let files = e.target.files || e.dataTransfer.files;
  115. if (!files.length) return;
  116. this.panel = true;
  117. this.picValue = files[0];
  118. this.url = this.getObjectURL(this.picValue);
  119. //每次替换图片要重新得到新的url
  120. if(this.cropper){
  121. this.cropper.replace(this.url);
  122. }
  123. this.panel = true;
  124. },
  125. crop () {
  126. this.panel = false;
  127. var croppedCanvas;
  128. var roundedCanvas;
  129.  
  130. if (!this.croppable) {
  131. return;
  132. }
  133. // Crop
  134. croppedCanvas = this.cropper.getCroppedCanvas();
  135. console.log(this.cropper)
  136. // Round
  137. /*截取圆形
  138. roundedCanvas = this.getRoundedCanvas(croppedCanvas);
  139. this.headerImage = roundedCanvas.toDataURL();
  140. */
  141. //方形
  142. this.headerImage = croppedCanvas.toDataURL();
  143.  
  144. var gettype=Object.prototype.toString
  145. this.postImg()
  146.  
  147. },
  148. dataURLtoFile (dataurl, filename = 'file') {
  149. let arr = dataurl.split(',')
  150. let mime = arr[0].match(/:(.*?);/)[1]
  151. let suffix = mime.split('/')[1]
  152. let bstr = atob(arr[1])
  153. let n = bstr.length
  154. let u8arr = new Uint8Array(n)
  155. while (n--) {
  156. u8arr[n] = bstr.charCodeAt(n)
  157. }
  158. return new File([u8arr], `${filename}.${suffix}`, {type: mime})
  159. },
  160. /* // 截取圆形
  161. getRoundedCanvas (sourceCanvas) {
  162. var canvas = document.createElement('canvas');
  163. var context = canvas.getContext('2d');
  164. var width = sourceCanvas.width;
  165. var height = sourceCanvas.height;
  166. canvas.width = width;
  167. canvas.height = height;
  168. context.imageSmoothingEnabled = true;
  169. context.drawImage(sourceCanvas, 0, 0, width, height);
  170. context.globalCompositeOperation = 'destination-in';
  171. context.beginPath();
  172. context.arc(width / 2, height / 2, Math.min(width, height) / 2, 0, 2 * Math.PI, true);
  173. context.fill();
  174. return canvas;
  175. },*/
  176. postImg () {
  177. //这边写图片的上传
  178. var formData = new FormData();
  179. formData.append("avatar",this.dataURLtoFile(this.headerImage));
  180. uploadImg(formData).then(response => {
  181. if(response.data.code == 0){
  182. // alert("成功");
  183. this.params.logo = response.data.data
  184. this.$emit('changeImg',response.data.data)
  185. }else{
  186. this.$toast(res.data.msg)
  187. }
  188.  
  189. }).catch((res) => {
  190. this.$toast('网路异常')
  191. })
  192.  
  193. }
  194. }
  195. }
  196.  
  197. </script>
  198.  
  199. <style>
  200. *{
  201. margin: 0;
  202. padding: 0;
  203. }
  204. .father{
  205. background-color: rgba(0,0,0,0.5);
  206. position: fixed;
  207. left:0px;
  208. top:0px;
  209. width:100%;
  210. height:100%;
  211. z-index: 33;
  212. }
  213. .i{
  214. font-size: 28px;
  215. color: #8c939d;
  216. width: 100px;
  217. height: 100px;
  218. line-height: 100px;
  219. text-align: center;
  220. }
  221. #demo .show {
  222. border: 1px dashed #d9d9d9;
  223. border-radius: 10%;
  224. width: 100px;
  225. height: 100px;
  226. cursor: pointer;
  227. position: relative;
  228. overflow: hidden;
  229. display: inline-block;
  230. text-align: center;
  231. outline: none;
  232. background-position: center center;
  233. background-repeat: no-repeat;
  234. background-size: cover;
  235. }
  236. #demo .container {
  237. z-index: 99;
  238. height: 300px;
  239. width: 100%;
  240.  
  241. position: fixed;
  242. /*padding-top: 60px;*/
  243. /* left: 35%; */
  244. top: 25%;
  245.  
  246. background-color: white;
  247. }
  248. #demo #image {
  249. max-width: 100%;
  250.  
  251. }
  252. /* .cropper-view-box,.cropper-face {
  253. border-radius: 100%; 圆形截图设置
  254. }*/
  255. /*!
  256. * Cropper.js v1.0.0-rc
  257. * https://github.com/fengyuanchen/cropperjs
  258. *
  259. * Copyright (c) 2017 Fengyuan Chen
  260. * Released under the MIT license
  261. *
  262. * Date: 2017-03-25T12:02:21.062Z
  263. */
  264. .cropper-container {
  265. font-size: 0;
  266. line-height: 0;
  267. position: relative;
  268. -webkit-user-select: none;
  269. -moz-user-select: none;
  270. -ms-user-select: none;
  271. user-select: none;
  272. direction: ltr;
  273. -ms-touch-action: none;
  274. touch-action: none
  275. }
  276.  
  277. .cropper-container img {
  278. /* Avoid margin top issue (Occur only when margin-top <= -height) */
  279. display: block;
  280. min-width: 0 !important;
  281. max-width: none !important;
  282. min-height: 0 !important;
  283. max-height: none !important;
  284. width: 100%;
  285. height: 100%;
  286. image-orientation: 0deg
  287. }
  288. .cropper-wrap-box,
  289. .cropper-canvas,
  290. .cropper-drag-box,
  291. .cropper-crop-box,
  292. .cropper-modal {
  293. position: absolute;
  294. top: 0;
  295. right: 0;
  296. bottom: 0;
  297. left: 0;
  298. }
  299. .cropper-wrap-box {
  300. overflow: hidden;
  301. }
  302. .cropper-drag-box {
  303. opacity: 0;
  304. background-color: #fff;
  305. }
  306. .cropper-modal {
  307. opacity: .5;
  308. background-color: #000;
  309. }
  310. .cropper-view-box {
  311. display: block;
  312. overflow: hidden;
  313. width: 100%;
  314. height: 100%;
  315. outline: 1px solid #39f;
  316. outline-color: rgba(51, 153, 255, 0.75);
  317. }
  318.  
  319. .cropper-dashed {
  320. position: absolute;
  321. display: block;
  322. opacity: .5;
  323. border: 0 dashed #eee
  324. }
  325. .cropper-dashed.dashed-h {
  326. top: 33.33333%;
  327. left: 0;
  328. width: 100%;
  329. height: 33.33333%;
  330. border-top-width: 1px;
  331. border-bottom-width: 1px
  332. }
  333. .cropper-dashed.dashed-v {
  334. top: 0;
  335. left: 33.33333%;
  336. width: 33.33333%;
  337. height: 100%;
  338. border-right-width: 1px;
  339. border-left-width: 1px
  340. }
  341. .cropper-center {
  342. position: absolute;
  343. top: 50%;
  344. left: 50%;
  345. display: block;
  346. width: 0;
  347. height: 0;
  348. opacity: .75
  349. }
  350. .cropper-center:before,
  351. .cropper-center:after {
  352. position: absolute;
  353. display: block;
  354. content: ' ';
  355. background-color: #eee
  356. }
  357. .cropper-center:before {
  358. top: 0;
  359. left: -3px;
  360. width: 7px;
  361. height: 1px
  362. }
  363. .cropper-center:after {
  364. top: -3px;
  365. left: 0;
  366. width: 1px;
  367. height: 7px
  368. }
  369. .cropper-face,
  370. .cropper-line,
  371. .cropper-point {
  372. position: absolute;
  373. display: block;
  374. width: 100%;
  375. height: 100%;
  376. opacity: .1;
  377. }
  378. .cropper-face {
  379. top: 0;
  380. left: 0;
  381. background-color: #fff;
  382. }
  383. .cropper-line {
  384. background-color: #39f
  385. }
  386. .cropper-line.line-e {
  387. top: 0;
  388. right: -3px;
  389. width: 5px;
  390. cursor: e-resize
  391. }
  392. .cropper-line.line-n {
  393. top: -3px;
  394. left: 0;
  395. height: 5px;
  396. cursor: n-resize
  397. }
  398. .cropper-line.line-w {
  399. top: 0;
  400. left: -3px;
  401. width: 5px;
  402. cursor: w-resize
  403. }
  404. .cropper-line.line-s {
  405. bottom: -3px;
  406. left: 0;
  407. height: 5px;
  408. cursor: s-resize
  409. }
  410. .cropper-point {
  411. width: 5px;
  412. height: 5px;
  413.  
  414. opacity: .75;
  415. background-color: #39f
  416. }
  417. .cropper-point.point-e {
  418. top: 50%;
  419. right: -3px;
  420. margin-top: -3px;
  421. cursor: e-resize
  422. }
  423. .cropper-point.point-n {
  424. top: -3px;
  425. left: 50%;
  426. margin-left: -3px;
  427. cursor: n-resize
  428. }
  429. .cropper-point.point-w {
  430. top: 50%;
  431. left: -3px;
  432. margin-top: -3px;
  433. cursor: w-resize
  434. }
  435. .cropper-point.point-s {
  436. bottom: -3px;
  437. left: 50%;
  438. margin-left: -3px;
  439. cursor: s-resize
  440. }
  441. .cropper-point.point-ne {
  442. top: -3px;
  443. right: -3px;
  444. cursor: ne-resize
  445. }
  446. .cropper-point.point-nw {
  447. top: -3px;
  448. left: -3px;
  449. cursor: nw-resize
  450. }
  451. .cropper-point.point-sw {
  452. bottom: -3px;
  453. left: -3px;
  454. cursor: sw-resize
  455. }
  456. .cropper-point.point-se {
  457. right: -3px;
  458. bottom: -3px;
  459. width: 20px;
  460. height: 20px;
  461. cursor: se-resize;
  462. opacity: 1
  463. }
  464. @media (min-width: 768px) {
  465. .cropper-point.point-se {
  466. width: 15px;
  467. height: 15px
  468. }
  469. }
  470. @media (min-width: 992px) {
  471. .cropper-point.point-se {
  472. width: 10px;
  473. height: 10px
  474. }
  475. }
  476. @media (min-width: 1200px) {
  477. .cropper-point.point-se {
  478. width: 5px;
  479. height: 5px;
  480. opacity: .75
  481. }
  482. }
  483. .cropper-point.point-se:before {
  484. position: absolute;
  485. right: -50%;
  486. bottom: -50%;
  487. display: block;
  488. width: 200%;
  489. height: 200%;
  490. content: ' ';
  491. opacity: 0;
  492. background-color: #39f
  493. }
  494. .cropper-invisible {
  495. opacity: 0;
  496. }
  497. .cropper-bg {
  498. background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAAA3NCSVQICAjb4U/gAAAABlBMVEXMzMz////TjRV2AAAACXBIWXMAAArrAAAK6wGCiw1aAAAAHHRFWHRTb2Z0d2FyZQBBZG9iZSBGaXJld29ya3MgQ1M26LyyjAAAABFJREFUCJlj+M/AgBVhF/0PAH6/D/HkDxOGAAAAAElFTkSuQmCC');
  499. }
  500. .cropper-hide {
  501. position: absolute;
  502. display: block;
  503. width: 0;
  504. height: 0;
  505. }
  506. .cropper-hidden {
  507. display: none !important;
  508. }
  509. .cropper-move {
  510. cursor: move;
  511. }
  512. .cropper-crop {
  513. cursor: crosshair;
  514. }
  515. .cropper-disabled .cropper-drag-box,
  516. .cropper-disabled .cropper-face,
  517. .cropper-disabled .cropper-line,
  518. .cropper-disabled .cropper-point {
  519. cursor: not-allowed;
  520. }
  521. </style>

vue项目使用cropperjs制作图片剪裁,压缩组件的更多相关文章

  1. Vue项目打包后背景图片路径错误

    vue项目打包之后背景图片出错的解决方案如下: 1,找到 config->index.js里面,如下修改 默认配置: env: require('./prod.env'), index: pat ...

  2. 详解如何在vue项目中引入饿了么elementUI组件

    在开发的过程之中,我们也经常会使用到很多组件库:vue 常用ui组件库:https://blog.csdn.net/qq_36538012/article/details/82146649 今天具体说 ...

  3. 配置vue项目将打包后图片文件的引用路径改为cdn路径?

    vue cli3项目, 需求: 图片文件打包时, 将项目内的所有图片文件的引用地址改为cdn路径 vue cli3的默认配置下, 打包后图片使用的是相对路径, 例如打包后项目内图片引用路径为 img/ ...

  4. vue项目使用Echarts制作项目工期甘特图

    目录 1,前言 2,布局和数据部分 3,制作甘特图 1,前言 项目迭代过程中,碰上一个需求,要求用甘特图的方式显示项目的工期进度,开完会我赶紧搜索一下甘特图是啥东东,大概了解之后,做出了如下样式 Ec ...

  5. vue中使用cropperjs进行图片裁剪上传

    下面代码直接就可以复制使用了,但是需要在本地下个cropperjs,下载命令:npm install cropperjs --save-dev <template> <div id= ...

  6. vue项目webpack打包后图片路径错误

    首先项目是vue-cli搭建的,项目结构如下: 然后发现在css里写的图片引用地址在开发时正常显示,但在打包扔上服务器之后报错 报的是404,路径前面多了/static/css,不知道为啥. 在自己慢 ...

  7. vue项目build打包后图片路径不对导致图片空白不显示问题解决方法

    1.在上篇文章src配置及引入的基础上修改项目配置: 文章链接地址:https://www.cnblogs.com/hsl-shiliang/p/10333022.html. 2.具体配置过程如图: ...

  8. vue项目中编写一个图片预览的公用组件

    今天产品提出了一个查看影像的功能需求. 在查看单据的列表中,有一列是影像字段,一开始根据单据号调用接口查看是否有图片附件,如果有则弹出一个全屏的弹出层,如果没有给出提示.而且,从列表进入详情之后,附件 ...

  9. 保存头像- vue项目-base64字符串转图片

    <img :onerror="errpic" class="customerHead" :src="param.customerHead&quo ...

随机推荐

  1. button标签与input type=button标签使用的差异

    button标签和input type=button标签都是html文档中用来表示按钮属性的元素,不过他们在布局和实际使用功能中存在一些差异. 下面将项目中遇到的一些总结如下: 1.属性和布局差异. ...

  2. StandardWrapper

    Tomcat中有四种类型的Servlet容器,分别是 Engine.Host.Context.Wrapper,每个Wrapper实例表示一个具体的Servlet定义,StandardWrapper就是 ...

  3. java中jar包的使用

    1:在工程目录下新建一个文件lib  导入jar包在里面 2:在jar包上右键  ——>build path——>add to bulid path OK!

  4. python+minicap(二)

    一.push文件至手机中 minicap 的使用有很强的针对性,针对不同架构的CPU和SDK制作了不同的 "minicap" 和 "minicap.so" 文件 ...

  5. golang(4):函数 & 数组 & 切片 & map & 锁

    内置函数 // 1. close:主要用来关闭channel // 2. len:用来求长度,比如string.array.slice.map.channel // 3. new:用来分配内存,主要用 ...

  6. centos7网络配置脚本

    如下参数根据实际情况修改 #!/bin/bash #设置网络环境 sed -i -e 's|BOOTPROTO=dhcp|BOOTPROTO=static|' /etc/sysconfig/netwo ...

  7. python多线程之_thread

    多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进 ...

  8. 黑马java课程视频java学习视频

    资料获取方式,关注公总号RaoRao1994,查看往期精彩-所有文章,即可获取资源下载链接 更多资源获取,请关注公总号RaoRao1994

  9. python学习之路入门篇

    本文是up学习python过程中遇到的一些问题及总结归纳,本小节是入门篇. python基本语法 循环.分支不多赘述. 模块:一个.py文件就是一个模块. 文件和异常 模式 含义解释 “r” 读模式 ...

  10. Jenkins+GitHub 项目环境搭建(一)

    安装Jenkins yum install -y java-1.8.0-openjdk wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkin ...