1、安装:npm install vue-cropper
2、引入:import VueCropper from 'vue-cropper'

Vue.use(VueCropper)
<template>
<div class='box'>
<el-button @click='dialogImg=true'>截图</el-button>
<div class="img">
<img style="width: 100%; height: 100%;" v-if="option.img" :src="option.img" alt="" />
</div>
<el-dialog
class="dialogImgClass"
title=""
:visible.sync="dialogImg"
size="middle"
:before-close="handleClose1">
<div class="dialogBox">
<el-row>
<span style="margin-right:20px;">切图工具比例</span>
<el-radio-group v-model="pictureRatio" @change="changePicRatio">
<el-radio class="radio" label="1">16:9</el-radio>
<el-radio class="radio" label="2">1:1</el-radio>
<el-radio class="radio" label="3">无</el-radio>
</el-radio-group>
</el-row>
<div class="wrapper">
<vueCropper
style="border: 1px solid;"
ref="cropper"
:img="option.img"
:outputSize="option.size"
: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"
:centerBox="option.centerBox"
:original="option.original"
@realTime="realTime">
</vueCropper>
</div>
<div class="btn-box">
<label class="btn" for="upload">选择照片</label>
<input name="file" type="file" id="upload" style="position:absolute; clip:rect(0 0 0 0);" accept="image/png, image/jpeg, image/gif, image/jpg" @change="uploadImg($event)">
<el-button :plain="true" type="danger" style="background: #e03434;color: #fff;" @click="finishUpdate('base64')">上传照片</el-button>
</div>
</div>
</el-dialog>
</div>
</template> <script>
import {VueCropper} from 'vue-cropper'
import api from '../fetch/api'
export default {
data () {
return {
dialogImg:false,
pictureRatio:"2",//根据选项修改图片比例
exam:null,
option:{
img: '',
info: true,
size: 1,
outputType: 'png',
canScale:true,
autoCrop: true,
// 只有自动截图开启 宽度高度才生效
autoCropWidth: 540,
autoCropHeight: 540,
// 开启宽度和高度比例
fixed: true,
fixedNumber: [16, 16],
fileaa:null,
full:true,//输出原图比例的截图
centerBox:true,//截图框是否限制在图片里(只有自动截图开启才生效)
original:false,//上传图片是否显示原始宽高(针对大图,可以铺满)
},
qiniu:{
key:'',
token:'',
file:''
},//七牛云数据
}
},
components: {
'vueCropper': VueCropper
},
created(){ },
methods: {
changePicRatio(val){//切换图片比例
// console.log(val);
if(val==="1"){
this.option.fixedNumber = [16, 9];
this.option.autoCrop = true;
this.option.fixed = true;
if(this.exam != null){
this.$refs.cropper.clearCrop();
this.$refs.cropper.startCrop();
this.uploadImg (this.exam);
console.log("改成16:9");
}else if(this.option.img != ''){
this.$refs.cropper.clearCrop();
this.$refs.cropper.startCrop();
}
}else if(val==="2"){
this.option.fixedNumber = [16, 16];
this.option.autoCrop = true;
this.option.fixed = true;
if(this.exam != null){
this.$refs.cropper.clearCrop();
this.$refs.cropper.startCrop();
this.uploadImg (this.exam);
console.log("改成1:1");
}else if(this.option.img != ''){
this.$refs.cropper.clearCrop();
this.$refs.cropper.startCrop();
}
}else if(val==="3"){
this.option.autoCrop = false;
this.option.fixed = false;
this.$refs.cropper.clearCrop();
this.$refs.cropper.startCrop();
}
},
uploadImg (e) {
this.exam=e;
//上传图片
// this.option.img
var file = e.target.files[0]
if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(e.target.value)) {
alert('图片类型必须是.gif,jpeg,jpg,png,bmp中的一种')
return false
}
var reader = new FileReader()
reader.onload = (e) => {
let data
if (typeof e.target.result === 'object') {
// 把Array Buffer转化为blob 如果是base64不需要
data = window.URL.createObjectURL(new Blob([e.target.result]))
} else {
data = e.target.result
}
this.option.img = data
}
this.fileaa = e.target.files[0];
console.log(this.fileaa);
// 转化为base64
reader.readAsDataURL(file)
// 转化为blob
// reader.readAsArrayBuffer(file)
},
finishUpdate(type){
this.$refs.cropper.getCropData((data)=>{
this.option.img = data;
let pic = data.replace(/^.*?,/, '');
let size = this.fileSize(pic);
let data1 = {};
api.get(api.config.getUploadVoucher,data1).then(res=>{
if(res.code==1){
console.log(res);
this.qiniu.key = res.data.mediaKey;
this.qiniu.token = res.data.token;
let id = res.data.mediaId;
this.fileId = res.data.mediaId;
let url = "http://up-z2.qiniu.com/putb64/"+size+"/key/"+this.baseCode64(this.qiniu.key);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange=function(){
if (xhr.readyState==4){
console.log(xhr.responseText)
}
}
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/octet-stream");
xhr.setRequestHeader("Authorization", "UpToken "+this.qiniu.token);
xhr.send(pic);
this.dialogImg = false;
}
})
})
},
fileSize(base64){
let fileSize;
//找到等号,把等号也去掉
if (base64.indexOf('=') > 0) {
var indexOf = base64.indexOf('=');
base64 = base64.substring(0, indexOf);//把末尾的’=‘号去掉
}
fileSize = parseInt(base64.length - (base64.length / 8) * 2);
return fileSize;
},
baseCode64(input){
var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = this._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
_keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
_keyStr.charAt(enc3) + _keyStr.charAt(enc4);
}
return output;
},
_utf8_encode(string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
handleClose1(){
this.dialogImg = false;
},
realTime (data) {
this.previews = data
},
}
}
</script> <style lang='stylus' scoped>
.img{
width 500px
}
.dialogBox{
position:relative
height:640px
} .dialogBox .wrapper{
width:540px
height:540px
} .dialogBox .btn-box{
position:absolute
width:200px
right:0
bottom:0
}
.dialogBox .btn{
background:#e03434
color:#fff
font-size:14px
line-height:1
cursor:pointer
margin:0
white-space:nowrap
border:1px solid #ddd
border-radius:4px
padding:9px 15px
box-sizing: border-box
}
</style>

  

vue+七牛云 截图工具的更多相关文章

  1. 七牛云qshell工具定时备份空间文件到本地

    qshell 是利用七牛文档上公开的 API实现的一个方便开发者测试和使用七牛API服务的命令行工具,使用该工具可以实现很多的功能,今天就分享一下利用qshell定时备份空间文件到本地 1.下载qsh ...

  2. 七牛云——qshell一个神奇的工具

    前言 qshell是利用七牛文档上公开的API实现的一个方便开发者测试和使用七牛API服务的命令行工具.该工具设计和开发的主要目的就是帮助开发者快速解决问题.目前该工具融合了七牛存储,CDN,以及其他 ...

  3. markown编辑器截图粘贴预览,并将图片传至七牛云

    最近在做一个项目,需要实现类似QQ截图后,就是能够在富文本编辑器中粘贴截图并预览. 先看一下效果: 分析一下实现步骤: QQ截图后在编辑器中粘贴,肯定会有一个粘贴事件,即 paste 事件 在事件回调 ...

  4. Linux下七牛云存储qrsync命令行上传同步工具

    原址:https://m.aliyun.com/yunqi/ziliao/54370 VPS数据备份是一个重要的工作,之前在文章:使用七牛云存储自动备份VPS数据分享过使用七牛云存储提供的工具QRSB ...

  5. 【微信小程序】在微信开发工具上七牛云的图片可以看到,但是在真机上看不到的原因解决

    在开发微信小程序过程中,在微信开发者工具上,七牛云的图片都可以展示出来,但是在真机上,七牛云的图片却展示不出来,也没有报404找不到或者不能加载图片的问题, 必须保证: 1.图片是用image加载的: ...

  6. 图床工具PicGO实现七牛云图片上传

    图床工具PicGO实现七牛云图片上传 我们在写博客或者网络文章时经常需要上传图片.目前最有名气的图床工具就是PicGO. 简单的界面,完整的功能,在相册里也能直接复制markdown图片链接.一直深受 ...

  7. vue上传视屏或者图片到七牛云

    首先下载七牛云的JavaScript-SDK npm install qiniu-js 下载完成JavaScript-SDK以后就可以上传图片信息了 <template> <div& ...

  8. iUploader 2.0 七牛云上传工具

    iUploader 软件介绍: iUploader主要功能将文件上传至七牛云,返回 Markdown 格式的链接到剪贴板 功能介绍: 图片本地压缩 图片右键上传 图片截取上传 图片复制上传 图片拖拽上 ...

  9. 七牛云-上传、删除文件,工具类(Day49)

    要求: 1. java1.8以上 2. Maven: 这里的version指定了一个版本范围,每次更新pom.xml的时候会尝试去下载7.5.x版本中的最新版本,你可以手动指定一个固定的版本. < ...

随机推荐

  1. airtest使用

    airtest Airtest是网易开发的手机UI界面自动化测试工具 通过截图功能操作手机虽然方便,但是截图涉及到分辨率的问题,代码不能在不同的手机上通用. 可以用来开发手机App爬虫 使用先抓大再抓 ...

  2. redis——持久化策略

    4.2.2 持久 化方式(1 ) RDB 方式1. 什么是 RDB 方式?Redis Database(RDB),就是在指定的时间间隔内将内存中的数据集快照写入磁盘,数据恢复时将快照文件直接再读到内存 ...

  3. java学习笔记14-多态

    多态可以理解为同一个操作在不同对象上会有不同的表现 比如在谷歌浏览器上按F1会弹出谷歌的帮助页面.在windows桌面按F1会弹出windows的帮助页面. 多态存在的三个必要条件: 继承 重写 父类 ...

  4. Charles----伪造手机端的request和reponse参数

    使用场景: 在测试中通过伪造reponse数据来模拟某些测试场景,如下截图.要求:通过修改reponse中的值,再次请求修改7为100,只是会显示为99+ 实现方式: 1.通过breakpoints来 ...

  5. mongo operations

    Check Mongo Operate Logs db.getCollection('oplog.rs').find({'ns':{$in:['sxa.sxacc-organizations','sx ...

  6. zabbix通过钉钉报警

    1.创建报警脚本  vim /usr/local/share/zabbix/alertscripts/dingalert.py #!/usr/bin/env python import json im ...

  7. pycharm 头模板

    #!/usr/local/bin/python3 # -*- coding: utf-8 -*- """ @author: Ray @contact: raylively ...

  8. Flutter布局----弹性布局 (Flex)

    弹性布局(Flex) 弹性布局允许子组件按照一定比例来分配父容器空间.弹性布局的概念在其它UI系统中也都存在,如H5中的弹性盒子布局,Android中的FlexboxLayout等.Flutter中的 ...

  9. Shell 04 字符串处理、正则表达式

    一.字符串的处理   1.字符串截取 1.1 s{}表达式  ${变量名:起始位置:长度} (从0开始) n=number (n="number") echo ${#n}   -- ...

  10. ue/um-editor实现word图片复制

    图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码 目前限chrome浏览器使用,但是项目要求需要支持所有的浏览器,包括Windows和macOS系统.没有办 ...