跨平台的 NodeJS 组件解决 .NetCore 不支持 System.Drawing图形功能的若干问题
2016-10-31 07:08
706 查看
问题
生成缩略图
生成验证码
生成二维码
给图片加水印
外部引用
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)
}
效果
生成二维码代码
效果
总结
安装Node引用组件时费了不少时间主要是因为没细看作者给出的各种环境下的安装说明。
加水印目前还没做好,主要是要修改源码实现支持stream类型输出
抛砖引玉,希望更多朋友分享 Node各种组件的应用.
原文地址:http://www.cnblogs.com/wspnet/articles/NodeJS_NetCore_System_Drawing_Faq.html
.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注
生成缩略图
生成验证码
生成二维码
给图片加水印
外部引用
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跨平台或扫描二维码关注
相关文章推荐
- 跨平台的 NodeJS 组件解决 .NetCore 不支持 System.Drawing图形功能的若干问题
- [C] 跨平台使用TCHAR——让Linux等平台也支持tchar.h,解决跨平台时的格式控制字符问题,多国语言的同时显示(兼容vc/gcc/bcb,支持Windows/Linux/Mac)
- mybatis 使用dblink 执行insert 报ora-22816 returning 子句不支持的功能问题解决方法
- 解决nodejs不支持async和await关键字的问题
- [C] 跨平台使用TCHAR——让Linux等平台也支持tchar.h,解决跨平台时的格式控制字符问题,多国语言的同时显示(兼容vc/gcc/bcb,支持Windows/Linux/Mac)
- 给网站增加解压缩功能,解决上传速度慢或者不支持FTP的问题(要求支持asp.net和SharpZip)
- 解决“System.Data.Odbc.OdbcException: ERROR [IM001] [Microsoft][ODBC 驱动程序管理器] 驱动程序不支持此功能 ”
- JS组件系列——自己动手扩展BootstrapTable的 冻结列 功能:彻底解决高度问题
- 解决还原数据库是出现system.data.sqlclient.sqlerror filestream功能被禁用的问题
- 关于nodejs express4.X框架不支持layout模板的问题解决
- .Net Core 下使用ZKWeb.System.Drawing实现验证码功能(图形验证码)
- 解决此word打开时提示:文档有宏 该应用程序的宏语言支持功能被取消 问题
- 升级到安卓5.0后,和包提示:“检测到您的手机或sim卡不完全支持和包业务,部分NFC相关功能将无法正常使用”的问题解决办法
- 关于nodejs express4.X框架不支持layout模板的问题解决
- 关于nodejs express4.X框架不支持layout模板的问题解决
- 解决nodejs不支持async和await关键字的问题
- JS 组件系列之Bootstrap Table的冻结列功能彻底解决高度问题
- .Net Core 下使用ZKWeb.System.Drawing实现验证码功能(图形验证码)
- 关于LaTeX + LyX + R/Sweave + pgfSweave + PDF图形的若干中文问题(使用knitr画图时中文乱码解决)
- 解决Java原生压缩组件不支持中文文件名乱码的问题