跨平台的 NodeJS 组件解决 .NetCore 不支持 System.Drawing图形功能的若干问题

问题

  • 生成缩略图

  • 生成验证码

  • 生成二维码

  • 给图片加水印

外部引用

  • Node  不解释  https://nodejs.org/en/download/

  • sharp 高性能缩略图  https://github.com/lovell/sharp

  • qr-image  二维码  https://github.com/alexeyten/qr-image

  • captchagen  验证码  https://github.com/contra/captchagen

  • node-images  轻量级跨平台图像编解码库 https://github.com/zhangyuanwei/node-images

生成缩略图代码
resizeImage.js

var sharp = require('sharp');


module.exports = function (result, physicalPath, mimeType, maxWidth, maxHeight) {

    // Invoke the 'sharp' NPM module, and have it pipe the resulting image data back to .NET

    sharp(physicalPath)

        .resize(maxWidth || null, maxHeight || null)

        .pipe(result.stream);

}

ResizeController.cs


public class ResizeController : Controller

    {

        private const int MaxDimension = 1000;

        private static string[] AllowedMimeTypes = new[] { "image/jpeg", "image/png", "image/gif" };


        private IHostingEnvironment _environment;

        private INodeServices _nodeServices;


        public ResizeController(IHostingEnvironment environment, INodeServices nodeServices)

        {

            _environment = environment;

            _nodeServices = nodeServices;

        }


        [Route("resize_{maxWidth}_{maxHeight}/{*imagePath}")]

        public async Task<IActionResult> Index(string imagePath, int maxWidth, int maxHeight)

        { 

            imagePath = imagePath;

            // Validate incoming params

            if (maxWidth < 0 || maxHeight < 0 || maxWidth > MaxDimension || maxHeight > MaxDimension

                || (maxWidth + maxHeight) == 0)

            {

                return BadRequest("Invalid dimensions");

            }


            var mimeType = GetContentType(imagePath);

            if (Array.IndexOf(AllowedMimeTypes, mimeType) < 0)

            {

                return BadRequest("Disallowed image format");

            }


            // Locate source image on disk

            var fileInfo = _environment.WebRootFileProvider.GetFileInfo(imagePath);

            if (!fileInfo.Exists)

            {

                return NotFound();

            }


            // Invoke Node and pipe the result to the response

            var imageStream = await _nodeServices.InvokeAsync<Stream>(

                "./Node/resizeImage",

                fileInfo.PhysicalPath,

                mimeType,

                maxWidth,

                maxHeight);

            return File(imageStream, mimeType);

        }


        private string GetContentType(string path)

        {

            string result;

            return new FileExtensionContentTypeProvider().TryGetContentType(path, out result) ? result : null;

        }

    }

效果

生成验证码代码
captchagen.js

var captchagen = require('captchagen');

module.exports = function (result, width, height, text) {

    // optional object arg with keys: height, width, text, font

    var captcha = captchagen.create({ width: width, height: height, text: text||'8888'});

    captcha.generate(); // Draws the image to the canvas

    /* call `generate()` before running the below */

    captcha.stream().pipe(result.stream); // outputs an image stream. type can be either png or jpeg (png is the default)

}

效果


生成二维码代码

1 var qr = require('qr-image');
2 module.exports = function (result, size, url) {
3 qr.image(url, { type: 'png', size: size, margin: 1 })
4      .pipe(result.stream);
5 }

效果


总结

安装Node引用组件时费了不少时间主要是因为没细看作者给出的各种环境下的安装说明。

加水印目前还没做好,主要是要修改源码实现支持stream类型输出

抛砖引玉,希望更多朋友分享 Node各种组件的应用.

原文地址:http://www.cnblogs.com/wspnet/articles/NodeJS_NetCore_System_Drawing_Faq.html


.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值