diff --git a/OliverBooth/Data/Web/Book.cs b/OliverBooth/Data/Web/Book.cs index 385e870..465e0f3 100644 --- a/OliverBooth/Data/Web/Book.cs +++ b/OliverBooth/Data/Web/Book.cs @@ -1,3 +1,9 @@ +using NetBarcode; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Formats.Png; +using SixLabors.ImageSharp.Processing; +using Type = System.Type; + namespace OliverBooth.Data.Web; /// @@ -16,4 +22,16 @@ internal sealed class Book : IBook /// public string Title { get; } -} + + public string GetBarcode() + { + var barcode = new Barcode(Isbn, NetBarcode.Type.EAN13); + using var image = barcode.GetImage(); + int width = image.Width; + int height = image.Height; + image.Mutate(i => i.Pad(width + 10, height + 10, Color.White)); + image.Mutate(i => i.Resize(i.GetCurrentSize() / 4 * 3)); + + return image.ToBase64String(PngFormat.Instance); + } +} \ No newline at end of file diff --git a/OliverBooth/Data/Web/IBook.cs b/OliverBooth/Data/Web/IBook.cs index 1c968cf..05c7f32 100644 --- a/OliverBooth/Data/Web/IBook.cs +++ b/OliverBooth/Data/Web/IBook.cs @@ -1,3 +1,5 @@ +using SixLabors.ImageSharp; + namespace OliverBooth.Data.Web; /// @@ -28,4 +30,11 @@ public interface IBook /// /// The title of the book. string Title { get; } + + /// + /// Generates the barcode for this book. + /// + /// The EAN-13 barcode encoded as PNG in Base64. + /// This value should be disposed. + string GetBarcode(); } \ No newline at end of file diff --git a/OliverBooth/Dockerfile b/OliverBooth/Dockerfile index 40e2a42..b9596e0 100644 --- a/OliverBooth/Dockerfile +++ b/OliverBooth/Dockerfile @@ -2,6 +2,7 @@ FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base WORKDIR /app EXPOSE 80 EXPOSE 443 +RUN apt install -y libc6-dev libgdiplus FROM node:20-alpine as build-deps WORKDIR /src diff --git a/OliverBooth/OliverBooth.csproj b/OliverBooth/OliverBooth.csproj index fce093d..1c6ddab 100644 --- a/OliverBooth/OliverBooth.csproj +++ b/OliverBooth/OliverBooth.csproj @@ -18,6 +18,7 @@ + diff --git a/OliverBooth/Pages/Books.cshtml b/OliverBooth/Pages/Books.cshtml index 6d73685..12d8138 100644 --- a/OliverBooth/Pages/Books.cshtml +++ b/OliverBooth/Pages/Books.cshtml @@ -1,5 +1,10 @@ @page +@using NetBarcode @using OliverBooth.Data.Web +@using SixLabors.ImageSharp +@using SixLabors.ImageSharp.Formats.Png +@using SixLabors.ImageSharp.Processing +@using Type = NetBarcode.Type @model OliverBooth.Pages.Books @{ ViewData["Title"] = "Reading List"; @@ -33,7 +38,7 @@ @book.Title.Trim() @book.Author.Trim() - @book.Isbn.Trim() + @book.Isbn.Trim()
@book.Isbn } @@ -58,7 +63,7 @@ @book.Title.Trim() @book.Author.Trim() - @book.Isbn.Trim() + @book.Isbn.Trim()
@book.Isbn } @@ -83,7 +88,7 @@ @book.Title.Trim() @book.Author.Trim() - @book.Isbn.Trim() + @book.Isbn.Trim()
@book.Isbn }