image-to-base64
Advanced tools
Comparing version 1.3.5 to 2.0.0
@@ -1,41 +0,72 @@ | ||
"use strict"; | ||
(function(escope){ | ||
"use strict"; | ||
function validUrl (url) { | ||
return /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi.test(url); | ||
} | ||
function validTypeImage (image) { | ||
return /(\.(jpg)|\.(png)|\.(jpeg))/gi.test(image); | ||
} | ||
function base64ToBrowser (buffer) { | ||
return window.btoa([].slice.call(new Uint8Array(buffer)).map(function(bin) { return String.fromCharCode(bin) }).join("")); | ||
} | ||
function base64ToNode (buffer) { | ||
return buffer.toString("base64"); | ||
} | ||
function readFileAndConvert (fileName) { | ||
var fileSystem = require("fs"); | ||
var path = require("path"); | ||
if (fileSystem.statSync(fileName).isFile()) { | ||
return base64ToNode(fileSystem.readFileSync(path.resolve(fileName)).toString("base64")); | ||
} | ||
return null; | ||
} | ||
function isImage (urlOrImage) { | ||
if (validTypeImage(urlOrImage)) { | ||
return Promise.resolve(readFileAndConvert(urlOrImage)); | ||
} else { | ||
return Promise.reject("[*] Occurent some error... [validTypeImage] == false"); | ||
} | ||
} | ||
function isBrowser (urlOrImage, param) { | ||
if (!("fetch" in window && "Promise" in window)) { | ||
return Promise.reject("[*] It's image2base64 not compatible with your browser."); | ||
} | ||
return fetch(urlOrImage, param || {}).then(function(response){ | ||
return response.arrayBuffer() | ||
}).then(base64ToBrowser); | ||
} | ||
function isNodeJs (urlOrImage) { | ||
if (validUrl(urlOrImage)) { | ||
var fetch = require("node-fetch"); | ||
return fetch(urlOrImage).then(function(response){ | ||
return response.arrayBuffer() | ||
}).then(base64ToNode); | ||
} else { | ||
return isImage(urlOrImage); | ||
} | ||
} | ||
function imageToBase64(urlOrImage, param) { | ||
if (typeof window !== "undefined" && ("document" in window && "navigator" in window)) { | ||
return isBrowser(urlOrImage, param); | ||
} else { | ||
return isNodeJs(urlOrImage); | ||
} | ||
} | ||
if (typeof module !== "undefined") { | ||
module.exports = imageToBase64; | ||
} else { | ||
escope.imageToBase64 = imageToBase64; | ||
} | ||
const image2base64 = (url, param) => { | ||
return new Promise( | ||
(resolve, reject) => { | ||
let valid = new RegExp("(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?", "gi"); | ||
if(valid.test(param) === true){ | ||
fetch( | ||
url, | ||
param || {} | ||
).then( | ||
(response) => response.arrayBuffer() | ||
) | ||
.then( | ||
(buffer) => { | ||
return window.btoa( | ||
[].slice.call( | ||
new Uint8Array(buffer) | ||
).map( | ||
(bin) => String.fromCharCode(bin) | ||
).join("") | ||
); | ||
} | ||
) | ||
.then( | ||
(body) => { | ||
resolve(body); | ||
} | ||
).catch(reject); | ||
}else{ | ||
reject(null); | ||
} | ||
} | ||
); | ||
}; | ||
})(this); |
{ | ||
"name": "image-to-base64", | ||
"version": "1.3.5", | ||
"version": "2.0.0", | ||
"description": "Generate a image to base64.", | ||
"main": "index.js", | ||
"main": "image-to-base64.min.js", | ||
"scripts": { | ||
@@ -7,0 +7,0 @@ "test": "mocha ./test" |
@@ -28,3 +28,3 @@ # image-to-base64 | ||
const image2base64 = require('image-to-base64'); | ||
image2base64("path/to/file.jpg") | ||
image2base64("path/to/file.jpg") // you can also to use url | ||
.then( | ||
@@ -46,3 +46,3 @@ (response) => { | ||
```html | ||
<script src="node_modules/image-to-base64/image-to-base64.js"></script> | ||
<script src="node_modules/image-to-base64/image-to-base64.min.js"></script> | ||
``` | ||
@@ -56,3 +56,3 @@ After you can to use conforme the example above of the Node.JS, but you must use URL. | ||
(response) => { | ||
console.log(response); //data:image/jpeg;base64,iVBORw0KGgoAAAANSwCAIA... | ||
console.log(response); //iVBORw0KGgoAAAANSwCAIA... | ||
} | ||
@@ -59,0 +59,0 @@ ) |
@@ -1,6 +0,6 @@ | ||
const image2base64 = require("./../"); | ||
const image2base64 = require("./../image-to-base64.js"); | ||
const assert = require("assert"); | ||
const pt = require("path"); | ||
let url = "http://jaystack.com/wp-content/uploads/2015/12/nodejs-logo-e1497443346889.png"; | ||
let url = "https://jaystack.com/wp-content/uploads/2015/12/nodejs-logo-e1497443346889.png"; | ||
let path = pt.resolve("test/nodejs.png"); | ||
@@ -31,2 +31,2 @@ let validBase64 = new RegExp("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{0,2}==)$","gim"); | ||
}); | ||
}); |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
25807
10
90
1
2
3