Permalink
Browse files

Add size support for svg

Solve #2
  • Loading branch information...
1 parent 1e33272 commit 29ed5c6192ed1df3769ca25c5ade84fec564edb0 @tobiaslins committed Apr 10, 2017
Showing with 32 additions and 1,410 deletions.
  1. +16 โˆ’10 src/image.js
  2. +1 โˆ’2 src/index.js
  3. +1 โˆ’1 src/svg.js
  4. +14 โˆ’1,397 yarn.lock
View
@@ -6,7 +6,7 @@ const Color = require('color')
const svg = require('./svg')
const helper = require('./helper')
-function generateGradient(username) {
+function generateGradient(username, size) {
const hash = crypto.createHash('md5').update(username).digest('hex')
let firstColor = helper.hashStringToColor(hash)
@@ -26,19 +26,25 @@ function generateGradient(username) {
let avatar = svg.replace('$FIRST', firstColor.hex())
avatar = avatar.replace('$SECOND', helper.getMatchingColor(firstColor).hex())
+ avatar = avatar.replace(/(\$SIZE)/g, size)
+
return avatar
}
-exports.generateSVG = function (username) {
- return generateGradient(username)
+function parseSize(size) {
+ if (size && size.match(/^-?\d+$/)) {
+ return parseInt(size, 10)
+ }
+ return 120
+}
+
+exports.generateSVG = function (username, size) {
+ size = parseSize(size)
+ return generateGradient(username, size)
}
exports.generatePNG = function (username, size) {
- size = size || '120'
- const maxSize = 1000
- if (size && size.match(/^-?\d+$/) && size <= maxSize) {
- size = parseInt(size, 10)
- }
- const svg = generateGradient(username)
- return sharp(new Buffer(svg)).resize(size, size).png()
+ size = parseSize(size)
+ const svg = generateGradient(username, size)
+ return sharp(new Buffer(svg)).png()
}
View
@@ -5,7 +5,6 @@ const pngExt = /\.png$/;
module.exports = (req, res) => {
let {pathname, query} = url.parse(req.url, true)
-
if (pathname === '/favicon.ico') {
return ''
}
@@ -16,7 +15,7 @@ module.exports = (req, res) => {
}
if (query.type === 'svg' || svgExt.test(pathname)) {
res.setHeader('Content-Type', 'image/svg+xml')
- return image.generateSVG(pathname.replace(svgExt, ''));
+ return image.generateSVG(pathname.replace(svgExt, ''), query.size);
}
res.setHeader('Content-Type', 'image/png')
return image.generatePNG(pathname.replace(pngExt, ''), query.size)
View
@@ -1,6 +1,6 @@
module.exports = `<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
-<svg width="120" height="120" viewBox="120 120 120 120" version="1.1" xmlns="http://www.w3.org/2000/svg">
+<svg width="$SIZE" height="$SIZE" viewBox="120 120 120 120" version="1.1" xmlns="http://www.w3.org/2000/svg">
<g>
<defs>
<linearGradient id="avatar" x1="0" y1="0" x2="1" y2="1">
Oops, something went wrong.

0 comments on commit 29ed5c6

Please sign in to comment.