#image #canvas #open-graphic #og

og_image_writer

Generate an Open Graphic Image using a CSS-like API

21 releases (9 breaking)

0.9.1 Jan 25, 2022
0.8.3 Jan 4, 2022
0.8.0 Dec 31, 2021
0.3.1 Nov 14, 2021

#350 in Web programming

Download history 25/week @ 2022-01-25 6/week @ 2022-02-01 8/week @ 2022-02-08 5/week @ 2022-02-15 23/week @ 2022-02-22 1/week @ 2022-03-01 85/week @ 2022-03-15 63/week @ 2022-03-22 21/week @ 2022-04-05 1/week @ 2022-04-19 147/week @ 2022-04-26 170/week @ 2022-05-03 316/week @ 2022-05-10

634 downloads per month

MIT license

105KB
2.5K SLoC

Open Graphic Image Writer

Documentation

You can generate Open Graphic Image dynamically.

  • A CSS-like API.
  • You can generate image by using template image.
  • WASM/WASI support.

Problem

Currently, when you want to create OGP image dynamically, you may use canvas. But to use canvas, you need to open a browser and run some script. This is overhead, especially if you are using feature like the SSG. Therefore this lib is targeting a high performance API for all platform by using wasm.

Example

For more examples, see keiya01/og_image_writer/examples.

use og_image_writer::{style, writer::OGImageWriter};
use std::path::Path;

fn main() -> anyhow::Result<()> {
    let text = "This is Open Graphic Image Writer for Web Developer.";

    let mut writer = OGImageWriter::new(style::WindowStyle {
        width: 1024,
        height: 512,
        background_color: Some(style::Rgba([70, 40, 90, 255])),
        align_items: style::AlignItems::Center,
        justify_content: style::JustifyContent::Center,
        ..style::WindowStyle::default()
    })?;

    let font = Vec::from(include_bytes!("../fonts/Mplus1-Black.ttf") as &[u8]);

    writer.set_text(
        text,
        style::Style {
            margin: style::Margin(0, 20, 0, 20),
            line_height: 1.8,
            font_size: 100.,
            word_break: style::WordBreak::Normal,
            color: style::Rgba([255, 255, 255, 255]),
            text_align: style::TextAlign::Start,
            ..style::Style::default()
        },
        font,
    )?;

    let out_dir = "./examples";
    let out_filename = "output_background_color.png";

    writer.generate(Path::new(&format!("{}/{}", out_dir, out_filename)))?;

    Ok(())
}

When you pass the following the template image,

example template image

this code generate the following image.

example output image

And you can also set background-color instead of passing the template image.

For more examples, see keiya01/og_image_writer/examples.

Dependencies

~9MB
~152K SLoC