Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/ConversionTool/bin/Debug/net5.0/ConversionTool.dll",
"program": "${workspaceFolder}/ConversionTool/bin/Debug/net7.0/ConversionTool.dll",
"args": [],
"cwd": "${workspaceFolder}/ConversionTool",
"stopAtEntry": false,
Expand Down
15 changes: 3 additions & 12 deletions ConversionTool/Controllers/HtmlToImageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

namespace ConversionTool.Controllers
Expand All @@ -25,19 +24,11 @@ public HtmlToImageController(ILogger<HtmlToImageController> logger, IHtmlToImage
[HttpPost("/html-to-image/png")]
public async Task<IActionResult> Png([FromBody] HtmlToImageModel htmlToImageModel)
{
try
{
_logger.LogInformation("Converting HTML to PNG image");
_logger.LogInformation("Converting HTML to PNG image");

var imageAsBytes = await _htmlToImage.FromStringToPngAsync(htmlToImageModel.Html, htmlToImageModel.Height, htmlToImageModel.Width);
var imageAsBytes = await _htmlToImage.FromStringToPngAsync(htmlToImageModel.Html, htmlToImageModel.Height, htmlToImageModel.Width);

return File(imageAsBytes, "image/png");
}
catch (Exception ex)
{
_logger.LogError("Error converting HTML text to PNG image", ex);
return BadRequest(ex);
}
return File(imageAsBytes, "image/png");
}
}
}
4 changes: 3 additions & 1 deletion ConversionTool/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
RUN apt-get update && apt-get install wkhtmltopdf -y && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install wkhtmltopdf binutils -y && rm -rf /var/lib/apt/lists/*
RUN strip --remove-section=.note.ABI-tag /usr/lib/x86_64-linux-gnu/libQt5Core.so.5

WORKDIR /app
EXPOSE 80

Expand Down
20 changes: 8 additions & 12 deletions ConversionTool/HtmlToImage/WkHtmlToImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ConversionTool.HtmlToImage
{
public class WkHtmlToImage : IHtmlToImage
{
IAppConfiguration _appConfiguration;
readonly IAppConfiguration _appConfiguration;
public WkHtmlToImage(IAppConfiguration appConfiguration)
{
_appConfiguration = appConfiguration;
Expand All @@ -31,18 +31,14 @@ public Task<byte[]> FromStringToPngAsync(string html, int? height = null, int? w

private byte[] ResizeImage(byte[] image, int? height, int? width)
{
using (Image imageRgba = Image.Load(image))
{
using (MemoryStream stream = new MemoryStream())
{
var withToMutate = width ?? imageRgba.Width * _appConfiguration.ImageSizeResult.Height / imageRgba.Height;
var heightToMutate = height ?? _appConfiguration.ImageSizeResult.Height;
using Image imageRgba = Image.Load(image);
using MemoryStream stream = new();
var withToMutate = width ?? imageRgba.Width * _appConfiguration.ImageSizeResult.Height / imageRgba.Height;
var heightToMutate = height ?? _appConfiguration.ImageSizeResult.Height;

imageRgba.Mutate(x => x.Resize(withToMutate, heightToMutate, KnownResamplers.Lanczos3));
imageRgba.Save(stream, new PngEncoder());
return stream.ToArray();
}
}
imageRgba.Mutate(x => x.Resize(withToMutate, heightToMutate, KnownResamplers.Lanczos3));
imageRgba.Save(stream, new PngEncoder());
return stream.ToArray();
}
}
}