GithubHelp home page GithubHelp logo

xyxiao001 / vue-cropper Goto Github PK

View Code? Open in Web Editor NEW
4.2K 35.0 689.0 1.79 MB

A simple picture clipping plugin for vue

Home Page: https://github.xyxiao.cn/vue-cropper/docs/vue3.html

License: MIT License

HTML 8.07% Vue 90.06% JavaScript 0.84% TypeScript 0.84% Shell 0.20%
vue cropper javascript vue2 vue3 crop vue-cropper

vue-cropper's Introduction

vue-cropper

一个优雅的图片裁剪插件

[ 查看演示 Demo ]
[ README_english ]
[ 更新日志 ]

一、安装使用

1. 安装

# npm 安装
npm install vue-cropper
# yarn 安装
yarn add vue-cropper

如果你没有使用 npm

在线例子vue-cropper + vue.2x

在线例子vue-cropper@next + vue.3x

服务器渲染 nuxt 解决方案 设置为 ssr: false

module.exports = {
  ...
  build: {
    vendor: [
      'vue-cropper
    ...
    plugins: [
      { src: '~/plugins/vue-cropper', ssr: false }
    ]
  }
}

2. 引入 Vue Cropper

Vue 3 组件内引入

npm install vue-cropper@next
import 'vue-cropper/dist/index.css'
import { VueCropper }  from "vue-cropper";

Vue3 全局引入

import VueCropper from 'vue-cropper'; 
import 'vue-cropper/dist/index.css'

const app = createApp(App)
app.use(VueCropper)
app.mount('#app')

Vue3 CDN 方式引入

<style type="text/css" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.css"></style> 
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.global.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue-cropper.umd.js"></script>
const app = Vue.createApp({...});
app.component('vue-cropper', window['vue-cropper'].VueCropper);

Vue2 组件内引入

import { VueCropper }  from 'vue-cropper' 
components: {
  VueCropper
}

Vue2 全局引入

import VueCropper from 'vue-cropper'
Vue.use(VueCropper)

Vue2 CDN 方式引入

<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/index.js"></script>
Vue.use(window['vue-cropper'].default)

nuxt 引入方式

if(process.browser) {
  vueCropper = require('vue-cropper')
  Vue.use(vueCropper.default)
}

3. 代码中使用

重要! 需要关掉本地的 mock 服务, 不然图片转化会报错 重要! 需要使用外层容器包裹并设置宽高

<vueCropper
  ref="cropper"
  :img="option.img"
  :outputSize="option.size"
  :outputType="option.outputType"
></vueCropper>

二、文档

1. props

目前还不知道什么原因项目里面开启 mock 会导致 file 报错,建议使用时关掉 mock

名称 功能 默认值 可选值
img 裁剪图片的地址 url 地址, base64, blob
outputSize 裁剪生成图片的质量 1 0.1 ~ 1
outputType 裁剪生成图片的格式 jpg (jpg 需要传入jpeg) jpeg, png, webp
info 裁剪框的大小信息 true true, false
canScale 图片是否允许滚轮缩放 true true, false
autoCrop 是否默认生成截图框 false true, false
autoCropWidth 默认生成截图框宽度 容器的 80% 0 ~ max
autoCropHeight 默认生成截图框高度 容器的 80% 0 ~ max
fixed 是否开启截图框宽高固定比例 false true, false
fixedNumber 截图框的宽高比例, 开启fixed生效 [1, 1] [ 宽度 , 高度 ]
full 是否输出原图比例的截图 false true, false
fixedBox 固定截图框大小 不允许改变 false
canMove 上传图片是否可以移动 true true, false
canMoveBox 截图框能否拖动 true true, false
original 上传图片按照原始比例渲染 false true, false
centerBox 截图框是否被限制在图片里面 false true, false
high 是否按照设备的dpr 输出等比例图片 true true, false
infoTrue true 为展示真实输出图片宽高 false 展示看到的截图框宽高 false true, false
maxImgSize 限制图片最大宽度和高度 2000 0 ~ max
enlarge 图片根据截图框输出比例倍数 1 0 ~ max(建议不要太大不然会卡死的呢)
mode 图片默认渲染方式 contain contain , cover, 100px, 100% auto
limitMinSize 裁剪框限制最小区域 10 Number, Array, String
fillColor 导出时背景颜色填充 #ffffff, white

2. 可用回调方法

  • @realTime 实时预览事件
  • @imgMoving 图片移动回调函数
  • @cropMoving 截图框移动回调函数
  • @imgLoad 图片加载的回调, 返回结果 success, error

@realTime 实时预览事件

realTime(data) {
  var previews = data
  var h = 0.5
  var w = 0.2

  this.previewStyle1 = {
    width: previews.w + "px",
    height: previews.h + "px",
    overflow: "hidden",
    margin: "0",
    zoom: h
  }

  this.previewStyle2 = {
    width: previews.w + "px",
    height: previews.h + "px",
    overflow: "hidden",
    margin: "0",
    zoom: w
  }

  // 固定为 100 宽度
  this.previewStyle3 = {
    width: previews.w + "px",
    height: previews.h + "px",
    overflow: "hidden",
    margin: "0",
    zoom: 100 / preview.w
  }

  // 固定为 100 高度
  this.previewStyle4 = {
    width: previews.w + "px",
    height: previews.h + "px",
    overflow: "hidden",
    margin: "0",
    zoom: 100 / preview.h
  }
  this.previews = data
}
<div class="show-preview" :style="{'width': previews.w + 'px', 'height': previews.h + 'px',  'overflow': 'hidden',
    'margin': '5px'}">
  <div :style="previews.div">
    <img :src="option.img" :style="previews.img">
  </div>
</div>
<p>中等大小</p>
<div :style="previewStyle1"> 
  <div :style="previews.div">
    <img :src="previews.url" :style="previews.img">
  </div>
</div>

<p>迷你大小</p>
<div :style="previewStyle2"> 
  <div :style="previews.div">
    <img :src="previews.url" :style="previews.img">
  </div>
</div>

@imgMoving 图片移动回调函数

返回的参数内容

{
   moving: true, // moving 是否在移动
   axis: {
     x1: 1, // 左上角
     x2: 1// 右上角
     y1: 1// 左下角
     y2: 1 // 右下角
   }
 }

@cropMoving 截图框移动回调函数

返回的参数内容

{
   moving: true, // moving 是否在移动
   axis: {
     x1: 1, // 左上角
     x2: 1// 右上角
     y1: 1// 左下角
     y2: 1 // 右下角
   }
 }

2. 内置方法 和 属性

通过 this.$refs.cropper 调用

属性

属性 说明
this.$refs.cropper.cropW 截图框宽度
this.$refs.cropper.cropH 截图框高度

方法

方法 说明
this.$refs.cropper.startCrop() 开始截图
this.$refs.cropper.stopCrop() 停止截图
this.$refs.cropper.clearCrop() 清除截图
this.$refs.cropper.changeScale() 修改图片大小 正数为变大 负数变小
this.$refs.cropper.getImgAxis() 获取图片基于容器的坐标点
this.$refs.cropper.getCropAxis() 获取截图框基于容器的坐标点
this.$refs.cropper.goAutoCrop 自动生成截图框函数
this.$refs.cropper.rotateRight() 向右边旋转90度
this.$refs.cropper.rotateLeft() 向左边旋转90度

获取截图内容

获取截图的 base64 数据

this.$refs.cropper.getCropData(data => {
  // do something
  console.log(data)  
})

获取截图的 blob 数据

this.$refs.cropper.getCropBlob(data => {
  // do something
  console.log(data)  
})

三、相关文章参考

四、交流

有什么意见,或者 bug,或者想一起开发 vue-cropper, 或者想一起开发其他插件 image

vue-cropper's People

Contributors

akiq2016 avatar chenhw2017 avatar dependabot[bot] avatar doter1995 avatar epsirom avatar fxxqq avatar gguney avatar hzsrc avatar imaxing avatar kylebing avatar laqking avatar lylanfeng avatar mmk909 avatar moudicat avatar panghuangdehaozi avatar peterchen1997 avatar powolnymarcel avatar qiupanfeng avatar senasiko avatar unsad avatar xyxiao001 avatar yiiu avatar yxzt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-cropper's Issues

指定截图尺寸大小

当按原图比例输出时(即 props = true)
1、指定截图尺寸大小
2、指定截图的最大宽度,当截图宽度大于最大宽度时,按照最大宽度输出

ios拍照之后无法显示在剪辑图片容器中?

1.拍完照之后,报错canvas.toBlob is not a function
pfnet/PaintsChainer#51
尝试了下vue-cropper依赖exif-js降级到2.2.0版本也不会报错,但是还是会出现下面的问题

2.用这个方法解决后,没有报错,但是还是无法在剪辑局域正常显示所拍照片

测试机型:iPhone5s(IOS 8.1)、iPhone 7 plus(IOS10.3.3)

能否让大尺寸的图片按照原始尺寸初始化?

能否让大尺寸的图片按照原始尺寸初始?或者是50%放大倍数初始化。。

这里有个需求是,用户用手机对着桌子上的文件拍照,然后通过这个上传。部分用户不会去缩放,直接传上来了。原本手机拍出来一个1920X1080的图 变长了一个极小的图。所有的文字都看不清。所以 如果这个插件能实现,当用户选择了图片之后,展示给用户的是一张放大了的,用户就会自己去选择需要截图的位置。

设置截图宽高比无效

设置canScale: false,
autoCrop: true,
// 只有自动截图开启 宽度高度才生效
autoCropWidth: 320,
autoCropHeight: 200,
// 开启宽度和高度比例
fixed: true,
fixedNumber: [1, 1]

容器内显示不对啊

为什么我用的会是这个样子呢??

image

.avatar-box {
            position: relative;
            height: 500px;
        }
<div class="avatar-box">
                    <VueCropper ref="cropper"
                    :img="option.img"
                    :autoCrop="true"
                    :outputType="option.outputType"></VueCropper>
                </div>

设置vue-cropper有点问题,能不能帮我看下是哪里出现的问题?

image

<template>
  <div class="edit-teacher-container">
        <Card>
                <div slot="title">
                    <Button type="text" icon="reply" @click.native="returnTeacherPage">返回</Button>
                </div>          
                <Button slot="extra" type="primary" icon="plus-round" @click.native="saveTeacher">保存</Button>
                <Row :gutter="16">
                    <Col span="10">
                        <Card style="height:500px">
                            <p slot="title">
                                <Icon type="person"></Icon>
                                设置老师头像
                            </p>
                                <Button type="primary" @click="modal1 = true">上传文件</Button>


                        </Card>
                    </Col>
                </Row>
        </Card>

        <Modal
            v-model="modal1"
            title="设置老师头像"
            width=800
            @on-ok="ok"
            @on-cancel="cancel">
            <vueCropper
                ref="cropper2"
                :img="example2.img"
                :outputSize="example2.size"
                :outputType="example2.outputType"
                :info="example2.info"
                :canScale="example2.canScale"
                :autoCrop="example2.autoCrop"
                :autoCropWidth="example2.width"
                :autoCropHeight="example2.height"
                :fixed="example2.fixed"
                :fixedNumber="example2.fixedNumber"
            ></vueCropper>
    </Modal>
  </div>
</template>
<script>
import vueCropper from 'vue-cropper'


export default {
    components:{
        vueCropper
    },
data () {
    return {
        modal1: false,
        example2: {
				img: 'http://ofyaji162.bkt.clouddn.com/bg1.jpg',
				info: true,
				size: 1,
				outputType: 'jpeg',
				canScale: false,
				autoCrop: true,
				// 只有自动截图开启 宽度高度才生效
				autoCropWidth: 300,
				autoCropHeight: 300,
				// 开启宽度和高度比例
				fixed: true,
				fixedNumber: [1, 1]
			}
    }
}
</script>


希望添加背景色支持

在我使用透明png图片的时候, 默认切割出来的是黑色背景色,
应该很多人都存在需要透明背景色的需求吧,如下图所示
cropper-touming
gougou-butouming

不能等比例缩放截图框? 宽高一比一

html

                     <VueCropper
                            ref="cropper"
                            :img="example2.img"
                            :outputSize="example2.size"
                            :outputType="example2.outputType"
                            :info="example2.info"
                            :canScale="example2.canScale"
                            :autoCrop="example2.autoCrop"
                            :autoCropWidth="example2.width"
                            :autoCropHeight="example2.height"
                    ></VueCropper>

js

                    img: 'http://ofyaji162.bkt.clouddn.com/bg1.jpg',    // 图片地址
                    info: false,    // 宽高信息
                    size: 1,        // 图片质量
                    outputType: 'jpeg', // 输出格式
                    canScale: false,    // 底图是否缩放
                    autoCrop: true,     // 自动截图框
                    // 只有自动截图开启 宽度高度才生效
                    autoCropWidth: 300,
                    autoCropHeight: 250,
                    // 开启宽度和高度比例
                    fixed: true,
                    fixedNumber: [1, 1],

java后台如何获取image值啊?

我现在不知道java后台如何获取这个data值

this.$refs.cropper.getCropData((data) => {
                this.$refs.avatarForm.reset();
                this.$post(`${this.$url}/teacher/uploadFileToQiniu`,{
                    imgStream : data
                })
                .then(res=>{

                })
                .catch(err=>{

                })
            })

不支持在Modal中使用

在使用iview的Modal组件中嵌入本插件,
Modal刚刚开始是隐藏的,点击按钮打开后,
初始化的数据全部失效,而且也无法切图,如下图所示
08ff7ca8-31d5-4c0f-904a-962209c344a8

上传服务端

如何将裁剪的数据传递给服务端,getCropData这个方法返回的只是一个base64编码,getCropBlob这个方法返回的是blob,这个blob数据是裁剪后的图片的流吗?

图片旋转的支持

对于网页上传的图片,不是所有的图片展示方向都是正的。所以有旋转图片会更好一点。
==,上传了一个竖着拍的图片,展示方向是横着。

怎么设置viewMode参数?

在手机上图片比例没有铺满,看了cropper文档里面有viewMode,但是在你开发的vue-cropper上好像缺少了这个支持,那么怎么设置viewMode?

希望添加服务器渲染支持

RT,我使用nuxt,为这个组件关闭了ssr,但是还是会在浏览器控制台得到一条警告信息。

The client-side rendered virtual DOM tree is not matching server-rendered content. This is likely caused by incorrect HTML markup, for example nesting block-level elements inside

, or missing. Bailing hydration and performing full client-side render.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.