git ssb

5+

Matt McKegg / ferment



Tree: ce8c9a23510ffcaae086aac41d0d04be17ab0af0

Files: ce8c9a23510ffcaae086aac41d0d04be17ab0af0 / lib / process-image.js

948 bytesRaw
1module.exports = function (path, opts, cb) {
2 var image = document.createElement('img')
3
4 image.onload = next
5 image.src = `file://${path}`
6 image.style.display = 'block'
7
8 if (image.complete) next()
9
10 function next () {
11 var imageHeight = image.height
12 var imageWidth = image.width
13
14 var multiplier = (opts.height / image.height)
15 if (multiplier * imageWidth < opts.width) {
16 multiplier = opts.width / image.width
17 }
18
19 var finalWidth = imageWidth * multiplier
20 var finalHeight = imageHeight * multiplier
21
22 var offsetX = (finalWidth - opts.width) / 2
23 var offsetY = (finalHeight - opts.height) / 2
24
25 var canvas = document.createElement('canvas')
26 canvas.width = opts.width
27 canvas.height = opts.height
28 var ctx = canvas.getContext('2d')
29 ctx.drawImage(image, -offsetX, -offsetY, finalWidth, finalHeight)
30 var dataURL = canvas.toDataURL('image/' + opts.type, 0.85)
31 cb(null, dataURL)
32 }
33}
34

Built with git-ssb-web