nodeJS图像大小增加/减少和裁剪

时间:2021-03-14 07:11:07

I am using node gm module for image manipulation like below

我正在使用节点gm模块进行图像处理,如下所示

 gm(req.files.file.path)
    .rotate('white', parseFloat(body.angle))
    .scale(body.mW * 1, body.mH * 1)
    .crop(body.mW_W * 1, body.mH_W * 1, body.x * 1, body.y * 1)
    .resize(parseInt(body.w), parseInt(body.h), "!")
    .quality(1)
    .setFormat("png")
    .write(small, function (err) {
    if (!err) {
        console.log('Done!');
    } else {
        console.log(err);
        console.log('error occurred');
    }
});

The actual dimensions might be different but the width and height I am feeding to scale (also tried resize) method could be higher or lesser actual dimensions. Because of this I am getting image as it is with resized to final body.w, body.h dimensions.How can I resize an image without bothering about original dimensions.

实际尺寸可能不同,但我按比例(也尝试调整大小)方法的宽度和高度可能是更高或更小的实际尺寸。正因为如此,我得到了图像,因为它是调整大小到最终body.w,body.h维度。如何在不打扰原始尺寸的情况下调整图像大小。

1 个解决方案

#1


2  

I found the answer, if anyone using jquery guillotine or similar library to select portion of image crop at server, please use below code

我找到了答案,如果有人使用jquery断头台或类似的库来选择服务器上的图像裁剪部分,请使用下面的代码

 var newW = body.imageWidth * body.scale, newH = body.imageHeight * body.scale;
 gm(req.files.file.path)
    .rotate('white', body.angle)
    .resize(newW, newH, '!')
    .crop(body.w, body.h, body.x, body.y).write(path, function(err){

    });

#1


2  

I found the answer, if anyone using jquery guillotine or similar library to select portion of image crop at server, please use below code

我找到了答案,如果有人使用jquery断头台或类似的库来选择服务器上的图像裁剪部分,请使用下面的代码

 var newW = body.imageWidth * body.scale, newH = body.imageHeight * body.scale;
 gm(req.files.file.path)
    .rotate('white', body.angle)
    .resize(newW, newH, '!')
    .crop(body.w, body.h, body.x, body.y).write(path, function(err){

    });