Created
October 26, 2017 19:28
-
-
Save RafaelC457ro/eed415676884c151fa3e43875e15541a to your computer and use it in GitHub Desktop.
Rxjs input file image compressed to base64 // source http://jsbin.com/tewiral
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Rxjs input file image compressed to base64</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.6/rx.all.js"></script> | |
<script src="https://unpkg.com/rx-dom@7.0.3/dist/rx.dom.js"></script> | |
<script src="https://cdn.rawgit.com/xkeshi/image-compressor/master/dist/image-compressor.js"></script> | |
</head> | |
<body> | |
<input type=file> | |
<script id="jsbin-javascript"> | |
const input = document.querySelector('input[type=file]') | |
Rx.DOM.change(input) | |
.flatMap( event => Rx.Observable.from(event.target.files) ) | |
.filter( file => file.type.match('image')) | |
.flatMap(file => { | |
const imageCompressor = new ImageCompressor() | |
const image = imageCompressor.compress(file, {quality: .6}) | |
return Rx.Observable.fromPromise(image) | |
}) | |
.map( file => { | |
return { | |
filename : file.name, | |
file: Rx.DOM.fromReader(file).asDataURL() | |
} | |
}) | |
.flatMap(fileInfo => { | |
return fileInfo.file.map(fileEncoded => { | |
fileInfo.file = fileEncoded | |
return fileInfo | |
}) | |
}) | |
.subscribe(data => { | |
console.log(data) | |
const image = new Image() | |
image.src = data.file | |
document.body.appendChild(image) | |
}) | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const input = document.querySelector('input[type=file]') | |
Rx.DOM.change(input) | |
.flatMap( event => Rx.Observable.from(event.target.files) ) | |
.filter( file => file.type.match('image')) | |
.flatMap(file => { | |
const imageCompressor = new ImageCompressor() | |
const image = imageCompressor.compress(file, {quality: .6}) | |
return Rx.Observable.fromPromise(image) | |
}) | |
.map( file => { | |
return { | |
filename : file.name, | |
file: Rx.DOM.fromReader(file).asDataURL() | |
} | |
}) | |
.flatMap(fileInfo => { | |
return fileInfo.file.map(fileEncoded => { | |
fileInfo.file = fileEncoded | |
return fileInfo | |
}) | |
}) | |
.subscribe(data => { | |
console.log(data) | |
const image = new Image() | |
image.src = data.file | |
document.body.appendChild(image) | |
}) | |
</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const input = document.querySelector('input[type=file]') | |
Rx.DOM.change(input) | |
.flatMap( event => Rx.Observable.from(event.target.files) ) | |
.filter( file => file.type.match('image')) | |
.flatMap(file => { | |
const imageCompressor = new ImageCompressor() | |
const image = imageCompressor.compress(file, {quality: .6}) | |
return Rx.Observable.fromPromise(image) | |
}) | |
.map( file => { | |
return { | |
filename : file.name, | |
file: Rx.DOM.fromReader(file).asDataURL() | |
} | |
}) | |
.flatMap(fileInfo => { | |
return fileInfo.file.map(fileEncoded => { | |
fileInfo.file = fileEncoded | |
return fileInfo | |
}) | |
}) | |
.subscribe(data => { | |
console.log(data) | |
const image = new Image() | |
image.src = data.file | |
document.body.appendChild(image) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment