Created
August 29, 2018 10:00
-
-
Save packtpartner/180024fd93e56451e8fba893b044d305 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.head("/uploads/:image", (req, res) => { | |
fs.access( | |
path.join(__dirname, "uploads", req.params.image), | |
fs.constants.R_OK , | |
(err) => { | |
res.status(err ? 404 : 200); | |
res.end(); | |
} | |
); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -X POST -H 'Content-Type: image/png' \ | |
- data-binary @example.png \ | |
http://localhost:3000/uploads/example.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let image = sharp({ | |
create : { | |
width : width, | |
height : height, | |
channels : 4, | |
background : { r: 0, g: 0, b: 0 }, | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const sharp = require("sharp"); | |
const app = express(); | |
app.get(/\/thumbnail\.(jpg|png)/, (req, res, next) => { | |
let format = (req.params[0] == "png" ? "png" : "jpeg"); | |
let width = 300; | |
let height = 200; | |
let border = 5; | |
let bgcolor = "#fcfcfc"; | |
let fgcolor = "#ddd"; | |
let textcolor = "#aaa"; | |
let textsize = 24; | |
let image = sharp({ | |
create : { | |
width : width, | |
height : height, | |
channels : 4, | |
background : { r: 0, g: 0, b: 0 }, | |
} | |
}); | |
const thumbnail = new Buffer( | |
`<svg width="${width}"height="${height}"> | |
<rect | |
x="0"y="0" | |
width="${width}"height="${height}" | |
fill="${fgcolor}"/> | |
<rect | |
x="${border}"y="${border}" | |
width="${width - border * 2}"height="${height - border * 2}" | |
fill="${bgcolor}"/> | |
<line | |
x1="${border * 2}"y1="${border * 2}" | |
x2="${width - border * 2}"y2="${height - border * 2}" | |
stroke-width="${border}"stroke="${fgcolor}"/> | |
<line | |
x1="${width - border * 2}"y1="${border * 2}" | |
x2="${border * 2}"y2="${height - border * 2}" | |
stroke-width="${border}"stroke="${fgcolor}"/> | |
<rect | |
x="${border}"y="${(height - textsize) / 2}" | |
width="${width - border * 2}"height="${textsize}" | |
fill="${bgcolor}"/> | |
<text | |
x="${width / 2}"y="${height / 2}"dy="8" | |
font-family="Helvetica"font-size="${textsize}" | |
fill="${textcolor}"text-anchor="middle">${width} x ${height}</text> | |
</svg>` | |
); | |
image.overlayWith(thumbnail)[format]().pipe(res); | |
}); | |
app.listen(3000, () => { | |
console.log("ready"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.listen(3000, () => { | |
console.log("ready"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let width = +req.query.width || 300; | |
let height = +req.query.height || 200; | |
let border = +req.query.border || 5; | |
let bgcolor = req.query.bgcolor || "#fcfcfc"; | |
let fgcolor = req.query.fgcolor || "#ddd"; | |
let textcolor = req.query.textcolor || "#aaa"; | |
let textsize = +req.query.textsize || 24; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post("/uploads/:image", bodyparser.raw({ | |
limit : "10mb", | |
type : "image/*" | |
}), (req, res) => { | |
let image = req.params.image.toLowerCase(); | |
if (!image.match(/\.(png|jpg)$/)) { | |
return res.status(403).end(); | |
} | |
let len = req.body.length; | |
let fd = fs.createWriteStream(path.join(__dirname, "uploads", | |
image), { | |
flags : "w+", | |
encoding : "binary" | |
}); | |
fd.write(req.body); | |
fd.end(); | |
fd.on("close", () => { | |
res.send({ status : "ok", size: len }); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//parser module | |
npm install body-parser - save | |
//other core modules | |
const bodyparser = require("body-parser"); | |
const path = require("path"); | |
const fs = require("fs"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment