├── .gitignore ├── Gruntfile.js ├── README.md ├── base64-img.js ├── package.json └── test ├── img ├── car.svg └── demo.png └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | test.js 3 | npm-debug.log 4 | test/dest/ 5 | 6 | .DS_Store -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | grunt.loadNpmTasks('grunt-contrib-jshint'); 3 | grunt.initConfig({ 4 | jshint: { 5 | all: [ 6 | 'base64-img.js' 7 | ] 8 | } 9 | }); 10 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # base64-img 2 | Convert img to base64, or convert base64 to img 3 | ```js 4 | var base64Img = require('base64-img'); 5 | ``` 6 | ## install 7 | ``` 8 | npm install base64-img --save 9 | ``` 10 | ## test 11 | ``` 12 | mocha 13 | ``` 14 | ## API 15 | ### .base64(filename, callback) 16 | Convert image file to image base64 data 17 | * {string} ``filename`` required 18 | The image path 19 | * {function} ``callback(err, data)`` required 20 | Callback with image base64 data 21 | ```js 22 | base64Img.base64('path/demo.png', function(err, data) {}) 23 | ``` 24 | 25 | ### .base64Sync(filename) 26 | The api same as base64, but it's synchronous 27 | ```js 28 | var data = base64Img.base64Sync('path/demo.png'); 29 | var data2 = base64Img.base64Sync('path/demo.svg'); 30 | ``` 31 | 32 | ### .requestBase64(url, callback) 33 | * {string} ``url`` required 34 | * {function} ``callback(err, res, body)`` required 35 | Callback with http request 36 | ```js 37 | var url = 'http://../demo.png'; 38 | base64Img.requestBase64(url, function(err, res, body) { 39 | 40 | }); 41 | ``` 42 | 43 | ### .img(data, destpath, name, callback) 44 | Convert image base64 data to image 45 | * {string} ``data`` required 46 | Image base64 data 47 | * {string} ``destpath`` required 48 | Dest path, if the destpath is root, pass empty string 49 | * {string} ``name`` required 50 | The image's filename 51 | * {function} ``callback(err, filepath)`` required 52 | ```js 53 | base64Img.img('data:image/png;base64,...', 'dest', '1', function(err, filepath) {}); 54 | ``` 55 | 56 | ### .imgSync(data, destpath, name) 57 | The api same as img, but it's synchronous 58 | ```js 59 | var filepath = base64Img.imgSync('data:image/png;base64,...', '', '2'); 60 | ``` -------------------------------------------------------------------------------- /base64-img.js: -------------------------------------------------------------------------------- 1 | var fs = require('file-system'); 2 | var path = require('path'); 3 | var request = require('ajax-request'); 4 | 5 | function base64(filename, data) { 6 | var extname = path.extname(filename).substr(1); 7 | extname = extname || 'png'; 8 | 9 | if (extname === 'svg') { 10 | extname = "svg+xml" 11 | } 12 | 13 | return 'data:image/' + extname + ';base64,' + data.toString('base64'); 14 | } 15 | 16 | function img(data) { 17 | var reg = /^data:image\/([\w+]+);base64,([\s\S]+)/; 18 | var match = data.match(reg); 19 | var baseType = { 20 | jpeg: 'jpg' 21 | }; 22 | 23 | baseType['svg+xml'] = 'svg' 24 | 25 | if (!match) { 26 | throw new Error('image base64 data error'); 27 | } 28 | 29 | var extname = baseType[match[1]] ? baseType[match[1]] : match[1]; 30 | 31 | return { 32 | extname: '.' + extname, 33 | base64: match[2] 34 | }; 35 | } 36 | 37 | /** 38 | * @description 39 | * Get image file base64 data 40 | * @example 41 | * base64Img.base64('path/demo.png', function(err, data) {}) 42 | */ 43 | exports.base64 = function(filename, callback) { 44 | if (!callback) callback = util.noop; 45 | 46 | fs.readFile(filename, function(err, data) { 47 | if (err) return callback(err); 48 | 49 | callback(null, base64(filename, data)); 50 | }); 51 | }; 52 | 53 | /** 54 | * @description 55 | * The api same as base64, but it's synchronous 56 | * @example 57 | * var data = base64Img.base64Sync('path/demo.png'); 58 | */ 59 | exports.base64Sync = function(filename) { 60 | var data = fs.readFileSync(filename); 61 | 62 | return base64(filename, data); 63 | }; 64 | 65 | /** 66 | * @description 67 | * Get base64 from url 68 | * @example 69 | * request.base64( 70 | * 'http://webresource.c-ctrip.com/ResCRMOnline/R5/html5/images/57.png', 71 | * function(err, res, body) { 72 | * 73 | * } 74 | * ); 75 | */ 76 | exports.requestBase64 = function(url, callback) { 77 | request({ 78 | url: url, 79 | isBuffer: true 80 | }, function (err, res, body) { 81 | if (err) return callback(err); 82 | 83 | var data = 'data:' + res.headers['content-type'] + ';base64,' + body.toString('base64'); 84 | callback(err, res, data); 85 | }); 86 | }; 87 | 88 | /** 89 | * @description 90 | * Convert image base64 data to img 91 | * @example 92 | * base64Img.img('data:image/png;base64,...', 'dest', '1', function(err, filepath) {}); 93 | */ 94 | exports.img = function(data, destpath, name, callback) { 95 | var result = img(data); 96 | var filepath = path.join(destpath, name + result.extname); 97 | 98 | fs.writeFile(filepath, result.base64, { encoding: 'base64' }, function(err) { 99 | callback(err, filepath); 100 | }); 101 | }; 102 | 103 | /** 104 | * @description 105 | * The api same as img, but it's synchronous 106 | * @example 107 | * var filepath = base64Img.imgSync('data:image/png;base64,...', 'dest', '1'); 108 | */ 109 | exports.imgSync = function(data, destpath, name) { 110 | var result = img(data); 111 | var filepath = path.join(destpath, name + result.extname); 112 | 113 | fs.writeFileSync(filepath, result.base64, { encoding: 'base64' }); 114 | return filepath; 115 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "base64-img", 3 | "version": "1.0.4", 4 | "description": "Convert img or svg to base64, or convert base64 to img", 5 | "main": "base64-img.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/douzi8/base64-img.git" 12 | }, 13 | "keywords": [ 14 | "base64", 15 | "img", 16 | "image", 17 | "img base64", 18 | "svg" 19 | ], 20 | "author": "douzi ", 21 | "license": "ISC", 22 | "bugs": { 23 | "url": "https://github.com/douzi8/base64-img/issues" 24 | }, 25 | "homepage": "https://github.com/douzi8/base64-img", 26 | "dependencies": { 27 | "ajax-request": "^1.2.0", 28 | "file-system": "^2.1.0" 29 | }, 30 | "devDependencies": { 31 | "grunt": "^0.4.5", 32 | "grunt-contrib-jshint": "^0.10.0" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /test/img/car.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/img/demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/douzi8/base64-img/28af7acdc67543067c882125183314471a88435e/test/img/demo.png -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var fs = require('fs'); 3 | var path = require('path'); 4 | var base64Img = require('../base64-img'); 5 | 6 | function getpath(filepath) { 7 | return path.join(__dirname, filepath); 8 | } 9 | 10 | function readFileSync(filepath, encoding) { 11 | return fs.readFileSync(path.join(__dirname, filepath), { encoding: encoding || 'base64' }); 12 | } 13 | 14 | describe('Test', function() { 15 | it('All test', function(done) { 16 | var demo = readFileSync('img/demo.png'); 17 | 18 | base64Img.base64(getpath('img/demo.png'), function(err, data) { 19 | base64Img.img(data, getpath('dest'), '1', function(err, filepath) { 20 | assert.equal(filepath, getpath('dest/1.png')); 21 | assert.equal(demo, readFileSync('dest/1.png')); 22 | done(); 23 | }); 24 | }); 25 | }); 26 | 27 | it('All test sync', function() { 28 | var demo = readFileSync('img/demo.png'); 29 | var data = base64Img.base64Sync(getpath('img/demo.png')); 30 | var filepath = base64Img.imgSync(data, getpath('dest'), '2'); 31 | 32 | assert.equal(filepath, getpath('dest/2.png')); 33 | assert.equal(demo, readFileSync('dest/2.png')); 34 | }); 35 | 36 | describe('img', function() { 37 | it('Image base64 data error', function() { 38 | assert.throws( 39 | function() { 40 | base64Img.imgSync('data:'); 41 | }, 42 | function(err) { 43 | if (/base64/.test(err)) { 44 | return true; 45 | } 46 | } 47 | ); 48 | }); 49 | 50 | it('jpeg', function() { 51 | var data = base64Img.base64Sync(getpath('img/demo.png')).replace('image/png', 'image/jpeg'); 52 | var filepath = base64Img.imgSync(data, getpath('dest'), '3'); 53 | assert.equal('.jpg', path.extname(filepath)); 54 | }); 55 | 56 | it('request', function(done) { 57 | var url = 'http://webresource.c-ctrip.com/ResCRMOnline/R5/html5/images/57.png'; 58 | var demo = readFileSync('img/demo.png'); 59 | var data = base64Img.base64Sync(getpath('img/demo.png')); 60 | 61 | base64Img.requestBase64(url, function(err, res, body) { 62 | if (err) { 63 | done(); 64 | return console.log(err); 65 | } 66 | 67 | assert.equal(body.substr(0, 10), data.substr(0, 10)); 68 | done(); 69 | }); 70 | }); 71 | }); 72 | 73 | it('Svg', function() { 74 | var data = base64Img.base64Sync(getpath('img/car.svg')) 75 | 76 | fs.writeFileSync(getpath('dest/svg.html'), ``, { encoding: 'utf8'}) 77 | 78 | base64Img.imgSync(data, getpath('dest'), '4') 79 | }) 80 | }); --------------------------------------------------------------------------------