跨平台的 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 = ;
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 < || maxHeight < || maxWidth > MaxDimension || maxHeight > MaxDimension
|| (maxWidth + maxHeight) == )
{
return BadRequest("Invalid dimensions");
} var mimeType = GetContentType(imagePath);
if (Array.IndexOf(AllowedMimeTypes, mimeType) < )
{
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)
}
效果
生成二维码代码
var qr = require('qr-image');
module.exports = function (result, size, url) {
qr.image(url, { type: 'png', size: size, margin: 1 })
.pipe(result.stream);
}
效果
总结
安装Node引用组件时费了不少时间主要是因为没细看作者给出的各种环境下的安装说明。
加水印目前还没做好,主要是要修改源码实现支持stream类型输出
抛砖引玉,希望更多朋友分享 Node各种组件的应用,
跨平台的 NodeJS 组件解决 .NetCore 不支持 System.Drawing图形功能的若干问题的更多相关文章
- .netcore中无法使用System.Drawing --解决方案
问题重现: 无法正常使用 解决方法: 安装System.Drawing.Common的NuGet就能正常使用了 操作之后: 这个是.netcoe中的解决办法,.net framework解决方案中添 ...
- nodejs项目mysql使用sequelize支持存储emoji
nodejs项目mysql使用sequelize支持存储emoji 本篇主要记录nodejs项目阿里云mysql如何支持存储emoji表情. 因由 最近项目遇到用户在文本输入emoji进行存储的时候导 ...
- Nginx配置解决NetCore的跨域
使用Nginx配置解决NetCore的跨域 废话不多说,直接上Nginx配置 server { listen 80; server_name 你的Id或域名; location / { add_hea ...
- jquery easyui-linkButton获取和设置按钮text并且解决火狐不支持innerText的方法
<a href="javascript:test" id="btn" class="easyui-linkbutton" iconCl ...
- 解决ie6不支持position: fixed;导致无法滚动的办法
<div id="im" style="top: 100px; position: fixed; left: 5px; border: 3px solid #006 ...
- 解决selenium不支持firefox低版本的问题
解决selenium不支持firefox低版本的问题 在火狐浏览器升级后,突然发现webdriver运行脚本的时候不能调出火狐浏览器了,并报错WebDriverException:Message:'C ...
- 解决IE8不支持console
解决IE8不支持console,代码中包含console时会报错. //解决 IE8 不支持console window.console = window.console || (function ( ...
- vue+nodejs+express解决跨域问题
nodejs+express解决跨域问题,发现网上的大部分都是误导人,花了不少时间,终于弄懂了, 我在vue+nodejs+express+mongodb的项目里面,发现本地用vue代理正常调用远程的 ...
- 基于V2EX API的nodejs组件.
今天又学习到了新的知(zi)识(shi),来给自己做个笔录,也算在这酷热的天气里给自己写了一篇降温的‘膏药’,话就讲这么多了 ,start off...... 首先 ,依赖选择: /**设置为严格模式 ...
随机推荐
- 工作常用git命令
克隆项目 git clone gitssh地址 提交前的准备 git config user.name 您的中文名 git config user.email 公司邮箱 获取分支 #### 将远端分支 ...
- 02-TypeScript中新的字符串
TypeScript中引入了字符串模板,通过字符串模板可以方便的实现字符串换行的连接.方便变量等. 1.在WebStorm中新建一个文件,后缀名为ts. 在建立ts文件时,WebStorm会问你是否需 ...
- Push or Pull?
采用Pull模型还是Push模型是很多中间件都会面临的一个问题.消息中间件.配置管理中心等都会需要考虑Client和Server之间的交互采用哪种模型: 服务端主动推送数据给客户端? 客户端主动从服务 ...
- ASP.NET Core Web服务器 Kestrel和Http.sys 特性详解
ASP.NET Core Web服务器 Kestrel和Http.sys 特性详解 1.1. 名词解释 1.2. Kestrel基本工作原理 1.2.1. Kestrel的基本架构 1.2.2. Ke ...
- 基础知识(C#语法、数据库SQL Server)回顾与总结
前言 已经有大概一个多月没有更新博客,可能是开始变得有点懒散了吧,有时候想写,但是又需要额外投入更多的时间去学习,感觉精力完全不够用啊,所以为了弥补这一个多月的潜水,决定写一篇,衔接9月未写博客的空缺 ...
- 【Beta】Daily Scrum Meeting——Day5
站立式会议照片 1.本次会议为第五次Meeting会议: 2.本次会议在早上9:35,在陆大2楼机房召开,本次会议为25分钟讨论今天要完成的任务以及接下来的任务安排. 燃尽图 每个人的工作分配 成 员 ...
- 201521123080《Java程序设计》第1周学习总结
#1. 本周学习总结 Java开发环境,如何用记事本和eclipse进行编程. #2. 书面作业 Q1.为什么java程序可以跨平台运行?执行java程序的步骤是什么?(请用自己的语言书写) ...
- 关闭Sublime Text的自动更新的方法
每次打开Sublime text 软件都会提示我让我更新软件,如图: 经过仔细的研究发现可以通过以下途径关闭软件的自动更新 打开Submine Text,找到Preferences -> Set ...
- 201521123114 《Java程序设计》第12周学习总结
1.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,double ...
- 从java的开始,java概述,java配置环境变量
一.java开发入门 java 概述 Java划分为三个技术平台:JavaSE(标准版,含Java基础类库),JavaEE(企业版,技术平台),JavaME(小型版,小型产品.嵌入式设备) Jav ...