" />

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

vue 如何使用vue-cropper裁剪圖片你知道嗎

 更新時間:2021年11月24日 16:09:34   作者:*且聽風吟  
這篇文章主要為大家介紹了vue 使用vue-cropper裁剪圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

官網(wǎng):

https://github.com/xyxiao001/vue-cropper

一、安裝:

npm install vue-cropper 

或者

yarn add vue-cropper

二、使用:

import VueCropper from 'vue-cropper'

設置component

export default {
  components: {
    VueCropper
  }
}

template中插入:(外面需要套一個div,用于設置插件顯示的大?。?/p>

<vueCropper
      ref="cropper"
      :img="option.img"
      :outputSize="option.size"
      :outputType="option.outputType"
></vueCropper>
    data(){
      return{
         option:{
            img: 'imgUrl', // img的URL或者base64
            size: 1,
            outputType: 'png',
         }
      }
    }

三、內(nèi)置方法:

名稱 功能 默認值 可選值
img 裁剪圖片的地址 url 地址 / base64 / blob
outputSize 裁剪生成圖片的質(zhì)量 1 0.1 - 1
outputType 裁剪圖片的地址 jpg (jpg 需要傳入jpeg) jpeg / png / web

內(nèi)置方法:通過this.$refs.cropper 調(diào)用插件。

this.$refs.cropper.startCrop() 開始截圖(如果沒有設置截圖框的話,通過這個啟動截圖框)

this.$refs.cropper.stopCrop() 停止截圖

this.$refs.cropper.clearCrop() 清除截圖

this.$refs.cropper.getCropData() 獲取截圖信息(得到截圖的URL或者base64)

    // 獲取截圖的base64 數(shù)據(jù)
    this.$refs.cropper.getCropData((data) => {
      // do something
      console.log(data)
    })
    // 獲取截圖的blob數(shù)據(jù)
    this.$refs.cropper.getCropBlob((data) => {
      // do something
      console.log(data)
    })

四、使用:

<template>
  <div>
    <el-dialog title="圖片剪裁" :visible.sync="show" append-to-body width="950px" center>
      <div class="cropper-content">
        <div class="cropper-box">
          <div class="cropper">
            <vue-cropper ref="cropper" :img="option.img" :outputSize="option.outputSize" :outputType="option.outputType" :info="option.info" :canScale="option.canScale" :autoCrop="option.autoCrop" :autoCropWidth="option.autoCropWidth" :autoCropHeight="option.autoCropHeight" :fixed="option.fixed" :fixedNumber="option.fixedNumber" :full="option.full" :fixedBox="option.fixedBox" :canMove="option.canMove" :canMoveBox="option.canMoveBox" :original="option.original" :centerBox="option.centerBox" :height="option.height" :infoTrue="option.infoTrue" :maxImgSize="option.maxImgSize" :enlarge="option.enlarge" :mode="option.mode" @realTime="realTime" @imgLoad="imgLoad">
            </vue-cropper>
          </div>
          <!--底部操作工具按鈕-->
          <div class="footer-btn">
            <div class="scope-btn">
              <label class="btn" for="uploads">選擇圖片</label>
              <input type="file" id="uploads" style="position:absolute; clip:rect(0 0 0 0);" accept="image/png, image/jpeg, image/gif, image/jpg" @change="selectImg($event)">
              <el-button size="mini" type="danger" plain icon="el-icon-zoom-in" @click="changeScale(1)">放大</el-button>
              <el-button size="mini" type="danger" plain icon="el-icon-zoom-out" @click="changeScale(-1)">縮小</el-button>
              <el-button size="mini" type="danger" plain @click="rotateLeft">? 左旋轉</el-button>
              <el-button size="mini" type="danger" plain @click="rotateRight">? 右旋轉</el-button>
            </div>
            <div class="upload-btn">
              <el-button size="mini" type="success" @click="uploadImg('blob')">上傳圖片 <i class="el-icon-upload"></i></el-button>
            </div>
          </div>
        </div>
        <!--預覽效果圖-->
        <div class="show-preview">
          <div :style="previews.div" class="preview">
            <img :src="previews.url" :style="previews.img">
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { VueCropper } from 'vue-cropper'
export default {
  name: "CropperImage",
  components: {
    VueCropper
  },
  data () {
    return {
      show: this.visible,
      name: this.Name,
      previews: {},
      option: {
        img: '',             //裁剪圖片的地址
        outputSize: 1,       //裁剪生成圖片的質(zhì)量(可選0.1 - 1)
        outputType: 'jpeg',  //裁剪生成圖片的格式(jpeg || png || webp)
        info: true,          //裁剪框的大小信息,圖片大小信息
        canScale: true,      //圖片是否允許滾輪縮放
        autoCrop: true,      //是否默認生成截圖框
        autoCropWidth: 300,  //默認生成截圖框?qū)挾?
        autoCropHeight: 200, //默認生成截圖框高度
        fixed: true,         //是否開啟截圖框?qū)捀吖潭ū壤?
        fixedNumber: [1.5, 1], //截圖框的寬高比例
        full: true,         //false按原比例裁切圖片,不失真
        fixedBox: true,      //固定截圖框大小,不允許改變
        canMove: false,      //上傳圖片是否可以移動
        canMoveBox: true,    //截圖框能否拖動
        original: false,     //上傳圖片按照原始比例渲染
        centerBox: false,    //截圖框是否被限制在圖片里面
        height: true,        //是否按照設備的dpr 輸出等比例圖片
        infoTrue: false,     //true為展示真實輸出圖片寬高,false展示看到的截圖框?qū)捀?
        maxImgSize: 3000,    //限制圖片最大寬度和高度
        enlarge: 1,          //圖片根據(jù)截圖框輸出比例倍數(shù)
        mode: '230px 150px'  //圖片默認渲染方式
      }
    };
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    Name: {
      type: String,
      default: ''
    }
  },
  watch: {
    visible () {
      this.show = this.visible
    }
  },
  methods: {
    //初始化函數(shù)
    imgLoad (msg) {
    },
    //圖片縮放
    changeScale (num) {
      num = num || 1
      this.$refs.cropper.changeScale(num)
    },
    //向左旋轉
    rotateLeft () {
      this.$refs.cropper.rotateLeft()
    },
    //向右旋轉
    rotateRight () {
      this.$refs.cropper.rotateRight()
    },
    //實時預覽函數(shù)
    realTime (data) {
      this.previews = data
    },
    //選擇圖片
    selectImg (e) {
      let file = e.target.files[0]
      if (!/\.(jpg|jpeg|png|JPG|PNG)$/.test(e.target.value)) {
        this.$message({
          message: '圖片類型要求:jpeg、jpg、png',
          type: "error"
        });
        return false
      }
      //轉化為blob
      let reader = new FileReader()
      reader.onload = (e) => {
        let data
        if (typeof e.target.result === 'object') {
          data = window.URL.createObjectURL(new Blob([e.target.result]))
        } else {
          data = e.target.result
        }
        this.option.img = data
      }
      //轉化為base64
      reader.readAsDataURL(file)
    },
    //上傳圖片
    uploadImg (type) {
      let _this = this
      if (type === 'blob') {
        // 獲取截圖的blob數(shù)據(jù)
        this.$refs.cropper.getCropBlob(async (data) => {
          let formData = new FormData();
          formData.append('file', data, new Date().getTime() + '.png')
          // 調(diào)用axios上傳
          let { data: res } = await _this.$http.post(`${msBaseUrl}common-tools-web/file/upload/image`, formData)
          if (res.code === 200) {
            _this.$message({
              message: res.desc,
              type: "success"
            });
            let data = res.result
            let imgInfo = {
              name: data.name,
              id: data.id,
              url: data.url
            };
            _this.$emit('uploadImgSuccess', imgInfo);
          } else {
            _this.$message({
              message: '文件服務異常,請聯(lián)系管理員!',
              type: "error"
            })
          }
        })
      }
    }
  }
}
</script>
<style scoped lang="less">
.cropper-content {
  display: flex;
  display: -webkit-flex;
  justify-content: flex-end;
  .cropper-box {
    flex: 1;
    width: 100%;
    .cropper {
      width: auto;
      height: 300px;
    }
  }
  .show-preview {
    flex: 1;
    -webkit-flex: 1;
    display: flex;
    display: -webkit-flex;
    justify-content: center;
    .preview {
      overflow: hidden;
      border: 1px solid #67c23a;
      background: #cccccc;
    }
  }
}
.footer-btn {
  margin-top: 30px;
  display: flex;
  display: -webkit-flex;
  justify-content: flex-end;
  .scope-btn {
    display: flex;
    display: -webkit-flex;
    justify-content: space-between;
    padding-right: 10px;
  }
  .upload-btn {
    flex: 1;
    -webkit-flex: 1;
    display: flex;
    display: -webkit-flex;
    justify-content: center;
  }
  .btn {
    outline: none;
    display: inline-block;
    line-height: 1;
    white-space: nowrap;
    cursor: pointer;
    -webkit-appearance: none;
    text-align: center;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    outline: 0;
    -webkit-transition: 0.1s;
    transition: 0.1s;
    font-weight: 500;
    padding: 8px 15px;
    font-size: 12px;
    border-radius: 3px;
    color: #fff;
    background-color: #409eff;
    border-color: #409eff;
    margin-right: 10px;
  }
}
</style>

效果:

在這里插入圖片描述

總結

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內(nèi)容!

相關文章

  • vue單頁應用加百度統(tǒng)計代碼(親測有效)

    vue單頁應用加百度統(tǒng)計代碼(親測有效)

    這篇文章主要介紹了vue單頁應用加百度統(tǒng)計代碼的解決方法,需要的朋友參考下吧
    2018-01-01
  • iView框架問題整理小結

    iView框架問題整理小結

    這篇文章主要介紹了iView框架問題整理小結,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10
  • vue-element-admin下載到登錄的一些坑

    vue-element-admin下載到登錄的一些坑

    本文主要介紹了vue-element-admin下載到登錄的一些坑,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • Vue實現(xiàn)當前頁面刷新的五種方法總結

    Vue實現(xiàn)當前頁面刷新的五種方法總結

    這篇文章主要介紹了Vue中實現(xiàn)頁面刷新的5種方法,包括使用$router.go(0)、location.reload()、通過router-view的key屬性、使用v-if指令手動觸發(fā)組件重新渲染,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-02-02
  • Vue中使用flv.js播放視頻的示例詳解

    Vue中使用flv.js播放視頻的示例詳解

    這篇文章主要為大家詳細介紹了如何在Vue項目中使用flv.js播放視頻,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-04-04
  • Vue自定義彈窗指令的實現(xiàn)代碼

    Vue自定義彈窗指令的實現(xiàn)代碼

    使用vue2.0實現(xiàn)自定義彈窗指令,當標簽有該指令時,點擊標簽可以彈出彈窗。下面通過實例代碼給大家介紹Vue自定義彈窗指令的相關知識,感興趣的朋友一起看看吧
    2018-08-08
  • el-table表頭全選框隱藏或禁用設置方法

    el-table表頭全選框隱藏或禁用設置方法

    有時候我們使用el-table的選擇框時,如果涉及修改、刪除時,可能一次只允許用戶選擇一條,這樣的話如果使用頂部的全選復選框就不合適了,這篇文章主要給大家介紹了關于el-table表頭全選框隱藏或禁用設置的相關資料,需要的朋友可以參考下
    2023-09-09
  • Vue彈出菜單功能的實現(xiàn)代碼

    Vue彈出菜單功能的實現(xiàn)代碼

    這篇文章主要介紹了Vue左側底部彈出菜單功能的實現(xiàn)代碼,需要的朋友可以參考下
    2018-09-09
  • Vue3.0組件通信mitt源碼ts實例解析

    Vue3.0組件通信mitt源碼ts實例解析

    這篇文章主要為大家介紹了Vue3.0組件通信mitt源碼ts實例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • vue仿攜程輪播圖效果(滑動輪播,下方高度自適應)

    vue仿攜程輪播圖效果(滑動輪播,下方高度自適應)

    這篇文章主要介紹了vue仿攜程輪播圖效果(滑動輪播,下方高度自適應),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02

最新評論

安康市| 珲春市| 闻喜县| 沿河| 漳平市| 讷河市| 即墨市| 门源| 遵义县| 东源县| 合肥市| 耒阳市| 濮阳市| 连州市| 阆中市| 佛教| 厦门市| 团风县| 五莲县| 报价| 大城县| 扬州市| 平泉县| 尚志市| 盘锦市| 武邑县| 浙江省| 大方县| 子洲县| 武强县| 虞城县| 合阳县| 永善县| 大埔县| 普宁市| 宁陕县| 霍林郭勒市| 黑龙江省| 龙江县| 门源| 嵩明县|