diff options
| author | Marak <[email protected]> | 2018-09-24 11:38:25 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2018-09-24 11:38:25 -0400 |
| commit | 948d0dac5aae515739e3b5fa5b6a64f69aebaa00 (patch) | |
| tree | 8824c6e7b2f60880f99fd2a5eb0b1e62f09a55bb | |
| parent | fb1f21e448f74408fa9133c5e4b26e23dc91cdd3 (diff) | |
| parent | a3a4f62f8d1185c9b6113d057bbb2e47f9b89913 (diff) | |
| download | faker-948d0dac5aae515739e3b5fa5b6a64f69aebaa00.tar.xz faker-948d0dac5aae515739e3b5fa5b6a64f69aebaa00.zip | |
Merge pull request #682 from yuezk/master
Add background color to `image.dataUri`
| -rwxr-xr-x | .travis.yml | 4 | ||||
| -rw-r--r-- | lib/image.js | 10 | ||||
| -rw-r--r-- | test/image.unit.js | 6 |
3 files changed, 15 insertions, 5 deletions
diff --git a/.travis.yml b/.travis.yml index f9bfca44..6fdb8c4b 100755 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,10 @@ sudo: false language: node_js node_js: + - "10" + - "9" + - "8" + - "7" - "6" - "6.1" - "5" diff --git a/lib/image.js b/lib/image.js index dc512a68..b25b5895 100644 --- a/lib/image.js +++ b/lib/image.js @@ -201,13 +201,15 @@ var Image = function (faker) { * * @param {number} width * @param {number} height - * @method faker.image.dataurl + * @param {string} color + * @method faker.image.dataUri */ - self.dataUri = function (width, height) { + self.dataUri = function (width, height, color) { + color = color || 'grey'; + var svgString = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" baseProfile="full" width="' + width + '" height="' + height + '"><rect width="100%" height="100%" fill="' + color + '"/><text x="' + width / 2 + '" y="' + height / 2 + '" font-size="20" alignment-baseline="middle" text-anchor="middle" fill="white">' + width + 'x' + height + '</text></svg>'; var rawPrefix = 'data:image/svg+xml;charset=UTF-8,'; - var svgString = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" baseProfile="full" width="' + width + '" height="' + height + '"> <rect width="100%" height="100%" fill="grey"/> <text x="0" y="20" font-size="20" text-anchor="start" fill="white">' + width + 'x' + height + '</text> </svg>'; return rawPrefix + encodeURIComponent(svgString); }; } -module["exports"] = Image;
\ No newline at end of file +module["exports"] = Image; diff --git a/test/image.unit.js b/test/image.unit.js index 2839d91a..02e4c42b 100644 --- a/test/image.unit.js +++ b/test/image.unit.js @@ -116,7 +116,11 @@ describe("image.js", function () { describe("dataUri", function () { it("returns a blank data", function () { var dataUri = faker.image.dataUri(200,300); - assert.equal(dataUri, 'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20baseProfile%3D%22full%22%20width%3D%22200%22%20height%3D%22300%22%3E%20%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22grey%22%2F%3E%20%20%3Ctext%20x%3D%220%22%20y%3D%2220%22%20font-size%3D%2220%22%20text-anchor%3D%22start%22%20fill%3D%22white%22%3E200x300%3C%2Ftext%3E%20%3C%2Fsvg%3E'); + assert.equal(dataUri, 'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20baseProfile%3D%22full%22%20width%3D%22200%22%20height%3D%22300%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22grey%22%2F%3E%3Ctext%20x%3D%22100%22%20y%3D%22150%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E200x300%3C%2Ftext%3E%3C%2Fsvg%3E'); + }); + it("returns a customed background color data URI", function () { + var dataUri = faker.image.dataUri(200, 300, 'red'); + assert.equal(dataUri, 'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20baseProfile%3D%22full%22%20width%3D%22200%22%20height%3D%22300%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22red%22%2F%3E%3Ctext%20x%3D%22100%22%20y%3D%22150%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E200x300%3C%2Ftext%3E%3C%2Fsvg%3E'); }); }); }); |
