Vue上传头像组件-基于vue-cropper

效果图: 

一、安装依赖

cnpm i -S vue-cropper ali-oss

二、引入组件

@/components/AvatarUploader/index.vue

<!--AvatarUploader-->
<template>
  <div class="AvatarUploader">
    <div>
      <span
        v-if="!avatarUrl"
        @click="openEditAvatarDialog()"
        style="
          display: inline-block;
          padding: 70px;
          border: 1px dashed gray;
          cursor: pointer;
        "
      >
        <el-tooltip effect="dark" content="上传头像" placement="top">
          <i class="el-icon-plus" />
        </el-tooltip>
      </span>

      <div v-else class="image">
        <el-image :src="avatarUrl" fit="fill" />
        <div class="mask">
          <el-tooltip effect="dark" content="预览头像" placement="top">
            <i
              class="el-icon-zoom-in"
              style="cursor: pointer"
              @click="isVisible_previewAvatarDialog = true"
            />
          </el-tooltip>
          <el-tooltip effect="dark" content="删除头像" placement="top">
            <i
              class="el-icon-delete"
              style="cursor: pointer"
              @click="deleteAvatar()"
            />
          </el-tooltip>
        </div>
      </div>
    </div>
    <!-- 预览头像窗口 -->
    <el-dialog :visible.sync="isVisible_previewAvatarDialog">
      <img width="100%" :src="avatarUrl" />
    </el-dialog>
    <!-- 修改头像窗口 -->
    <el-dialog
      title="修改头像"
      :visible.sync="isVisible_editAvatarDialog"
      width="800"
    >
      <el-row>
        <el-col :span="12" style="height: 300px">
          <vue-cropper
            ref="cropper"
            :img="options.img"
            :info="true"
            :autoCrop="options.autoCrop"
            :autoCropWidth="options.autoCropWidth"
            :autoCropHeight="options.autoCropHeight"
            :fixedBox="options.fixedBox"
            @realTime="realTime"
          >
          </vue-cropper>
          <div style="display: flex; padding-top: 20px">
            <el-upload
              ref="uploadCropper"
              :before-upload="beforeAvatarUpload"
              :show-file-list="false"
              action
              style="margin-right: 10px"
            >
              <el-button type="primary" size="small">上传头像</el-button>
            </el-upload>
            <el-tooltip
              class="item"
              effect="dark"
              content="向左旋转"
              placement="top"
            >
              <el-button size="small" @click="rotateLeft"
                ><i class="el-icon-refresh-left"></i
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="向右旋转"
              placement="top"
            >
              <el-button size="small" @click="rotateRight"
                ><i class="el-icon-refresh-right"></i
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="放大"
              placement="top"
            >
              <el-button size="small" @click="changeScale(1)"
                ><i class="el-icon-plus"></i
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="缩小"
              placement="top"
            >
              <el-button size="small" @click="changeScale(-1)"
                ><i class="el-icon-minus"></i
              ></el-button>
            </el-tooltip>
          </div>
        </el-col>
        <el-col :span="12" style="height: 300px">
          <div class="upload-preview">
            <img
              :src="previews.url"
              :style="previews.img"
              v-show="previews.url"
            />
          </div>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isVisible_editAvatarDialog = false" size="small"
          >取 消</el-button
        >
        <el-button
          type="primary"
          :loading="loading"
          @click="submitUpdate"
          size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { VueCropper } from "vue-cropper";
export default {
  name: "AvatarUploader",
  components: { VueCropper },
  props: {
    avatar: String,

    region: String,
    accessKeyId: String,
    accessKeySecret: String,
    bucket: String,
  },
  data() {
    return {
      isVisible_editAvatarDialog: false,
      isVisible_previewAvatarDialog: false,
      options: {
        img: "", //裁剪图片的地址
        info: true, //裁剪框的大小信息
        outputSize: 0.8, // 裁剪生成图片的质量
        outputType: "", // 裁剪生成图片的格式
        canScale: false, // 图片是否允许滚轮缩放
        autoCrop: true, //是否默认生成截图框
        autoCropWidth: 200, //默认生成截图框宽度
        autoCropHeight: 200,
        fixedBox: true, // 固定截图框大小 是否允许改变
        fixed: true, //是否开启截图框宽高固定比例
        fixedNumber: [1, 1], //截图框的宽高比例
        original: false, // 上传图片按照原始比例渲染
        centerBox: false, // 截图框是否被限制在图片里面
        infoTrue: true, // true 为展示真实输出图片宽高 false 展示看到的截图框宽高
      },
      previews: {},
      loading: false,
      avatarName: "", //头像名,包含后缀
      avatarUrl:this.avatar, //头像服务端链接
      client_alioss: {},
    };
  },

  created() {
    const OSS = require("ali-oss");
    this.client_alioss = new OSS({
      region: this.region,
      accessKeyId: this.accessKeyId,
      accessKeySecret: this.accessKeySecret,
      bucket: this.bucket,
    });
  },
  mounted() {},
  computed: {},
  methods: {
    //打开编辑头像窗口
    openEditAvatarDialog() {
      this.isVisible_editAvatarDialog = true;
      if (this.previews.url) {
        this.previews.url = "";
        this.options.img = "";
      }
    },
    // 实时预览函数
    realTime(data) {
      this.previews = data;
    },
    beforeAvatarUpload(file) {
      this.avatarName = this.$uuid.v1() + file.name; //包含后缀
      let uploadAccept = ["jpeg", "jpg", "png"]; //上传图片允许的格式
      let ext = file.type.split("/")[1]; //后缀名
      if (!uploadAccept.includes(ext)) {
        this.$message.warning("您上传的图片格式不符,请重新上传");
        return;
      }
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = () => {
        this.options.img = reader.result; //base64
      };
    },
    changeScale(num) {
      this.$refs.cropper.changeScale(num);
    },
    rotateLeft() {
      this.$refs.cropper.rotateLeft();
    },
    rotateRight() {
      this.$refs.cropper.rotateRight();
    },
    //删除服务器中的头像
    async deleteAvatar() {
      try {
        if (this.avatarUrl) {
          let { pathname } = new URL(this.avatarUrl);
          await this.client_alioss.deleteMulti([decodeURIComponent(pathname)]); //删除服务器中的头像。decodeURIComponent解决中文乱码
          this.$message.success("头像删除成功");
          this.avatarUrl = "";
          this.$emit("deleteAvatar");
        }
      } catch (error) {
        this.$message.error("头像删除失败");
      }
    },
    //上传头像
    submitUpdate() {
      try {
        this.loading = true;
        this.$refs.cropper.getCropData(async (base64) => {
          let file_img = this.base64toFile(base64, this.avatarName);
          let { name } = file_img;
          let {
            res: { requestUrls },
          } = await this.client_alioss.multipartUpload(name, file_img);
          this.deleteAvatar(); //删除服务器中的旧头像
          this.avatarUrl = requestUrls[0]; //展示新头像
          this.$message.success("上传头像成功");
          this.$emit("avatarUrl", this.avatarUrl); //把头像url传出去
        });
      } catch (error) {
        this.$message.error("上传头像失败");
      } finally {
        this.loading = false;
        this.isVisible_editAvatarDialog = false;
      }
    },
    //base64转换为file类型
    base64toFile(base64, filename) {
      let arr = base64.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 });
    },
  },
  watch: {},
};
</script>

<style lang="scss" scoped>
.AvatarUploader {
  .image {
    position: relative;
    display: inline-block;
    width: 150px;
    height: 150px;
    .mask {
      opacity: 0;
      color: white;
      font-size: 25px;
      position: absolute;
      top: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.5);

      display: flex;
      justify-content: space-evenly;
      align-items: center;

      &:hover {
        opacity: 1;
      }
    }
  }

  .upload-preview {
    position: relative;
    top: 50%;
    transform: translate(50%, -50%);
    width: 200px;
    height: 200px;
    border-radius: 50%;
    box-shadow: 0 0 4px #bbbbbb;
    overflow: hidden;
  }
}
</style>

三、使用

<template>
  <div style="text-align: center">
    <AvatarUploader
        :avatar="avatar"
        :region="options_OSS.region"
        :accessKeyId="options_OSS.accessKeyId"
        :accessKeySecret="options_OSS.accessKeySecret"
        :bucket="options_OSS.bucket"
        @avatarUrl="getAvatarUrl"
        @deleteAvatar="deleteAvatar"
    ></AvatarUploader>
  </div>
</template>
<script>
import AvatarUploader from "@/components/AvatarUploader/index.vue";
export default {
  components: { AvatarUploader },
  data() {
    return {
      options_OSS: {
        region: "oss-cn-chengdu",
        accessKeyId: "xxx",
        accessKeySecret: "xxx",
        bucket: "yimiao-manage",
      },
      avatar:""
    };
  },
  methods: {
    getAvatarUrl(avatarUrl) {
      console.log("头像url:", avatarUrl);
    },
    deleteAvatar() {
    }

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
你好!关于Vue裁剪组件,我推荐你使用vue-cropper组件。它是一个功能强大且易于使用的图片裁剪工具,可以在Vue项目中方便地集成和使用。 Vue-cropper提供了一些常见的图片裁剪功能,包括缩放、旋转、翻转、裁剪框调整等。你可以使用它来实现用户上传图片后的裁剪操作,适用于头像上传、图片编辑等场景。 使用vue-cropper组件很简单,首先你需要安装它: ``` npm install vue-cropper ``` 然后,在你的Vue组件中引入并使用它: ```vue <template> <div> <vue-cropper ref="cropper" :src="imageUrl" :config="cropperConfig" ></vue-cropper> <button @click="cropImage">裁剪图片</button> </div> </template> <script> import VueCropper from 'vue-cropper'; export default { components: { VueCropper, }, data() { return { imageUrl: 'https://example.com/image.jpg', cropperConfig: { aspectRatio: 1, // 裁剪框宽高比例 viewMode: 1, // 显示模式:0表示自由模式,1表示限制裁剪框在图片内 dragMode: 'move', // 拖拽模式 }, }; }, methods: { cropImage() { const cropper = this.$refs.cropper.getCropper(); const croppedImageData = cropper.getCroppedCanvas().toDataURL(); // 处理裁剪后的图片数据 }, }, }; </script> ``` 以上是一个简单的示例代码,其中`imageUrl`是待裁剪的图片地址,`cropperConfig`是裁剪工具的配置选项。你可以根据自己的需求自定义配置。 在裁剪按钮点击事件中,我们通过`this.$refs.cropper.getCropper()`获取裁剪实例,然后使用`.getCroppedCanvas().toDataURL()`方法获取裁剪后的图片数据,你可以根据需要对其进行进一步的处理。 希望这个组件能满足你的需求,如果还有其他问题,请随时提问!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值