diff --git a/samples/misc/NodeServicesExamples/Controllers/HomeController.cs b/samples/misc/NodeServicesExamples/Controllers/HomeController.cs index be21da6..34ce07c 100755 --- a/samples/misc/NodeServicesExamples/Controllers/HomeController.cs +++ b/samples/misc/NodeServicesExamples/Controllers/HomeController.cs @@ -15,6 +15,11 @@ namespace NodeServicesExamples.Controllers return View(); } + public IActionResult ImageResizing() + { + return View(); + } + public IActionResult Error() { return View("~/Views/Shared/Error.cshtml"); diff --git a/samples/misc/NodeServicesExamples/Controllers/ResizeImage.cs b/samples/misc/NodeServicesExamples/Controllers/ResizeImage.cs new file mode 100644 index 0000000..1606a0d --- /dev/null +++ b/samples/misc/NodeServicesExamples/Controllers/ResizeImage.cs @@ -0,0 +1,61 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.NodeServices; +using Microsoft.AspNetCore.StaticFiles; + +namespace NodeServicesExamples.Controllers +{ + public class ResizeImageController : Controller + { + private const int MaxDimension = 1000; + + private IHostingEnvironment _environment; + private INodeServices _nodeServices; + + public ResizeImageController(IHostingEnvironment environment, INodeServices nodeServices) + { + _environment = environment; + _nodeServices = nodeServices; + } + + [Route("resize/{*imagePath}")] + public async Task Index(string imagePath, int maxWidth, int maxHeight) + { + // Validate incoming params + if (maxWidth > MaxDimension || maxHeight > MaxDimension || (maxHeight <= 0 && maxWidth <= 0)) + { + return BadRequest("Invalid dimensions"); + } + + // Locate source image on disk + var fileInfo = _environment.WebRootFileProvider.GetFileInfo(imagePath); + if (!fileInfo.Exists) + { + return NotFound(); + } + + // Invoke Node and convert the base64 result back to bytes + var mimeType = GetContentType(imagePath); + var resizedImage = await _nodeServices.Invoke("./Node/resizeImage", fileInfo.PhysicalPath, mimeType, maxWidth, maxHeight); + return File(Convert.FromBase64String(resizedImage.Base64), mimeType); + } + + private string GetContentType(string path) + { + string result; + if (!new FileExtensionContentTypeProvider().TryGetContentType(path, out result)) + { + result = "application/octet-stream"; + } + + return result; + } + + private class ResizeImageResult + { + public string Base64 { get; set; } + } + } +} diff --git a/samples/misc/NodeServicesExamples/Node/resizeImage.js b/samples/misc/NodeServicesExamples/Node/resizeImage.js new file mode 100644 index 0000000..119f94a --- /dev/null +++ b/samples/misc/NodeServicesExamples/Node/resizeImage.js @@ -0,0 +1,15 @@ +var Jimp = require('jimp'); + +module.exports = function(cb, physicalPath, mimeType, maxWidth, maxHeight) { + Jimp.read(physicalPath, function (err, loadedImage) { + if (err) { + cb(err); + } + + loadedImage + .contain(maxWidth > 0 ? maxWidth : Jimp.AUTO, maxHeight > 0 ? maxHeight : Jimp.AUTO) + .getBuffer(mimeType, function(err, buffer) { + cb(err, { base64: buffer && buffer.toString('base64') }); + }); + }); +} diff --git a/samples/misc/NodeServicesExamples/Views/Home/ImageResizing.cshtml b/samples/misc/NodeServicesExamples/Views/Home/ImageResizing.cshtml new file mode 100644 index 0000000..1ff36af --- /dev/null +++ b/samples/misc/NodeServicesExamples/Views/Home/ImageResizing.cshtml @@ -0,0 +1,26 @@ +

Image Resizing

+ +

+ This sample shows how the NPM module jimp + can be used for dynamic image resizing from within an ASP.NET Core application. There is one copy of the + following image on disk, but we can set up an MVC action method that returns it resized to fit within an + arbitrary width and height. +

+ +

+ Parrot + by D Coetzee + is dedicated to the public domain (CC0) +

+ +

100px wide [open]

+ + +

200px wide [open]

+ + +

400px wide [open]

+ + +

800px wide [open]

+ diff --git a/samples/misc/NodeServicesExamples/Views/Home/Index.cshtml b/samples/misc/NodeServicesExamples/Views/Home/Index.cshtml index 21dcd3b..50d388c 100644 --- a/samples/misc/NodeServicesExamples/Views/Home/Index.cshtml +++ b/samples/misc/NodeServicesExamples/Views/Home/Index.cshtml @@ -8,5 +8,6 @@