Mirror of
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #32 from trueberryless-org/feat/image-formats

authored by

Felix Schneider and committed by
GitHub
015ae4c1 d82df0db

+51 -11
+5
.changeset/fresh-teams-wait.md
··· 1 + --- 2 + "release-image-generator": minor 3 + --- 4 + 5 + Add support for different image formats. Support formats png, jpg and webp. Read more in [the docs](https://github.com/trueberryless-org/release-image-generator#usage).
+11 -3
README.md
··· 3 3 Get your random default image from [https://release-image-generator.trueberryless.org/](https://release-image-generator.trueberryless.org/) and adjust some parameters in the URL: 4 4 5 5 - **text**: Configure the text display in the middle of the image: [https://release-image-generator.trueberryless.org?text=2.0](https://release-image-generator.trueberryless.org?text=2.0), by default there is no text 6 - - **width** & **height**: Configure the width and height of the image: [https://release-image-generator.trueberryless.org?width=1560&height=640](https://release-image-generator.trueberryless.org?width=1560&height=640) 6 + - **width** & **height**: Configure the width and height of the image (default: 1920x1080): [https://release-image-generator.trueberryless.org?width=1560&height=640](https://release-image-generator.trueberryless.org?width=1560&height=640) 7 7 - **primaryColor**: Set the HEX value of the desired primary color (with or without the octothorpe - escape `#` with `%23`); example with pink: [https://release-image-generator.trueberryless.org/?text=1.0&primaryColor=%23ffc0d2](https://release-image-generator.trueberryless.org/?text=1.0&primaryColor=%23ffc0d2) 8 + - **imageFormat**: Define the desired image output format: [https://release-image-generator.trueberryless.org?text=1.0&imageFormat=jpg](https://release-image-generator.trueberryless.org?text=1.0&imageFormat=jpg) 9 + 10 + Image Format Options: 11 + 12 + - `png` (default) 13 + - `jpg` / `jpeg` 14 + - `webp` 15 + 8 16 - **fontFamily** & **fontWeight**: Configure the font family and weight used: [https://release-image-generator.trueberryless.org?text=1.0&fontFamily=jetbrainsmono&fontWeight=bold](https://release-image-generator.trueberryless.org?text=1.0&fontFamily=jetbrainsmono&fontWeight=bold) 9 17 10 18 Family Options: ··· 17 25 - `poppins` 18 26 - `quicksand` 19 27 - `raleway` 20 - - `readexpro` 28 + - `readexpro` (default) 21 29 - `roboto` 22 30 - `robotomono` 23 31 - `rubik` ··· 25 33 26 34 Weight Options: 27 35 28 - - `bold` 36 + - `bold` (default) 29 37 - `medium` 30 38 - `light` 31 39
+12 -3
app/ReleaseImageGenerator.API/Program.cs
··· 19 19 app.UseHttpsRedirection(); 20 20 21 21 app.MapGet("/", 22 - (string? text, int? width, int? height, SupportedFontFamily? fontFamily, SupportedFontWeight? fontWeight, string? primaryColor) => 22 + (string? text, int? width, int? height, SupportedFontFamily? fontFamily, SupportedFontWeight? fontWeight, 23 + string? primaryColor, SupportedImageFormat? imageFormat) => 23 24 { 24 25 var options = new ImageGeneratorOptions(text, width ?? 1920, height ?? 1080, 25 - fontFamily ?? SupportedFontFamily.readexpro, fontWeight ?? SupportedFontWeight.bold, primaryColor); 26 + fontFamily ?? SupportedFontFamily.readexpro, fontWeight ?? SupportedFontWeight.bold, primaryColor, 27 + imageFormat ?? SupportedImageFormat.png); 26 28 var imageGenerator = new ImageGenerator(options); 27 - return Results.File(imageGenerator.GenerateImage().ToArray(), "image/png"); 29 + return Results.File(imageGenerator.GenerateImage().ToArray(), imageFormat switch 30 + { 31 + SupportedImageFormat.jpeg => "image/jpeg", 32 + SupportedImageFormat.jpg => "image/jpeg", 33 + SupportedImageFormat.png => "image/png", 34 + SupportedImageFormat.webp => "image/webp", 35 + _ => "image/png" 36 + }); 28 37 }) 29 38 .WithName("ImageGenerator") 30 39 .WithOpenApi();
+13 -3
app/ReleaseImageGenerator.Domain/Implementations/ImageGenerator.cs
··· 12 12 public SupportedFontFamily FontFamily { get; set; } 13 13 public SupportedFontWeight FontWeight { get; set; } 14 14 public string? PrimaryColor { get; set; } 15 + public SupportedImageFormat ImageFormat { get; set; } 15 16 16 17 public ImageGenerator(ImageGeneratorOptions options) 17 18 { ··· 21 22 FontFamily = options.fontFamily; 22 23 FontWeight = options.fontWeight; 23 24 PrimaryColor = options.primaryColor; 25 + ImageFormat = options.imageFormat; 24 26 } 25 27 26 28 public MemoryStream GenerateImage() ··· 38 40 NoiseGenerator.GenerateNoise(canvas, Width, Height, random); 39 41 if (Text != null) TextGenerator.GenerateText(canvas, Text, Width, Height, FontFamily, FontWeight, primaryColor); 40 42 41 - // Return as PNG 43 + // Encode and return image in the requested format 42 44 var stream = new MemoryStream(); 43 45 using var image = surface.Snapshot(); 44 - using var data = image.Encode(SKEncodedImageFormat.Png, 100); 46 + using var data = image.Encode(ImageFormat switch 47 + { 48 + SupportedImageFormat.jpeg => SKEncodedImageFormat.Jpeg, 49 + SupportedImageFormat.jpg => SKEncodedImageFormat.Jpeg, 50 + SupportedImageFormat.png => SKEncodedImageFormat.Png, 51 + SupportedImageFormat.webp => SKEncodedImageFormat.Webp, 52 + _ => SKEncodedImageFormat.Png 53 + }, 100); 45 54 data.SaveTo(stream); 55 + stream.Position = 0; 46 56 return stream; 47 57 } 48 - } 58 + }
+10 -2
app/ReleaseImageGenerator.Domain/Implementations/ImageGeneratorOptions.cs
··· 1 1 namespace ReleaseImageGenerator.Domain.Implementations; 2 2 3 - public record ImageGeneratorOptions(string? text, int width, int height, SupportedFontFamily fontFamily, SupportedFontWeight fontWeight, string? primaryColor); 3 + public record ImageGeneratorOptions(string? text, int width, int height, SupportedFontFamily fontFamily, SupportedFontWeight fontWeight, string? primaryColor, SupportedImageFormat imageFormat); 4 4 5 5 public enum SupportedFontFamily 6 6 { ··· 24 24 bold, 25 25 medium, 26 26 light 27 - } 27 + } 28 + 29 + public enum SupportedImageFormat 30 + { 31 + jpeg, 32 + jpg, 33 + png, 34 + webp 35 + }