如何用nodejs给图片添加半透明水印(nodejs,web开发)

时间:2024-05-08 03:47:50 作者 : 石家庄SEO 分类 : web开发
  • TAG :

如何用nodejs给图片添加半透明水印

作为中后台项目的导出功能,通常会被要求具备导出的追溯能力。

当导出的数据形态为图片时,一般会为图片添加水印以达到此目的。

DEMO

那么在导出图片前如何为其添加上可以作为导出者身份识别的水印呢?先看成品:

如何用nodejs给图片添加半透明水印

上图原图为我随便在网上找的一张图片,添加水印之后的效果如图所示。

业务需求分解

这里我们需要考虑在此业务场景之下,这个需求的三个要点:

  • 水印需要铺满整个图片

  • 水印文字成半透明状,保证原图的可读性

  • 水印文字应清晰可读

选型

如我一样负责在一个node.js server上实现以上需求,可选项相当多,比如直接使用c lib imagemagick或者已有人封装的各种node watermarking库。在本文中,我们将选择使用对Jimp库的封装。

Jimp 库的官方github页面上这样描述它自己:

An image processing library for Node written entirely in JavaScript, with zero native dependencies.

并且提供为数众多的操作图片的API

  • blit - Blit an image onto another.

  • blur - Quickly blur an image.

  • color - Various color manipulation methods.

  • contain - Contain an image within a height and width.

  • cover - Scale the image so the given width and height keeping the aspect ratio.

  • displace - Displaces the image based on a displacement map

  • dither - Apply a dither effect to an image.

  • flip - Flip an image along it's x or y axis.

  • gaussian - Hardcore blur.

  • invert - Invert an images colors

  • mask - Mask one image with another.

  • normalize - Normalize the colors in an image

  • print - Print text onto an image

  • resize - Resize an image.

  • rotate - Rotate an image.

  • scale - Uniformly scales the image by a factor.

在本文所述的业务场景中,我们只需使用其中部分API即可。

设计和实现

input 参数设计:

  • url: 原图片的存储地址(对于Jimp来说,可以是远程地址,也可以是本地地址)

  • textSize: 需添加的水印文字大小

  • opacity:透明度

  • text:需要添加的水印文字

  • dstPath:添加水印之后的输出图片地址,地址为脚本执行目录的相对路径

  • rotate:水印文字的旋转角度

  • colWidth:因为可旋转的水印文字是作为一张图片覆盖到原图上的,因此这里定义一下水印图片的宽度,默认为300像素

  • rowHeight:理由同上,水印图片的高度,默认为50像素。(PS:这里的水印图片尺寸可以大致理解为水印文字的间隔)

因此在该模块的coverTextWatermark函数中对外暴露以上参数即可

coverTextWatermark

/***@param{String}mainImage-Pathoftheimagetobewatermarked*@param{Object}options*@param{String}options.text-Stringtobewatermarked*@param{Number}options.textSize-Textsizerangingfrom1to8*@param{String}options.dstPath-Destinationpathwhereimageistobeexported*@param{Number}options.rotate-Textrotaterangingfrom1to360*@param{Number}options.colWidth-Textwatermarkcolumnwidth*@param{Number}options.rowHeight-Textwatermarkrowheight*/module.exports.coverTextWatermark=async(mainImage,options)=>{try{options=checkOptions(options);constmain=awaitJimp.read(mainImage);constwatermark=awaittextWatermark(options.text,options);constpositionList=calculatePositionList(main,watermark)for(leti=0;i<positionList.length;i++){constcoords=positionList[i]main.composite(watermark,coords[0],coords[1]);}main.quality(100).write(options.dstPath);return{destinationPath:options.dstPath,imageHeight:main.getHeight(),imageWidth:main.getWidth(),};}catch(err){throwerr;}}

textWatermark

Jimp不能直接将文本旋转一定角度,并写到原图片上,因此我们需要根据水印文本生成新的图片二进制流,并将其旋转。最终以这个新生成的图片作为真正的水印添加到原图片上。下面是生成水印图片的函数定义:

consttextWatermark=async(text,options)=>{constimage=awaitnewJimp(options.colWidth,options.rowHeight,'#FFFFFF00');constfont=awaitJimp.loadFont(SizeEnum[options.textSize])image.print(font,10,0,{text,alignmentX:Jimp.HORIZONTAL_ALIGN_CENTER,alignmentY:Jimp.VERTICAL_ALIGN_MIDDLE},400,50)image.opacity(options.opacity);image.scale(3)image.rotate(options.rotation)image.scale(0.3)returnimage}

calculatePositionList

到目前为止原图有了,水印图片也有了,如果想达到铺满原图的水印效果,我们还需要计算出水印图片应该在哪些坐标上画在原图上,才能达成水印铺满原图的目的。

constcalculatePositionList=(mainImage,watermarkImg)=>{constwidth=mainImage.getWidth()constheight=mainImage.getHeight()conststepWidth=watermarkImg.getWidth()conststepHeight=watermarkImg.getHeight()letret=[]for(leti=0;i<width;i=i+stepWidth){for(letj=0;j<height;j=j+stepHeight){ret.push([i,j])}}returnret}

如上代码所示,我们使用一个二维数组记录所有水印图片需出现在原图上的坐标列表。

示例代码:

varwatermark=require('jimp-fullpage-watermark');watermark.coverTextWatermark('./img/main.jpg',{textSize:5,opacity:0.5,rotation:45,text:'watermarktest',colWidth:300,rowHeight:50}).then(data=>{console.log(data);}).catch(err=>{console.log(err);});
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:如何用nodejs给图片添加半透明水印的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:在html中怎么实现文本属性和颜色控制属性效果下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18