最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue使用vue-cropper實(shí)現(xiàn)圖片裁剪之單圖裁剪的步驟

 更新時(shí)間:2025年08月23日 10:28:00   作者:XUE_雪  
本文介紹在若依系統(tǒng)中使用vue-cropper實(shí)現(xiàn)固定尺寸圖片裁剪上傳的步驟,包括安裝、引入、封裝子組件、父級調(diào)用及參數(shù)接收,ruoyi.css已集成相關(guān)樣式,感興趣的朋友跟隨小編一起看看吧

vue制作的pc系統(tǒng)中(如若依系統(tǒng)),需要實(shí)現(xiàn)按照固定尺寸進(jìn)行裁剪后再進(jìn)行圖片上傳,以下代碼講述的是實(shí)現(xiàn)單張圖片裁剪上傳。

1.第一步需要安裝vue-cropper

npm install vue-cropper

2.第二步在需要的頁面進(jìn)入代碼引入

import {VueCropper} from "vue-cropper"

3.第三步封裝子組件

<template>
  <div>
    <div class="user-info-head" @click="editCropper()"><img v-bind:src="options.img" title="點(diǎn)擊上傳頭像" class="img-lg" /></div>
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body @opened="modalOpened"  @close="closeDialog">
      <el-row>
        <el-col :xs="24" :md="12" :style="{height: '350px'}">
          <vue-cropper
            ref="cropper"
            :img="options.img"
            :info="true"
            :autoCrop="options.autoCrop"
            :autoCropWidth="options.autoCropWidth"
            :autoCropHeight="options.autoCropHeight"
            :fixedBox="options.fixedBox"
            :outputType="options.outputType"
            @realTime="realTime"
            v-if="visible"
          />
        </el-col>
        <el-col :xs="24" :md="12" :style="{height: '350px'}">
          <div class="avatar-upload-preview-update">
            <img :src="previews.url" :style="previews.img" />
          </div>
        </el-col>
      </el-row>
      <br />
      <el-row>
        <el-col :lg="2" :sm="3" :xs="3">
          <el-upload action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
            <el-button size="small">
              選擇
              <i class="el-icon-upload el-icon--right"></i>
            </el-button>
          </el-upload>
        </el-col>
        <el-col :lg="{span: 1, offset: 2}" :sm="2" :xs="2">
          <el-button icon="el-icon-plus" size="small" @click="changeScale(1)"></el-button>
        </el-col>
        <el-col :lg="{span: 1, offset: 1}" :sm="2" :xs="2">
          <el-button icon="el-icon-minus" size="small" @click="changeScale(-1)"></el-button>
        </el-col>
        <el-col :lg="{span: 1, offset: 1}" :sm="2" :xs="2">
          <el-button icon="el-icon-refresh-left" size="small" @click="rotateLeft()"></el-button>
        </el-col>
        <el-col :lg="{span: 1, offset: 1}" :sm="2" :xs="2">
          <el-button icon="el-icon-refresh-right" size="small" @click="rotateRight()"></el-button>
        </el-col>
        <el-col :lg="{span: 2, offset: 6}" :sm="2" :xs="2">
          <el-button type="primary" size="small" @click="uploadImg()">提 交</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import store from "@/store"
import { VueCropper } from "vue-cropper"
import { uploadCommonImg } from "@/api/system/user"
import { debounce } from '@/utils'
export default {
  components: { VueCropper },
  props: {
    url: {
      type: String,
      default: ""
    },
  },
  data() {
    return {
      // 是否顯示彈出層
      open: false,
      // 是否顯示cropper
      visible: false,
      // 彈出層標(biāo)題
      title: "修改頭像",
      options: {
        img: '',  //裁剪圖片的地址
        autoCrop: true,             // 是否默認(rèn)生成截圖框
        autoCropWidth: 162,         // 默認(rèn)生成截圖框?qū)挾?
        autoCropHeight: 200,        // 默認(rèn)生成截圖框高度
        fixedBox: true,             // 固定截圖框大小 不允許改變
        outputType:"png",           // 默認(rèn)生成截圖為PNG格式
        filename: 'avatar'          // 文件名稱
      },
      previews: {},
      resizeHandler: null
    }
  },
  mounted() {
    this.options.img= this.url?(process.env.VUE_APP_BASE_API +this.url):''
  },
  methods: {
    // 編輯頭像
    editCropper() {
      this.open = true
    },
    // 打開彈出層結(jié)束時(shí)的回調(diào)
    modalOpened() {
      this.visible = true
      if (!this.resizeHandler) {
        this.resizeHandler = debounce(() => {
          this.refresh()
        }, 100)
      }
      window.addEventListener("resize", this.resizeHandler)
    },
    // 刷新組件
    refresh() {
      this.$refs.cropper.refresh()
    },
    // 覆蓋默認(rèn)的上傳行為
    requestUpload() {
    },
    // 向左旋轉(zhuǎn)
    rotateLeft() {
      this.$refs.cropper.rotateLeft()
    },
    // 向右旋轉(zhuǎn)
    rotateRight() {
      this.$refs.cropper.rotateRight()
    },
    // 圖片縮放
    changeScale(num) {
      num = num || 1
      this.$refs.cropper.changeScale(num)
    },
    // 上傳預(yù)處理
    beforeUpload(file) {
      if (file.type.indexOf("image/") == -1) {
        this.$modal.msgError("文件格式錯(cuò)誤,請上傳圖片類型,如:JPG,PNG后綴的文件。")
      } else {
        const reader = new FileReader()
        reader.readAsDataURL(file)
        reader.onload = () => {
          this.options.img = reader.result
          this.options.filename = file.name
        }
      }
    },
    // 上傳圖片
    uploadImg() {
      this.$refs.cropper.getCropBlob(data => {
        let formData = new FormData()
        formData.append("file", data, this.options.filename)
        uploadCommonImg(formData).then(response => {
          this.open = false
          this.options.img = process.env.VUE_APP_BASE_API + response.fileName
          console.log('this.options.img',this.options.img)
          this.$emit('outUrl', response.fileName)
          this.visible = false
        })
      })
    },
    // 實(shí)時(shí)預(yù)覽
    realTime(data) {
      this.previews = data
    },
    // 關(guān)閉窗口
    closeDialog() {
      // this.options.img =''
      this.visible = false
      window.removeEventListener("resize", this.resizeHandler)
    }
  }
}
</script>
<style scoped lang="scss">
.user-info-head {
  position: relative;
  display: inline-block;
  height: 120px;
}
.user-info-head:hover:after {
  content: '+';
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  color: #eee;
  background: rgba(0, 0, 0, 0.5);
  font-size: 24px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  cursor: pointer;
  line-height: 110px;
  //border-radius: 50%;
  text-align: center;
}
</style>

4.第四步,父級頁面引入該子組件

 //其中url是父組件進(jìn)行子組件的初始化傳值  outUrl是接收子組件傳遞過來的參數(shù) 
 <imgCrop @outUrl="getUrl" :url="form.peoImage"/>

5.第五步 接收子組件傳遞過來的參數(shù)

 getUrl(url) {
      this.form.peoImage = url
    },

因?yàn)槲矣玫娜粢老到y(tǒng),ruoyi.css中集成了上述組件中的樣式 ,粘貼如下:

/**
* 通用css樣式布局處理
* Copyright (c) 2019 ruoyi
*/
/** 基礎(chǔ)通用 **/
.pt5 {
  padding-top: 5px;
}
.pr5 {
  padding-right: 5px;
}
.pb5 {
  padding-bottom: 5px;
}
.mt5 {
  margin-top: 5px;
}
.mr5 {
  margin-right: 5px;
}
.mb5 {
  margin-bottom: 5px;
}
.mb8 {
  margin-bottom: 8px;
}
.ml5 {
  margin-left: 5px;
}
.mt10 {
  margin-top: 10px;
}
.mr10 {
  margin-right: 10px;
}
.mb10 {
  margin-bottom: 10px;
}
.ml10 {
	margin-left: 10px;
}
.mt20 {
  margin-top: 20px;
}
.mr20 {
  margin-right: 20px;
}
.mb20 {
  margin-bottom: 20px;
}
.ml20 {
	margin-left: 20px;
}
.h1, .h2, .h3, .h4, .h5, .h6, h1, h2, h3, h4, h5, h6 {
  font-family: inherit;
  font-weight: 500;
  line-height: 1.1;
  color: inherit;
}
.el-message-box__status + .el-message-box__message{
  word-break: break-word;
}
.el-dialog:not(.is-fullscreen) {
  margin-top: 6vh !important;
}
.el-dialog__wrapper.scrollbar .el-dialog .el-dialog__body {
  overflow: auto;
  overflow-x: hidden;
  max-height: 70vh;
  padding: 10px 20px 0;
}
.el-table {
  .el-table__header-wrapper, .el-table__fixed-header-wrapper {
    th {
      word-break: break-word;
      background-color: #f8f8f9;
      color: #515a6e;
      height: 40px;
      font-size: 13px;
    }
  }
  .el-table__body-wrapper {
    .el-button [class*="el-icon-"] + span {
      margin-left: 1px;
    }
  }
}
/** 表單布局 **/
.form-header {
  font-size: 15px;
  color: #6379bb;
  border-bottom: 1px solid #ddd;
  margin: 8px 10px 25px 10px;
  padding-bottom: 5px
}
/** 表格布局 **/
.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
/* tree border */
.tree-border {
  margin-top: 5px;
  border: 1px solid #e5e6e7;
  background: #FFFFFF none;
  border-radius: 4px;
}
@media (max-width: 768px) {
  .pagination-container .el-pagination > .el-pagination__jump {
    display: none !important;
  }
  .pagination-container .el-pagination > .el-pagination__sizes {
    display: none !important;
  }
}
.el-table .fixed-width .el-button--mini {
  padding-left: 0;
  padding-right: 0;
  width: inherit;
}
/** 表格更多操作下拉樣式 */
.el-table .el-dropdown-link,.el-table .el-dropdown-selfdefine {
	cursor: pointer;
	margin-left: 5px;
}
.el-table .el-dropdown, .el-icon-arrow-down {
  font-size: 12px;
}
.el-tree-node__content > .el-checkbox {
  margin-right: 8px;
}
.list-group-striped > .list-group-item {
  border-left: 0;
  border-right: 0;
  border-radius: 0;
  padding-left: 0;
  padding-right: 0;
}
.list-group {
  padding-left: 0px;
  list-style: none;
}
.list-group-item {
  border-bottom: 1px solid #e7eaec;
  border-top: 1px solid #e7eaec;
  margin-bottom: -1px;
  padding: 11px 0px;
  font-size: 13px;
}
.pull-right {
  float: right !important;
}
.el-card__header {
  padding: 14px 15px 7px;
  min-height: 40px;
}
.el-card__body {
  padding: 15px 20px 20px 20px;
}
.card-box {
  margin-bottom: 10px;
}
/* button color */
.el-button--cyan.is-active,
.el-button--cyan:active {
  background: #20B2AA;
  border-color: #20B2AA;
  color: #FFFFFF;
}
.el-button--cyan:focus,
.el-button--cyan:hover {
  background: #48D1CC;
  border-color: #48D1CC;
  color: #FFFFFF;
}
.el-button--cyan {
  background-color: #20B2AA;
  border-color: #20B2AA;
  color: #FFFFFF;
}
/* text color */
.text-navy {
  color: #1ab394;
}
.text-primary {
  color: inherit;
}
.text-success {
  color: #1c84c6;
}
.text-info {
  color: #23c6c8;
}
.text-warning {
  color: #f8ac59;
}
.text-danger {
  color: #ed5565;
}
.text-muted {
  color: #888888;
}
/* image */
.img-circle {
  border-radius: 50%;
}
.img-lg {
  width: 120px;
  height: 120px;
}
.avatar-upload-preview {
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 200px;
  height: 200px;
  border-radius: 50%;
  box-shadow: 0 0 4px #ccc;
  overflow: hidden;
}
.avatar-upload-preview-update {
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width:162px;
  height: 200px;
  //border-radius: 50%;
  box-shadow: 0 0 4px #ccc;
  overflow: hidden;
}
.swiper-upload-preview-update {
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width:352px;
  height: 150px;
  //border-radius: 50%;
  box-shadow: 0 0 4px #ccc;
  overflow: hidden;
}
/* 拖拽列樣式 */
.sortable-ghost {
  opacity: .8;
  color: #fff !important;
  background: #42b983 !important;
}
.top-right-btn {
  position: relative;
  float: right;
}
/* 分割面板樣式 */
.splitpanes.default-theme .splitpanes__pane {
  background-color: #fff!important;
}

6.至此完結(jié)。

到此這篇關(guān)于vue使用vue-cropper實(shí)現(xiàn)圖片裁剪之單圖裁剪的文章就介紹到這了,更多相關(guān)vue圖片裁剪內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue循環(huán)數(shù)組改變點(diǎn)擊文字的顏色

    vue循環(huán)數(shù)組改變點(diǎn)擊文字的顏色

    這篇文章主要為大家詳細(xì)介紹了vue循環(huán)數(shù)組改變點(diǎn)擊文字的顏色,非常實(shí)用的切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • vue中keep-alive組件的用法示例

    vue中keep-alive組件的用法示例

    眾所周知keep-alive是Vue提供的一個(gè)抽象組件,主要是用來對組件進(jìn)行緩存,從而做到節(jié)省性能,這篇文章主要給大家介紹了關(guān)于vue中keep-alive組件用法的相關(guān)資料,需要的朋友可以參考下
    2021-05-05
  • Vue 與 Vuex 的第一次接觸遇到的坑

    Vue 與 Vuex 的第一次接觸遇到的坑

    在 Vue.js 的項(xiàng)目中,如果項(xiàng)目結(jié)構(gòu)簡單, 父子組件之間的數(shù)據(jù)傳遞可以使用 props 或者 $emit 等方式,如果是大型項(xiàng)目,很多時(shí)候都需要在子組件之間傳遞數(shù)據(jù),使用vue的狀態(tài)管理工具vuex很好的解決
    2018-08-08
  • vue實(shí)現(xiàn)一個(gè)矩形標(biāo)記區(qū)域(rectangle marker)的方法

    vue實(shí)現(xiàn)一個(gè)矩形標(biāo)記區(qū)域(rectangle marker)的方法

    這篇文章主要介紹了vue實(shí)現(xiàn)一個(gè)矩形標(biāo)記區(qū)域 rectangle marker的方法,幫助大家實(shí)現(xiàn)區(qū)域標(biāo)記功能,感興趣的朋友可以了解下
    2020-10-10
  • vue3和beego跨域請求配置方式

    vue3和beego跨域請求配置方式

    文章介紹了如何在Vue3和Beego中配置跨域請求,在Beego的router.go文件的init函數(shù)中添加option函數(shù)來回應(yīng)預(yù)檢請求,以支持跨域請求,這是個(gè)人經(jīng)驗(yàn)分享,希望能幫助到大家
    2025-01-01
  • 關(guān)于VanCascader默認(rèn)值(地址code轉(zhuǎn)換)

    關(guān)于VanCascader默認(rèn)值(地址code轉(zhuǎn)換)

    這篇文章主要介紹了關(guān)于VanCascader默認(rèn)值(地址code轉(zhuǎn)換),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Vue.directive使用注意(小結(jié))

    Vue.directive使用注意(小結(jié))

    這篇文章主要介紹了Vue.directive使用注意(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • vue+element實(shí)現(xiàn)錨點(diǎn)鏈接方式

    vue+element實(shí)現(xiàn)錨點(diǎn)鏈接方式

    這篇文章主要介紹了vue+element實(shí)現(xiàn)錨點(diǎn)鏈接方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • vue項(xiàng)目引入Iconfont圖標(biāo)庫的教程圖解

    vue項(xiàng)目引入Iconfont圖標(biāo)庫的教程圖解

    這篇文章主要介紹了vue項(xiàng)目引入Iconfont圖標(biāo)庫的相關(guān)知識,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-10-10
  • vue3+el-table封裝示例詳解(編輯、刪除、查看詳情按鈕一起封裝)

    vue3+el-table封裝示例詳解(編輯、刪除、查看詳情按鈕一起封裝)

    在Vue3中,利用Element?Plus?UI庫封裝表格組件,實(shí)現(xiàn)編輯、刪除和查看詳情的功能,通過定義tableData和tableDataHeader來管理表格數(shù)據(jù)和表頭,其中tableData通常從后端獲取,而tableHeader可根據(jù)具體需求自定義,感興趣的朋友跟隨小編一起看看吧
    2024-09-09

最新評論

兴业县| 和龙市| 南通市| 巴中市| 阿克| 搜索| 广南县| 巩义市| 阳东县| 祁门县| 定边县| 资源县| 全椒县| 中阳县| 榆林市| 晋中市| 唐河县| 海丰县| 铜陵市| 石台县| 时尚| 电白县| 盐亭县| 昭通市| 德州市| 鞍山市| 七台河市| 邢台市| 金沙县| 茶陵县| 贵州省| 揭阳市| 林口县| 靖江市| 佳木斯市| 凤庆县| 塔城市| 齐齐哈尔市| 明水县| 微山县| 黄大仙区|