Я попытался перенести службу из php, которая разбивает изображение на 6 равных частей. Я попытался добиться того же в узле, используя функцию кадрирования модуля gm, но мне не удалось получить все равные фрагменты изображения. Ниже приведен код php, который я использую
$width=800;
$height=1200;
$orig = imagecreatefrompng($image_url);
$slices = 6;
for ($x = 0; $x < $slices; $x++) {
$section[$x] = imagecreatetruecolor($width, $height/$slices);
imagecopy($section[$x], $orig, 0, 0, 0, ($height/$slices)*$x, $width, $height/$slices);
}
, а в узле я пробовал, например,
gm(image).size(function(err, value){
// check for errors, TO DO: put this in 'if' statement
console.log('Error: ', err);
// get current image width
var imageWidth = value.width;
// get current image height
var imageHeight = value.height;
// start slicing on first pixel
var sliceCounter = imageHeight/6;
//
(function getSlices() {
// use 'setTimeout' to get around memory issues
setTimeout(function() {
// if the image height is bigger than the current slice
if (imageHeight > sliceCounter) {
// crop image to the full width of current image and increments of 1 pixel
gm(image).crop(imageWidth, 1, sliceCounter, 0).write('slice' + sliceCounter + '.png', function (err) {
// increase the slice counter, to affect the next slice
sliceCounter+=sliceCounter;
// fire function recurssively, to help with memory
getSlices();
});
}
}, 250);
})();
Я новичок в узле, поэтому я не могу понять. Спасибо