Forked from edwardhotchkiss/graphics_magick_center_image_on_canvas.js
Created
March 7, 2017 19:38
-
-
Save SergeyMaas/8d165bbe9fe535909978b984af7a7cd1 to your computer and use it in GitHub Desktop.
Use GM Module (Graphics Magick) for Node.js to center an image on a white background canvas
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
var gm = require('gm'); | |
var canvasWidth = 248; | |
var canvasHeight = 389; | |
gm(__dirname + '/original.jpg').size(function(error, size) { | |
if (error) { | |
console.error(error); | |
} else { | |
// current image size | |
var imageWidth = size.width; | |
var imageHeight = size.height; | |
// center placement | |
var x = (canvasWidth / 2) - (imageWidth / 2); | |
var y = (canvasHeight / 2) - (imageHeight / 2); | |
console.log(x, y); | |
this.background('#ffffff') | |
.resize(imageWidth, imageHeight) | |
.gravity('Center') | |
.extent(canvasWidth, canvasHeight) | |
.write(__dirname + '/test.jpg', function(error) { | |
if (error) { | |
console.error(error); | |
} else { | |
console.log(this.outname); | |
}; | |
}) | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment