vue头像裁剪插件_一个简单而优雅的头像裁剪和上传插件

Vue Avatar Cropper是一个简洁的Vue.js插件,用于实现头像裁剪和上传功能。它提供了安装、使用、属性和事件监听等详细指南,帮助开发者轻松集成到项目中。
摘要由CSDN通过智能技术生成

vue头像裁剪插件

Vue头像作物 (Vue Avatar Cropper)

A simple and elegant avatar cropping and upload plugin.

一个简单而优雅的头像裁剪和上传插件。

正在安装 (Installing)

$  npm i vue-avatar-cropper

用法 (Usage)

<template>
  <div class="text-center">
    <img v-if="userAvatar" :src="userAvatar">
    <button id="pick-avatar">Select an image</button>
    <avatar-cropper
      @uploaded="handleUploaded"
      trigger="#pick-avatar"
      upload-url="/files/upload" />
  </div>
</template>

<script>
  import AvatarCropper from "vue-avatar-cropper"

  export default {
    components: { AvatarCropper },
    data() {
      return {
          userAvatar: undefined,
      }
    },
    methods: {
      handleUploaded(resp) {
        this.userAvatar = resp.relative_url;
      }
    }
  }
</script>

物产 (Properties)

Property NameTypeDescription
triggerString|ElementThe element to trigger avatar pick
upload-urlStringUrl of upload croppped image
upload-form-nameObjectForm name of upload request, default: 'file'
upload-form-dataObjectAdditional form data, default: '{}'
upload-handlerFunctionHandler to replace default upload handler, the argument is cropperJS instance.
upload-headersObjectHeaders of upload request, default: {}
cropper-optionsObjectOptions passed to the cropperJS instance,
default: {
aspectRatio: 1,
autoCropArea: 1,
viewMode: 1,
movable: false,
zoomable: false
}
output-optionsObjectOptions passed to the cropper.getCroppedCanvas() method,
default: {width: 512, height: 512}
output-mimeStringThe resulting avatar image mime type, default: image/jpeg
output-qualityNumberThe resulting avatar image quality [0 - 1], default: 0.9
(if the output-mime property is image/jpeg or image/webp)
mimesStringAllowed image formats, default:
image/png, image/gif, image/jpeg, image/bmp, image/x-icon
labelsObjectLabel for buttons, default: { submit: "提交", cancel: "取消"}
物业名称 类型 描述
trigger 字符串|元素 触发头像选择的元素
upload-url 上传裁剪图片的网址
upload-form-name 目的 上载请求的表单名称,默认值:“文件”
upload-form-data 目的 其他表单数据,默认值:“ {}”
upload-handler 功能 替换默认上传处理程序的处理程序,其参数为cropperJS实例。
upload-headers 目的 上载请求的标题,默认值: {}
cropper-options 目的 传递给cropperJS实例的选项,
默认值: {
aspectRatio: 1,
autoCropArea: 1,
viewMode: 1,
movable: false,
zoomable: false
}
output-options 目的 传递给cropper.getCroppedCanvas()方法的选项,
默认值: {width: 512, height: 512}
output-mime 生成的头像图片mime类型,默认值: image/jpeg
output-quality 生成的头像图像质量[0-1],默认值: 0.9
(如果output-mime属性为image/jpegimage/webp )
mimes 允许的图像格式,默认:
image/png, image/gif, image/jpeg, image/bmp, image/x-icon
labels 目的 按钮的标签,默认值: { submit: "提交", cancel: "取消"}

大事记 (Events)

  • changed user picked a file

    更改的用户选择了一个文件

  • submit right after a click on the submit button

    提交按钮点击后立即提交

  • uploading before submit upload request, params:

    上传之前提交上传请求,则params:

  • uploaded after request is successful, params:

    请求成功后上传 ,参数:

    • response object, json parsed from xhr.responseText
    • response对象,从xhr.responseText解析为xhr.responseText
    • form object, form对象, FormData instance.FormData实例。
    • xhr object, xhr对象, XMLHttpRequest instance.XMLHttpRequest实例。
  • completed after request has completed, params:

    请求完成后完成,参数:

    • response object, json parsed from xhr.responseText
    • response对象,从xhr.responseText解析为xhr.responseText
    • form object, form对象, FormData instance.FormData实例。
    • xhr object, xhr对象, XMLHttpRequest instance.XMLHttpRequest实例。
  • error something went wrong, params:

    错误出了点问题,参数:

    • message error message.message错误消息。
    • type error type, example: type错误类型,例如: upload/upload / user.user
    • context context data.context上下文数据。

You can listen these events like this:

您可以这样听这些事件:

<avatar-cropper
    trigger="#set-avatar"
    upload-url="/files/upload"
    @uploading="handleUploading"
    @uploaded="handleUploaded"
    @completed="handleCompleted"
    @error="handlerError"
></avatar-cropper>
...
    methods: {
        ...
        handleUploading(form, xhr) {
            form.append('foo', 'bar')
        },
        handleUploaded(response, form, xhr) {
            // update user avatar attribute
        },
        handleCompleted(response, form, xhr) {
            // xhr.status
        },
        handlerError(message, type, xhr) {
          if (type == 'upload') {
            // xhr.response...
          }
        }
        ...
    }
    ...

翻译自: https://vuejsexamples.com/a-simple-and-elegant-avatar-cropping-and-upload-plugin/

vue头像裁剪插件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值