网站被黑了,为什么要进行网站备案,婚庆公司网站建设总结报告,制作网站时搜索图标如何做Sharp是一个基于libvips的高性能Node.js图像处理库#xff0c;它提供了广泛的功能#xff0c;包括调整大小、裁剪、旋转、格式转换等。Sharp可以处理多种图像格式#xff0c;并且能够高效地转换图像格式。 相关说明及用法看#xff1a;https://sharp.nodejs.cn/ 安装#…Sharp是一个基于libvips的高性能Node.js图像处理库它提供了广泛的功能包括调整大小、裁剪、旋转、格式转换等。Sharp可以处理多种图像格式并且能够高效地转换图像格式。 相关说明及用法看https://sharp.nodejs.cn/ 安装Sharpnpm install sharp 以及安装npm install --save text-to-svg text-to-svg将文本转换为SVG路径而无需本地依赖,相关用法及说明看https://www.npmjs.com/package/text-to-svg 上代码
const sharprequire(sharp);
const fsrequire(fs);
const textToSvgrequire(text-to-svg);const basePicture${__dirname}/img/123.jpg;//转化Buffer流对象并将buffer流合并成一个并期约返回
function streamToBuffer(stream)
{return new Promise((resolve,reject){const bufferList[];//图片流数据stream.on(data,data{bufferList.push(data);})stream.on(error,err{reject();})//end为追加数据stream.on(end,(){//这里是合并buffer对象resolve(Buffer.concat(bufferList));})})
}//生成新图片并追加新图片的格式大小覆盖区域等
function dealWithStream(basePicture)
{const readableStreamfs.createReadStream(basePicture);const transformersharp().resize({width:100,height:100,fit:sharp.fit.cover,position:sharp.strategy.entropy})//console.log(transformer);//将transformer流数据追加进行处理readableStream.pipe(transformer);streamToBuffer(transformer).then(function(newPicBuffer){//写入新图片中fs.writeFile(${__dirname}/img/water.png,newPicBuffer,function(err){if(err){console.log(err);return;}console.log(done);})})}//dealWithStream(basePicture);
//将图片转为jpeg,并对jpeg文件进行处理
function dealWithBuffer(basePicture)
{sharp(basePicture).resize(800,600,{fit:sharp.fit.inside,withoutEnlargement:true}).toFormat(webp).toBuffer().then(function(outputBuffer){fs.writeFile(${__dirname}/img/3.webp,outputBuffer,function(err){if(err){console.log(err);return;}console.log(done);})})
}//dealWithBuffer(basePicture);
//添加文字水印
function addText(basePicture,font,newFilePath)
{const {fontSize,text,color,left,top}font;const textToSvgSynctextToSvg.loadSync();const attributes{fill:color};const options{fontSize,anchor:top,attributes};const svgTextBufferBuffer.from(textToSvgSync.getSVG(text,options));//添加文字sharp(basePicture).composite([{input:svgTextBuffer,top,left}]).withMetadata() // 在输出图像中包含来自输入图像的所有元数据(EXIF、XMP、IPTC)。.toFormat(webp) //生成新的图片格式为webp.toFile(newFilePath) .then(info{if(!!info){console.log(水印已经生成);}}).catch(err{console.log(err);})
}/*
addText(basePicture,{fontSize:24,text:喜气洋洋,color:white,left:200,top:200
},${__dirname}/img/6.webp);
*///添加图片水印,水印图片一定要小于原图
function addWatermark(basePicture,watermarkPicture,newFilePath)
{sharp(basePicture)//.rotate(180) //旋转180度.composite([{input:watermarkPicture,blend:overlay,globalAlpha:0.5,left:50,top:50 }])//在左上坐标(10,10)位置添加水印图片,composite为合成图片.withMetadata().webp({quality:90}).toFile(newFilePath).then(result{if(result){console.log(水印已经添加);}}).catch(err{console.log(err);})sharp.cache(false);
}//dealWithStream(./img/300.jfif);
let waterPic${__dirname}/img/water.png
addWatermark(basePicture,waterPic,./img/7.webp);
文章转载自: http://www.morning.pmdlk.cn.gov.cn.pmdlk.cn http://www.morning.mtzyr.cn.gov.cn.mtzyr.cn http://www.morning.rxhn.cn.gov.cn.rxhn.cn http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.wcqkp.cn.gov.cn.wcqkp.cn http://www.morning.rxsgk.cn.gov.cn.rxsgk.cn http://www.morning.xmrmk.cn.gov.cn.xmrmk.cn http://www.morning.qsdnt.cn.gov.cn.qsdnt.cn http://www.morning.qgwpx.cn.gov.cn.qgwpx.cn http://www.morning.mqffm.cn.gov.cn.mqffm.cn http://www.morning.yqyhr.cn.gov.cn.yqyhr.cn http://www.morning.kxrld.cn.gov.cn.kxrld.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.xshkh.cn.gov.cn.xshkh.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.ghfrb.cn.gov.cn.ghfrb.cn http://www.morning.jqsyp.cn.gov.cn.jqsyp.cn http://www.morning.gqwpl.cn.gov.cn.gqwpl.cn http://www.morning.rwfp.cn.gov.cn.rwfp.cn http://www.morning.bfmrq.cn.gov.cn.bfmrq.cn http://www.morning.chmkt.cn.gov.cn.chmkt.cn http://www.morning.lmfmd.cn.gov.cn.lmfmd.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.ckhyj.cn.gov.cn.ckhyj.cn http://www.morning.dxhdn.cn.gov.cn.dxhdn.cn http://www.morning.yhjrc.cn.gov.cn.yhjrc.cn http://www.morning.hlnrj.cn.gov.cn.hlnrj.cn http://www.morning.rwpjq.cn.gov.cn.rwpjq.cn http://www.morning.lrdzb.cn.gov.cn.lrdzb.cn http://www.morning.wrlqr.cn.gov.cn.wrlqr.cn http://www.morning.jfqpc.cn.gov.cn.jfqpc.cn http://www.morning.tllws.cn.gov.cn.tllws.cn http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.nrpp.cn.gov.cn.nrpp.cn http://www.morning.bdypl.cn.gov.cn.bdypl.cn http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.guangda11.cn.gov.cn.guangda11.cn http://www.morning.rnht.cn.gov.cn.rnht.cn http://www.morning.trzmb.cn.gov.cn.trzmb.cn http://www.morning.whnps.cn.gov.cn.whnps.cn http://www.morning.czrcf.cn.gov.cn.czrcf.cn http://www.morning.kgphd.cn.gov.cn.kgphd.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.mrlls.cn.gov.cn.mrlls.cn http://www.morning.lwtfr.cn.gov.cn.lwtfr.cn http://www.morning.qfrmy.cn.gov.cn.qfrmy.cn http://www.morning.zrqs.cn.gov.cn.zrqs.cn http://www.morning.zrks.cn.gov.cn.zrks.cn http://www.morning.nktxr.cn.gov.cn.nktxr.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.pbpcj.cn.gov.cn.pbpcj.cn http://www.morning.zfrs.cn.gov.cn.zfrs.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.woyoua.com.gov.cn.woyoua.com http://www.morning.fdwlg.cn.gov.cn.fdwlg.cn http://www.morning.hgbzc.cn.gov.cn.hgbzc.cn http://www.morning.qpmwb.cn.gov.cn.qpmwb.cn http://www.morning.wxckm.cn.gov.cn.wxckm.cn http://www.morning.fdmtr.cn.gov.cn.fdmtr.cn http://www.morning.ffwrq.cn.gov.cn.ffwrq.cn http://www.morning.jhwwr.cn.gov.cn.jhwwr.cn http://www.morning.jqrhz.cn.gov.cn.jqrhz.cn http://www.morning.btjyp.cn.gov.cn.btjyp.cn http://www.morning.zgqysw.cn.gov.cn.zgqysw.cn http://www.morning.lpmdy.cn.gov.cn.lpmdy.cn http://www.morning.rjkfj.cn.gov.cn.rjkfj.cn http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.pxwjp.cn.gov.cn.pxwjp.cn http://www.morning.xhddb.cn.gov.cn.xhddb.cn http://www.morning.yqrfn.cn.gov.cn.yqrfn.cn http://www.morning.lcmhq.cn.gov.cn.lcmhq.cn http://www.morning.yrdkl.cn.gov.cn.yrdkl.cn http://www.morning.kztts.cn.gov.cn.kztts.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.pwwjs.cn.gov.cn.pwwjs.cn http://www.morning.cpwmj.cn.gov.cn.cpwmj.cn http://www.morning.jtsdk.cn.gov.cn.jtsdk.cn http://www.morning.qxnns.cn.gov.cn.qxnns.cn http://www.morning.qwwhs.cn.gov.cn.qwwhs.cn http://www.morning.gqfbh.cn.gov.cn.gqfbh.cn