" />

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

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

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

官網(wǎng):

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

一、安裝:

npm install vue-cropper 

或者

yarn add vue-cropper

二、使用:

import VueCropper from 'vue-cropper'

設(shè)置component

export default {
  components: {
    VueCropper
  }
}

template中插入:(外面需要套一個(gè)div,用于設(shè)置插件顯示的大?。?/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)置方法:

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

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

this.$refs.cropper.startCrop() 開(kāi)始截圖(如果沒(méi)有設(shè)置截圖框的話,通過(guò)這個(gè)啟動(dòng)截圖框)

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">? 左旋轉(zhuǎn)</el-button>
              <el-button size="mini" type="danger" plain @click="rotateRight">? 右旋轉(zhuǎn)</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>
        <!--預(yù)覽效果圖-->
        <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,      //是否默認(rèn)生成截圖框
        autoCropWidth: 300,  //默認(rèn)生成截圖框?qū)挾?
        autoCropHeight: 200, //默認(rèn)生成截圖框高度
        fixed: true,         //是否開(kāi)啟截圖框?qū)捀吖潭ū壤?
        fixedNumber: [1.5, 1], //截圖框的寬高比例
        full: true,         //false按原比例裁切圖片,不失真
        fixedBox: true,      //固定截圖框大小,不允許改變
        canMove: false,      //上傳圖片是否可以移動(dòng)
        canMoveBox: true,    //截圖框能否拖動(dòng)
        original: false,     //上傳圖片按照原始比例渲染
        centerBox: false,    //截圖框是否被限制在圖片里面
        height: true,        //是否按照設(shè)備的dpr 輸出等比例圖片
        infoTrue: false,     //true為展示真實(shí)輸出圖片寬高,false展示看到的截圖框?qū)捀?
        maxImgSize: 3000,    //限制圖片最大寬度和高度
        enlarge: 1,          //圖片根據(jù)截圖框輸出比例倍數(shù)
        mode: '230px 150px'  //圖片默認(rèn)渲染方式
      }
    };
  },
  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)
    },
    //向左旋轉(zhuǎn)
    rotateLeft () {
      this.$refs.cropper.rotateLeft()
    },
    //向右旋轉(zhuǎn)
    rotateRight () {
      this.$refs.cropper.rotateRight()
    },
    //實(shí)時(shí)預(yù)覽函數(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
      }
      //轉(zhuǎn)化為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
      }
      //轉(zhuǎn)化為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: '文件服務(wù)異常,請(qǐng)聯(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>

效果:

在這里插入圖片描述

總結(jié)

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

相關(guān)文章

  • vue單頁(yè)應(yīng)用加百度統(tǒng)計(jì)代碼(親測(cè)有效)

    vue單頁(yè)應(yīng)用加百度統(tǒng)計(jì)代碼(親測(cè)有效)

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

    iView框架問(wèn)題整理小結(jié)

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

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

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

    Vue實(shí)現(xiàn)當(dāng)前頁(yè)面刷新的五種方法總結(jié)

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

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

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

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

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

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

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

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

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

    Vue3.0組件通信mitt源碼ts實(shí)例解析

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

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

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

最新評(píng)論

云南省| 凯里市| 谷城县| 宜春市| 沿河| 轮台县| 依兰县| 达拉特旗| 扎赉特旗| 莫力| 股票| 临沧市| 乐都县| 定边县| 墨脱县| 定陶县| 汪清县| 绥德县| 获嘉县| 竹北市| 太康县| 大姚县| 合山市| 荃湾区| 呼和浩特市| 砚山县| 清原| 阜新市| 信宜市| 营山县| 芮城县| 舒城县| 博野县| 安图县| 宁城县| 句容市| 电白县| 临朐县| 泰兴市| 兰西县| 阳泉市|