internal static SKBitmap WatermarkImage(SKBitmap original, ResizeParams resizeParams, WatermarkImageModel watermarkImage) { return(null); }
public async Task InvokeAsync(HttpContext context) { var path = context.Request.Path; var rootPath = _env.WebRootPath ?? _env.ContentRootPath; //use web or content root path // hand to next middleware if we are not dealing with an image if (context.Request.Query.Count == 0 || !IsImagePath(path)) { await _req.Invoke(context); return; } // hand to next middleware if we are dealing with an image but it doesn't have any usable resize querystring params var resizeParams = GetResizeParams(path, context.Request.Query); if (!resizeParams.hasParams) { await _req.Invoke(context); return; } var imageResizerJsonPath = Path.Combine(rootPath, "ImageResizerJson.json"); // if json file doesn't exist we don't work with watermark if (File.Exists(imageResizerJsonPath)) { var watermarks = new WatermarksModel(); using (StreamReader r = new StreamReader(imageResizerJsonPath)) { string json = r.ReadToEnd(); watermarks = JsonConvert.DeserializeObject <WatermarksModel>(json); } if (resizeParams.wmtext != 0) { if (watermarks.WatermarkTextList.Any()) { watermarkText = watermarks.WatermarkTextList.FirstOrDefault(p => p.Key == resizeParams.wmtext); } } if (resizeParams.wmimage != 0) { if (watermarks.WatermarkImageList.Any()) { watermarkImage = watermarks.WatermarkImageList.FirstOrDefault(p => p.Key == resizeParams.wmimage); } } } // if we got this far, resize it _logger.LogInformation($"Resizing {path.Value} with params {resizeParams}"); //update the path to deal with ";" var pathValue = path.Value; if (pathValue.Contains(";")) { pathValue = pathValue.Split(';')[0]; } var provider = new PhysicalFileProvider(rootPath); var imagePath = provider.GetFileInfo(pathValue).PhysicalPath; //// get the image location on disk //var imagePath = Path.Combine( // rootPath.Replace("\\wwwroot", ""), // path.Value.Replace('/', Path.DirectorySeparatorChar).TrimStart(Path.DirectorySeparatorChar)); // check file lastwrite var lastWriteTimeUtc = File.GetLastWriteTimeUtc(imagePath); if (lastWriteTimeUtc.Year == 1601) // file doesn't exist, pass to next middleware { await _req.Invoke(context); return; } var imageData = GetImageData(imagePath, resizeParams, lastWriteTimeUtc); // write to stream context.Response.ContentType = resizeParams.format == "png" ? "image/png" : "image/jpeg"; context.Response.ContentLength = imageData.Size; await context.Response.Body.WriteAsync(imageData.ToArray(), 0, (int)imageData.Size); // cleanup imageData.Dispose(); }