广告
返回顶部
首页 > 资讯 > 精选 >vue-cropper怎么实现裁剪图片
  • 334
分享到

vue-cropper怎么实现裁剪图片

2023-06-30 14:06:11 334人浏览 薄情痞子
摘要

这篇文章主要讲解了“Vue-cropper怎么实现裁剪图片”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“vue-cropper怎么实现裁剪图片”吧!先展示一下效果如何使用:1、安装 

这篇文章主要讲解了“Vue-cropper怎么实现裁剪图片”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“vue-cropper怎么实现裁剪图片”吧!

先展示一下效果

vue-cropper怎么实现裁剪图片

如何使用:

1、安装 

npm install vue-cropper  //(如果安装不上,用cnpm)

2、直接贴代码爽快人就是这样

<template>  <el-dialog    title="裁剪图片"    :visible.sync="visible"    @close="onCallback(false)"    class="handleDialog"    width="1000px"  >    <div class="wrap">      <div class="main">        <div class="cropperContent">          <div class="cropper">            <vueCropper              ref="cropper"              :img="option.img"              :outputSize="option.size"              :outputType="option.outputType"              :info="true"              :full="option.full"              :canMove="option.canMove"              :canMoveBox="option.canMoveBox"              :original="option.original"              :autoCrop="option.autoCrop"              :autoCropWidth="option.autoCropWidth"              :autoCropHeight="option.autoCropHeight"              :fixedBox="option.fixedBox"              @realTime="realTime"              @imgLoad="imgLoad"            ></vueCropper>          </div>          <div class="previewBox">            <div class="title">实时预览</div>            <div              class="showPreview"              :style="{                width: previews.w + 'px',                height: previews.h + 'px',              }"            >              <div : class="preview">                <img :src="previews.url" : />              </div>            </div>          </div>        </div>         <div class="footerButton">          <div class="scopeButton">            <label class="localButton" for="uploads">本地图片</label>            <input              type="file"              id="uploads"              class="inputFile"              accept="image/png, image/jpeg, image/gif, image/jpg"              @change="uploadImg($event)"            />             <el-button              type="primary"              @click="changeScale(1)"              icon="el-icon-plus"            ></el-button>            <el-button              type="primary"              @click="changeScale(-1)"              icon="el-icon-minus"            ></el-button>            <el-button              type="primary"              @click="rotateLeft"              icon="el-icon-refresh-left"            ></el-button>            <el-button              type="primary"              @click="rotateRight"              icon="el-icon-refresh-right"            ></el-button>          </div>          <div class="uploadButton">            <el-button              @click="down('blob')"              type="primary"              icon="el-icon-download"              >下载</el-button            >            <el-button              @click="uploadNewPic"              type="primary"              icon="el-icon-upload2"              >上传</el-button            >          </div>        </div>      </div>      <div class="end">        <el-button type="primary" @click="saveNewPic">保存</el-button>        <el-button @click="onCallback(false)">取消</el-button>      </div>    </div>  </el-dialog></template><script>import { VueCropper } from "vue-cropper";import { imgView, imgUploadUrl, uploadImg } from "services";import { alerts } from "js/yydjs.js";export default {  components: { VueCropper },  data() {    return {      imgView,      picId: "",      newPicId: "",      crap: false,      previews: {},      option: {        img: "",        size: 1,        full: false, //输出原图比例截图 props名full        outputType: "png",        canMove: true,        original: false,        canMoveBox: false,        autoCrop: true,        autoCropWidth: 300,        autoCropHeight: 300,        fixedBox: true,      },      downImg: "#",      cate: "",      ratio: 1,    };  },  mounted() {    this.option.img = this.imgView + this.picId;    this.option.autoCropHeight = this.option.autoCropWidth * this.ratio;  },  methods: {    saveNewPic() {      if (!this.newPicId) {        return alerts("请上传裁剪后的图片");      }      this.onCallback(this.newPicId);    },    changeScale(num) {      num = num || 1;      this.$refs.cropper.changeScale(num);    },    rotateLeft() {      this.$refs.cropper.rotateLeft();    },    rotateRight() {      this.$refs.cropper.rotateRight();    },    // 实时预览函数    realTime(data) {      console.log(data, "realTime");      this.previews = data;    },    // 将base64转换为文件 百度随便找的 看需求使用    dataURLtoFile(dataurl, filename) {      var arr = dataurl.split(","),        mime = arr[0].match(/:(.*?);/)[1],        bstr = atob(arr[1]),        n = bstr.length,        u8arr = new Uint8Array(n);      while (n--) {        u8arr[n] = bstr.charCodeAt(n);      }      return new File([u8arr], filename, { type: mime });    },    uploadNewPic() {      this.$refs.cropper.getCropData((data) => {        let name = new Date().getTime();        let file = this.dataURLtoFile(data, `${name}.png`);        console.log(file, "ssss");        let fd = new FORMData();        fd.append("file", file);        fd.append("cate", this.cate);        uploadImg(fd).then((res) => {          if (res) {            let { scaleRelativePath = "" } = res.body;            this.newPicId = scaleRelativePath;            alerts("上传成功", "success"); // 自己写的弹框          }        });      });    },    down(type) {      // event.preventDefault()      var aLink = document.createElement("a");      aLink.download = "author-img";      // 输出      if (type === "blob") {        this.$refs.cropper.getCropBlob((data) => {          console.log(data, type);          this.downImg = window.URL.createObjectURL(data);          // aLink.download = this.downImg;          console.log(this.downImg);          aLink.href = window.URL.createObjectURL(data);          aLink.click();        });      } else {        this.$refs.cropper.getCropData((data) => {          this.downImg = data;          aLink.href = data;          aLink.click();        });      }    },    uploadImg(e) {      //上传图片      // this.option.img      var file = e.target.files[0];      if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(e.target.value)) {        alerts("图片类型必须是.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;      };      // 转化为base64      // reader.readAsDataURL(file)      // 转化为blob      reader.readAsArrayBuffer(file);    },    imgLoad(msg) {      console.log(msg, "msg");    },  },};</script><style lang="sCSS" scoped>@import "~css/public.scss";.handleDialog {  @include cententCenterDialog;  .cropperContent {    display: flex;    justify-content: space-between;    padding-left: 20px;    .cropper {      width: 400px;      height: 400px;      border: 1px solid #DDD;    }    .previewBox {      flex: 1;      display: flex;      justify-content: center;      flex-direction: column;      align-items: center;      .title {        font-size: 18px;        height: 36px;        margin-bottom: 20px;      }      .showPreview {        flex: 1;        display: flex;        justify-content: center;        .preview {          overflow: hidden;          background: #eeeeee;          border: 1px solid #eeeeee;        }      }    }  }  .footerButton {    margin-top: 30px;    margin-left: 20px;    display: flex;    justify-content: flex-end;    .scopeButton {      width: 400px;      display: flex;      justify-content: space-between;    }    .uploadButton {      flex: 1;      display: flex;      justify-content: center;    }    .localButton {      cursor: pointer;      color: #ffffff;      background: #409eff;      padding: 10px 15px;      border-radius: 3px;      appearance: none;      display: flex;      align-self: center;      margin-right: 10px;    }    .inputFile {      width: 180px;      position: absolute;      clip: rect(0 0 0 0);    }  }}</style>

说明,支持网络图片也支持本地图片,图片如果需要上传,我是通过base64转文件,再上传的。

感谢各位的阅读,以上就是“vue-cropper怎么实现裁剪图片”的内容了,经过本文的学习后,相信大家对vue-cropper怎么实现裁剪图片这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

--结束END--

本文标题: vue-cropper怎么实现裁剪图片

本文链接: https://www.lsjlt.com/news/329676.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • vue-cropper怎么实现裁剪图片
    这篇文章主要讲解了“vue-cropper怎么实现裁剪图片”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“vue-cropper怎么实现裁剪图片”吧!先展示一下效果如何使用:1、安装 ...
    99+
    2023-06-30
  • vue-cropper实现裁剪图片
    本文实例为大家分享了vue-cropper实现裁剪图片的具体代码,供大家参考,具体内容如下 先展示一下效果 如何使用: 1、安装  npm install vue-cro...
    99+
    2022-11-13
  • Vue使用Vue-cropper实现图片裁剪
    前言 这两天想给图片添加一个图片裁剪的功能,因为之前的图片都是直接上传的,很多图片肯定在前台显示的时候,都不能很好的达到我们想要的效果,因此就需要我们在对个别图片进行细微调整,已达到...
    99+
    2022-11-13
  • vue中怎么使用vue-cropper裁剪图片
    这篇文章主要讲解了“vue中怎么使用vue-cropper裁剪图片”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“vue中怎么使用vue-cropper裁剪图片”吧!一、安装:npm ...
    99+
    2023-06-21
  • vue项目中借助vue-cropper做图片裁剪
    由于项目需要,需要做图片裁剪。之前的项目已经由cropper.js实现过,因为这次使用的是vue,所以采用了vue-cropper这个组件,使用起来很简单,但是坑也很多。(学习视频分享:vue视频教程)一、安装npm install vue...
    99+
    2022-11-22
    vue3 vue.js Vue
  • vue如何使用vue-cropper裁剪图片你知道吗
    目录一、安装:二、使用:三、内置方法:四、使用:总结 官网: https://github.com/xyxiao001/vue-cropper 一、安装: npm instal...
    99+
    2022-11-12
  • vue项目中如何使用vue-cropper做图片裁剪
    这篇文章主要介绍了vue项目中如何使用vue-cropper做图片裁剪的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇vue项目中如何使用vue-cropper做图片裁剪文章都会有所收获,下面我们一起来看看吧。一...
    99+
    2023-07-04
  • 前端vue cropperjs怎么实现图片裁剪
    这篇文章主要介绍“前端vue cropperjs怎么实现图片裁剪”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“前端vue cropperjs怎么实现图片裁剪”文章能帮助大家解决问题。图片裁剪图片裁剪...
    99+
    2023-07-02
  • Vue图片裁剪功能实现代码
    目录一、效果展示:1、表单的图片上传项:2、裁剪框页面二、代码部分1、首先安装Vue-Cropper,基于此组件的基础上开发的裁剪页面2、裁剪弹窗的组件编写:3、【图片上传表单项】组...
    99+
    2022-11-13
  • cropperjs怎么实现裁剪图片功能
    这篇“cropperjs怎么实现裁剪图片功能”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“cropperjs怎么实现裁剪图片...
    99+
    2023-06-29
  • 如何用Vue实现图片裁剪组件
    这篇文章主要介绍“如何用Vue实现图片裁剪组件”,在日常操作中,相信很多人在如何用Vue实现图片裁剪组件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何用Vue实现图片裁剪组件”的疑惑有所帮助!接下来,请跟...
    99+
    2023-06-20
  • 前端vue-cropperjs实现图片裁剪方案
    目录引言图片裁剪图片裁剪的流程vue-cropperjs的使用代码实现实现效果v-viewer的使用代码实现实现效果最后引言 做了个图片的裁剪的功能和预览的功能,前端图片的裁剪方案,...
    99+
    2022-11-13
  • 怎么用python裁剪图片
    这篇文章主要介绍“怎么用python裁剪图片”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么用python裁剪图片”文章能帮助大家解决问题。一、 安装PIL库如果此前没有安装过PIL库,在pyth...
    99+
    2023-07-02
  • cropperjs实现裁剪图片功能
    本文实例为大家分享了cropperjs实现裁剪图片功能的具体代码,供大家参考,具体内容如下 cropperjs (裁剪图片) vue版本 // 下载 // npm install c...
    99+
    2022-11-13
  • vue项目实现添加图片裁剪组件
    本文实例为大家分享了vue项目添加图片裁剪组件的具体代码,供大家参考,具体内容如下 功能如下图所示: 1、安装命令如下 npm i vue-cropper --save 2、调用组...
    99+
    2022-11-13
  • Vue图片裁剪组件实例代码
    示例: tip: 该组件基于vue-cropper二次封装 安装插件 npm install vue-cropper yarn add vue-cropper 写入封装...
    99+
    2022-11-12
  • 使用css怎么实现动态图片裁剪
    今天就跟大家聊聊有关使用css怎么实现动态图片裁剪,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。<!DOCTYPE html PUBLIC &qu...
    99+
    2023-06-09
  • Java是怎么实现图片裁剪功能的
    今天就跟大家聊聊有关Java是怎么实现图片裁剪功能的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。前言下面提供将图片按照自定义尺寸进行裁剪的Java工具类,一如既往的实用主义。Mav...
    99+
    2023-06-29
  • windows中coreldraw怎么裁剪图片
    本文小编为大家详细介绍“windows中coreldraw怎么裁剪图片”,内容详细,步骤清晰,细节处理妥当,希望这篇“windows中coreldraw怎么裁剪图片”文章能帮助大家解决疑惑,下面跟着小编的思...
    99+
    2022-12-02
    windows coreldraw
  • Vue实现简单基础的图片裁剪功能
    目录一、准备工作二、基本结构三、添加功能onMouseDownonMouseMoveonMouseUponMouseLeave四、总结近日,在写公司项目的时候接到一个需求:对已加载的...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作