image-to-base64
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -1,72 +0,44 @@ | ||
(function(escope){ | ||
"use strict"; | ||
function validUrl (url) { | ||
return /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi.test(url); | ||
'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 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')); | ||
} | ||
function validTypeImage (image) { | ||
return /(\.(jpg)|\.(png)|\.(jpeg))/gi.test(image); | ||
return null; | ||
} | ||
function isImage(urlOrImage) { | ||
if (validTypeImage(urlOrImage)) { | ||
return Promise.resolve(readFileAndConvert(urlOrImage)); | ||
} else { | ||
return Promise.reject('[*] Occurent some error... [validTypeImage] == false'); | ||
} | ||
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.buffer() | ||
}).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; | ||
} | ||
function imageToBase64(urlOrImage) { | ||
if (validUrl(urlOrImage)) { | ||
var fetch = require('node-fetch'); | ||
return fetch(urlOrImage).then(function(response) { | ||
return response.buffer(); | ||
}).then(base64ToNode); | ||
} else { | ||
escope.imageToBase64 = imageToBase64; | ||
return isImage(urlOrImage); | ||
} | ||
} | ||
})(this); | ||
module.exports = imageToBase64; |
@@ -1,1 +0,1 @@ | ||
!function(e){"use strict";function o(e){return window.btoa([].slice.call(new Uint8Array(e)).map(function(e){return String.fromCharCode(e)}).join(""))}function u(e){return e.toString("base64")}function s(e){return/(\.(jpg)|\.(png)|\.(jpeg))/gi.test(e)?Promise.resolve((n=e,t=require("fs"),r=require("path"),t.statSync(n).isFile()?u(t.readFileSync(r.resolve(n)).toString("base64")):null)):Promise.reject("[*] Occurent some error... [validTypeImage] == false");var n,t,r}function n(e,n){return"undefined"!=typeof window&&"document"in window&&"navigator"in window?(r=e,i=n,"fetch"in window&&"Promise"in window?fetch(r,i||{}).then(function(e){return e.arrayBuffer()}).then(o):Promise.reject("[*] It's image2base64 not compatible with your browser.")):/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi.test(t=e)?require("node-fetch")(t).then(function(e){return e.buffer()}).then(u):s(t);var t,r,i}"undefined"!=typeof module?module.exports=n:e.imageToBase64=n}(this); | ||
"use strict";function validUrl(e){return/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi.test(e)}function validTypeImage(e){return/(\.(jpg)|\.(png)|\.(jpeg))/gi.test(e)}function base64ToNode(e){return e.toString("base64")}function readFileAndConvert(e){var r=require("fs"),t=require("path");return r.statSync(e).isFile()?base64ToNode(r.readFileSync(t.resolve(e)).toString("base64")):null}function isImage(e){return validTypeImage(e)?Promise.resolve(readFileAndConvert(e)):Promise.reject("[*] Occurent some error... [validTypeImage] == false")}function imageToBase64(e){return validUrl(e)?require("node-fetch")(e).then(function(e){return e.buffer()}).then(base64ToNode):isImage(e)}module.exports=imageToBase64; |
{ | ||
"name": "image-to-base64", | ||
"version": "2.0.1", | ||
"description": "Generate a image to base64.", | ||
"main": "image-to-base64.min.js", | ||
"scripts": { | ||
"test": "mocha ./test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/renanbastos93/image-to-base64.git" | ||
}, | ||
"keywords": [ | ||
"node", | ||
"nodejs", | ||
"module convert base64 nodejs", | ||
"image2base64", | ||
"image-to-base64", | ||
"convert-image-base64", | ||
"convert", | ||
"save", | ||
"code", | ||
"base64", | ||
"image", | ||
"webpack", | ||
"loader", | ||
"img", | ||
"src", | ||
"img src" | ||
], | ||
"author": "Renan Bastos - <renanbastos.tec@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/renanbastos93/image-to-base64/issues" | ||
}, | ||
"homepage": "https://github.com/renanbastos93/image-to-base64#readme", | ||
"dependencies": { | ||
"node-fetch": "^1.7.3" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^5.0.1" | ||
} | ||
"name": "image-to-base64", | ||
"version": "2.1.0", | ||
"description": "Generate a image to base64.", | ||
"main": "image-to-base64.min.js", | ||
"scripts": { | ||
"minify:base": "uglifyjs image-to-base64.js -c -m -o image-to-base64.min.js", | ||
"minify:browser": "uglifyjs browser.js -c -m -o browser.min.js", | ||
"minify": "npm run minify:base && npm run minify:browser", | ||
"test": "mocha ./test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/renanbastos93/image-to-base64.git" | ||
}, | ||
"keywords": [ | ||
"node", | ||
"nodejs", | ||
"module convert base64 nodejs", | ||
"image2base64", | ||
"image-to-base64", | ||
"convert-image-base64", | ||
"convert", | ||
"save", | ||
"code", | ||
"base64", | ||
"image", | ||
"webpack", | ||
"loader", | ||
"img", | ||
"src", | ||
"img src" | ||
], | ||
"author": "Renan Bastos - <renanbastos.tec@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/renanbastos93/image-to-base64/issues" | ||
}, | ||
"homepage": "https://github.com/renanbastos93/image-to-base64#readme", | ||
"dependencies": { | ||
"node-fetch": "^1.7.3" | ||
}, | ||
"devDependencies": { | ||
"uglify-js": "^3.9.1", | ||
"mocha": "^7.1.1" | ||
} | ||
} |
@@ -7,2 +7,3 @@ # image-to-base64 | ||
[![Build Status](https://travis-ci.org/renanbastos93/image-to-base64.svg?branch=master)](https://travis-ci.org/renanbastos93/image-to-base64) | ||
![Tests](https://github.com/renanbastos93/image-to-base64/workflows/Tests/badge.svg) | ||
[![devDependencies Status](https://david-dm.org/renanbastos93/image-to-base64/dev-status.svg)](https://david-dm.org/renanbastos93/image-to-base64?type=dev) | ||
@@ -28,4 +29,4 @@ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/319a6e0b35474cf3ada5b0894e959b65)](https://www.codacy.com/app/renanbastos93/image-to-base64?utm_source=github.com&utm_medium=referral&utm_content=renanbastos93/image-to-base64&utm_campaign=Badge_Grade) | ||
```js | ||
const image2base64 = require('image-to-base64'); | ||
image2base64("path/to/file.jpg") // you can also to use url | ||
const imageToBase64 = require('image-to-base64'); | ||
imageToBase64("path/to/file.jpg") // you can also to use url | ||
.then( | ||
@@ -53,3 +54,3 @@ (response) => { | ||
```js | ||
image2base64("https://whatever-image/") | ||
imageToBase64("https://whatever-image/") | ||
.then( | ||
@@ -56,0 +57,0 @@ (response) => { |
@@ -10,3 +10,3 @@ const image2base64 = require("./../image-to-base64.js"); | ||
describe("must to be resolved the promise", function(){ | ||
it("get image of the url and convert to base64", function(){ | ||
@@ -21,3 +21,3 @@ image2base64(url) | ||
}); | ||
it("get image of the path and convert to base64", function(){ | ||
@@ -32,3 +32,3 @@ image2base64(path) | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
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
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
26982
12
67
2
81