GithubHelp home page GithubHelp logo

dai-siki / vue-image-crop-upload Goto Github PK

View Code? Open in Web Editor NEW
2.1K 39.0 370.0 1.69 MB

A beautiful vue component for image cropping and uploading. (vue图片剪裁上传组件)

Home Page: https://dai-siki.github.io/vue-image-crop-upload/example-2/demo.html

JavaScript 93.55% HTML 0.27% CSS 0.95% Vue 4.72% SCSS 0.50%
vue image-crop file-upload image-uploader avatar

vue-image-crop-upload's Introduction

vue-image-crop-upload

A beautiful vue component for image crop and upload.

Notice: This component is designed for pc, not recommended for use on the mobile side.

GitHub issues GitHub forks GitHub stars Twitter

NPM

Change log

@3.0.0

  • Compatible vue3

Demo

Click me.

Screenshot

screenshot

Brower compatibility

IE10+

Install

npm

$ npm install vue-image-crop-upload

Usage

Props

Name Type Default Description
url String '' Server api path,like "/avatar/upload", If empty, will not be uploaded
method String 'POST' request http method
field String 'upload' Upload input filename, used for server side get the file stream.
value Boolean twoWay show or not
params Object null POST Params,like "{k:v}"
headers Object null POST request header,like "{k:v}"
langType String 'zh' language type
langExt Object language extend
width Number 200 width of receive image
height Number 200 height of receive image
imgFormat string 'png' jpg/png, Server api receive file type.
imgBgc string '#fff' background color, if the imgFormat prop is jpg
noCircle Boolean false disable circle preview
noSquare Boolean false disable square preview
noRotate Boolean true disable rotate function
withCredentials Boolean false support cross-domain

Events

Name Description
srcFileSet Once you've selected the file, params( fileName, fileType, fileSize )
cropSuccess image crop success, params( imageDataUrl, field )
cropUploadSuccess upload success, params( jsonData, field )
cropUploadFail upload fail, params( status, field )

Language package

view details.

Example Of "langExt"

{
    hint: 'Click or drag the file here to upload',
    loading: 'Uploading…',
    noSupported: 'Browser is not supported, please use IE10+ or other browsers',
    success: 'Upload success',
    fail: 'Upload failed',
    preview: 'Preview',
    btn: {
    	off: 'Cancel',
    	close: 'Close',
    	back: 'Back',
    	save: 'Save'
    },
    error: {
    	onlyImg: 'Image only',
    	outOfSize: 'Image exceeds size limit: ',
    	lowestPx: 'Image\'s size is too low. Expected at least: '
    }
}

Example vue@3

<div id="app">
	<a class="btn" @click="toggleShow">set avatar</a>
	<my-upload field="img"
        @crop-success="cropSuccess"
        @crop-upload-success="cropUploadSuccess"
        @crop-upload-fail="cropUploadFail"
        v-model="show"
		:width="300"
		:height="300"
		url="/upload"
		:params="params"
		:headers="headers"
		img-format="png"></my-upload>
	<img :src="imgDataUrl">
</div>

<script>
	import 'babel-polyfill'; // es6 shim
	import Vue from 'vue';
	import myUpload from 'vue-image-crop-upload';

	new Vue({
		el: '#app',
		data: {
			show: true,
			params: {
				token: '123456798',
				name: 'avatar'
			},
			headers: {
				smail: '*_~'
			},
			imgDataUrl: '' // the datebase64 url of created image
		},
		components: {
			'my-upload': myUpload
		},
		methods: {
			toggleShow() {
				this.show = !this.show;
			},
            /**
			 * crop success
			 *
			 * [param] imgDataUrl
			 * [param] field
			 */
			cropSuccess(imgDataUrl, field){
				console.log('-------- crop success --------');
				this.imgDataUrl = imgDataUrl;
			},
			/**
			 * upload success
			 *
			 * [param] jsonData  server api return data, already json encode
			 * [param] field
			 */
			cropUploadSuccess(jsonData, field){
				console.log('-------- upload success --------');
				console.log(jsonData);
				console.log('field: ' + field);
			},
			/**
			 * upload fail
			 *
			 * [param] status    server api return error status, like 500
			 * [param] field
			 */
			cropUploadFail(status, field){
				console.log('-------- upload fail --------');
				console.log(status);
				console.log('field: ' + field);
			}
		}
	});
</script>

Example vue@2

<div id="app">
	<a class="btn" @click="toggleShow">set avatar</a>
	<my-upload field="img"
        @crop-success="cropSuccess"
        @crop-upload-success="cropUploadSuccess"
        @crop-upload-fail="cropUploadFail"
        v-model="show"
		:width="300"
		:height="300"
		url="/upload"
		:params="params"
		:headers="headers"
		img-format="png"></my-upload>
	<img :src="imgDataUrl">
</div>

<script>
	import 'babel-polyfill'; // es6 shim
	import Vue from 'vue';
	import myUpload from 'vue-image-crop-upload/upload-2.vue';

	new Vue({
		el: '#app',
		data: {
			show: true,
			params: {
				token: '123456798',
				name: 'avatar'
			},
			headers: {
				smail: '*_~'
			},
			imgDataUrl: '' // the datebase64 url of created image
		},
		components: {
			'my-upload': myUpload
		},
		methods: {
			toggleShow() {
				this.show = !this.show;
			},
            /**
			 * crop success
			 *
			 * [param] imgDataUrl
			 * [param] field
			 */
			cropSuccess(imgDataUrl, field){
				console.log('-------- crop success --------');
				this.imgDataUrl = imgDataUrl;
			},
			/**
			 * upload success
			 *
			 * [param] jsonData  server api return data, already json encode
			 * [param] field
			 */
			cropUploadSuccess(jsonData, field){
				console.log('-------- upload success --------');
				console.log(jsonData);
				console.log('field: ' + field);
			},
			/**
			 * upload fail
			 *
			 * [param] status    server api return error status, like 500
			 * [param] field
			 */
			cropUploadFail(status, field){
				console.log('-------- upload fail --------');
				console.log(status);
				console.log('field: ' + field);
			}
		}
	});
</script>

Example vue@1

<div id="app">
	<a class="btn" @click="toggleShow">set avatar</a>
	<my-upload field="img"
		:width="300"
		:height="300"
		url="/upload"
		:params="params"
		:headers="headers"
        lang-type="en"
		:value.sync="show"
		img-format="png"></my-upload>
	<img :src="imgDataUrl">
</div>

<script>
	import 'babel-polyfill'; // es6 shim
	import Vue from 'vue';
	import myUpload from 'vue-image-crop-upload/upload-1.vue';

	new Vue({
		el: '#app',
		data: {
			show: true,
			params: {
				token: '123456798',
				name: 'avatar'
			},
			headers: {
				smail: '*_~'
			},
			imgDataUrl: '' // the datebase64 url of created image
		},
		components: {
			'my-upload': myUpload
		},
		methods: {
			toggleShow() {
				this.show = !this.show;
			}
		},
		events: {
			/**
			 * crop success
			 *
			 * [param] imgDataUrl
			 * [param] field
			 */
			cropSuccess(imgDataUrl, field){
				console.log('-------- crop success --------');
				this.imgDataUrl = imgDataUrl;
			},
			/**
			 * upload success
			 *
			 * [param] jsonData   server api return data, already json encode
			 * [param] field
			 */
			cropUploadSuccess(jsonData, field){
				console.log('-------- upload success --------');
				console.log(jsonData);
				console.log('field: ' + field);
			},
			/**
			 * upload fail
			 *
			 * [param] status    server api return error status, like 500
			 * [param] field
			 */
			cropUploadFail(status, field){
				console.log('-------- upload fail --------');
				console.log(status);
				console.log('field: ' + field);
			}
		}
	});

</script>

vue-image-crop-upload's People

Contributors

abensur avatar ali-turki avatar anteriovieira avatar attx avatar bananacoffee avatar bigperson avatar choirudin2210 avatar dai-siki avatar dvomaks avatar elialejandro avatar excaliburhan avatar godxavia avatar hardmode2015 avatar hekin1 avatar hieuhani avatar kangyoosam avatar katrine-marie avatar lschyi avatar mjamro avatar moosoul avatar moosti avatar phaberest avatar s950329 avatar sergiocastrovale avatar smhayhan avatar timothepearce avatar tintnaingwinn avatar valerymelou avatar vickvasquez avatar wormwlrm 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  avatar  avatar  avatar  avatar

vue-image-crop-upload's Issues

初始化时报错 [Vue warn]: Failed to resolve directive: el

从上面复制的代码,运行时报 [Vue warn]: Failed to resolve directive: el,弹框能正常弹出来。但是点上传的时候报Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "value" 错误。vue 版本是2.2.2 , vue-image-crop-upload版本是1.3.8

upload image have bug

I have find this component have bug in upload-2.vue

let client = new XMLHttpRequest();
                client.open('POST', url, true);
                client.onreadystatechange = function() {
                    if (this.readyState !== 4) {
                        return;
                    }
                    if (this.status === 200) {
                        resolve(JSON.parse(this.responseText));
                    } else {
                        reject(this.status);
                    }
                };
                client.upload.addEventListener("progress", uploadProgress, false); //监听进度
				// 设置header
	            if (typeof headers == 'object' && headers) {
	                Object.keys(headers).forEach((k) => {
	                    client.setRequestHeader(k, headers[k]);
	                })
	            }
                client.send(fmData);

the client.upload is Object- {} , find the addEventListener is undefined ,
but I delete upload to 'client.addEventListener("progress", uploadProgress,false)',the
progress not change.

Thank you, if you reply to me

支持移动端

您好,这个组件很炫酷,想问一下楼大大,什么时候支持移动端适配,基于vue的移动webApp现在比较流行。谢谢

Offset zooming and language pack

Hello Dai-siki, thanks for this very nice plugin. Can i ask your help.

When i load an image it zooms unevenly from the side but in the preview the image looks fine.

Also, how and were do i load the translation files?

Finally, i am uploading all my data at the same time using a form, is there a way to handle the upload in another part of the page and not inside vue-image-crop-upload?

Thank you very much.

upload.css

on upload-2.vue stlye section importing ./upload.css, but its loaded as localhost/upload.css
image

can you help me ?

Bug on iPhone browser

Some bugs I found when using iPhone

  1. Cannot drag image to reposition
  2. Wrong orientation (crop result) when selecting portrait photo
  3. Cannot zoom in and zoom out (slider and +/- button doesn't work)

Large image (10MP) looks jagged on resize

on resizing big image canvas default algorithm makes images look very ugly and jagged. I found out you can use https://github.com/danschumann/limby-resize to resize it smoothly without any artifacts in resized image. here is a demo, select a fairly large image (4000px wide) and see how it looks, this algorithm is not as fast as canvas default but its output image is very clean.

Please see if you can incorporate this or give an option if the user has limby-resize installed use this instead.

取消自动上传

用vue的话基本上是前后端分离的,而且token一般放cookie的,所以应该自己来控制上传这块,只需要把base64数据得到就行了。

Error while uglifying using Webpack

Webpack throws this error while uglifying the code.

ERROR in static/js/kite.4.76951c1af06af38f3f25.js from UglifyJs
Unexpected token: name (opts) [./~/vue-image-crop-upload/utils/effectRipple.js:9,0][static/js/kite.4.76951c1af06af38f3f25.js:3870,5]

In my opinion the library should not be using effectRipple because most of the users might not prefer it.

在运行webpack的时候出现错误

ERROR in ./~/vue-image-crop-upload/upload-2.vue
Module parse failed: node_modules/vue-image-crop-upload/upload-2.vue Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
|
|


|

@/js/entry.js 9:14-59

More useful arguments for handlers of crop-success

First thank you for making this component.

Actually crop-success is triggered with the following arguments: createImgUrl, field and ki. I have a use case here where I want to display the cropped image and wait for the user to click on a button before performing the upload. The upload must be done as a normal upload so I'm gonna need to do something like what you did in upload()
fmData.append(field, data2blob(createImgUrl, mime), field + '.' + imgFormat);
Is it possible to change the arguments sent to crop-success handlers so that they can be used to do some like that and also display the cropped image?

找不到 upload.css 文件

你好,直接安装该组件后,找不到upload.css 文件:
Failed to load resource: the server responded with a status of 404 upload.css (Not Found)

什么原因呢?

Crop but don't upload

Is it possible to crop without uploading? I am using vuex so i want to dispatch an action from the component.

浏览器缩小问题

缩小浏览器后,组件会浮在上层,无法关闭,卡住页面,建议屏幕小于组件时启动关闭部分

适配移动端

楼大大您好,这个组件很不错,如果适配成移动端会更佳。谢谢

Give option to add some label text on first step

Thanks for really beautiful and complete image crop uploader, If you can add a <slot> where we can add some text on the first step it will be great, currently it says nothing only a upload area. I wanted to add some text on the first step. like Please upload an image no more than 1 MB.

How to add custom headers?

Hi, i use jwt auth.
How to add custom headers?

method in :other-params="otherParams" or method in :params="otherParams" :

otherParams: {
    headers: {
        'Authorization': 'Bearer ...'
   }
}

not working :-(

vue 2.0.6

请教下报错的问题

你好,我用的是cli的模板工具,引用你的插件
oi6hz u 9qulb 7h u 3
isz0vr5 yiqm 2rug4d47
跟你推荐的写法一样的啊 但是引用报错,能劳烦帮忙看一下吗 谢谢啊

Give possibility to crop but not upload

We should be able to crop the image and not upload it directly to the server. Maybe by performing the upload only if url prop is evaluated to true.
The solution to the issue #21 is not acceptable in this case because the upload will still occur.
I can make a PR for this if needed.

Need to clear the error message

  • Choose one image,then turn off the network. It will show uploading error message
  • Turn on network, it will upload success, and show success message
  • Now, there are two message

Follows the screenshots:

  • error
  • continueupload
  • finally

配合element-ui使用时报Failed to resolve directive: el

<Avatar-upload field="avatar" @crop-success="cropSuccess" @crop-upload-success="cropUploadSuccess" @crop-upload-fail="cropUploadFail" v-model="uploadShow" :width="300" :height="300" url="/upload" :params="params" :headers="headers" img-format="png"></Avatar-upload>

import AvatarUpload from 'vue-image-crop-upload';
components: { 'Avatar-upload': AvatarUpload },
screen shot 2017-05-13 at 1 11 18 pm

缓存

图片比较复杂情况下,放大缩小或者拖动图片后会出现图片重叠
qq 20170227104859

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.