Skip to content

Commit

Permalink
Add SVGO optimization at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
gmetais committed Jul 31, 2015
1 parent ec440fa commit a3ddda5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -26,7 +26,7 @@ Once it's done, simply serve the new SVG like any other image in your HTML:
<img src="merged-image.svg" />
```

*Don't forget to check that your server is configured to properly **gzip** SVG images (content type `image/svg+xml`).*
*Don't forget to check that your server is configured to properly* **gzip** *SVG images (content type `image/svg+xml`).*


### Installation
Expand Down
36 changes: 34 additions & 2 deletions lib/index.js
Expand Up @@ -4,6 +4,7 @@ var fs = require('fs');
var path = require('path');
var mustache = require('mustache');
var sizeOf = require('image-size');
var SVGO = require('svgo');

var SvgImageMerge = function() {

Expand All @@ -25,15 +26,46 @@ var SvgImageMerge = function() {
return loadTemplate()

.then(function(template) {
debug('Template loaded');
return mustache.render(template, templateVars);
return createSVG(template, templateVars);
})

.then(function(svg) {
return optimizeSVG(svg);
});
};


function loadTemplate() {
debug('Loading template');
return Q.nfcall(fs.readFile, path.resolve(__dirname, '../templates/output.svg'), 'utf8');
}

function createSVG(template, vars) {
var deferred = Q.defer();

debug('Creating the SVG');
deferred.resolve(mustache.render(template, vars));

return deferred.promise;
}

function optimizeSVG(svgBuffer) {
var deferred = Q.defer();

debug('Optimizing a little bit more the file with the terrific SVGO');
var svgo = new SVGO();

svgo.optimize(svgBuffer, function(result) {
if (result.error) {
deferred.reject(result.error);
} else {
deferred.resolve(result.data);
}
});

return deferred.promise;
}

};

module.exports = new SvgImageMerge();
7 changes: 4 additions & 3 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "svg-image-merge",
"version": "0.0.0",
"description": "When you want an image with both a compressed background and a pixel perfect text on top... Merge a JPEG background and a transparent PNG into a single SVG image",
"version": "0.1.0",
"description": "When you want an image with both a compressed background and a pixel perfect text on top... Merges a JPEG background and a transparent PNG into a single SVG image",
"main": "lib/index.js",
"bin": {
"svg-image-merge": "./bin/cli.js"
Expand All @@ -26,6 +26,7 @@
"image-size": "0.3.5",
"meow": "3.3.0",
"mustache": "2.1.2",
"q": "1.4.1"
"q": "1.4.1",
"svgo": "0.5.3"
}
}
5 changes: 1 addition & 4 deletions test/result.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a3ddda5

Please sign in to comment.