aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Yue <[email protected]>2018-08-13 00:05:19 +0800
committerKevin Yue <[email protected]>2018-08-13 00:05:19 +0800
commit3448b0afcecfa5bf26d5716594347f76496d400b (patch)
treee36c51fba8d50dafc559538345caa13eb04beb41
parent1605703565046c40f3d4b5ff318ff2d5846c8685 (diff)
downloadfaker-3448b0afcecfa5bf26d5716594347f76496d400b.tar.xz
faker-3448b0afcecfa5bf26d5716594347f76496d400b.zip
Added custom background color to data URI image
-rw-r--r--lib/image.js6
-rw-r--r--test/image.unit.js4
2 files changed, 8 insertions, 2 deletions
diff --git a/lib/image.js b/lib/image.js
index 311acee4..b25b5895 100644
--- a/lib/image.js
+++ b/lib/image.js
@@ -201,10 +201,12 @@ var Image = function (faker) {
*
* @param {number} width
* @param {number} height
+ * @param {string} color
* @method faker.image.dataUri
*/
- self.dataUri = function (width, height) {
- 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="' + width / 2 + '" y="' + height / 2 + '" font-size="20" alignment-baseline="middle" text-anchor="middle" fill="white">' + width + 'x' + height + '</text></svg>';
+ 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,';
return rawPrefix + encodeURIComponent(svgString);
};
diff --git a/test/image.unit.js b/test/image.unit.js
index 055e1cb3..02e4c42b 100644
--- a/test/image.unit.js
+++ b/test/image.unit.js
@@ -118,5 +118,9 @@ describe("image.js", 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%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');
+ });
});
});