Skip to content

A dynamic image generator.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

sigoden/dynimgen

Repository files navigation

dynimgen

CI Crates

A self-hosted dynamic image generator.

demo

How to use

step 1: The designers export the design drawing as an svg file

<svg>
  <rect />
  <image src="img.png" /> 
  <image src="qr.png" />
  <text>66666</text>
</svg>

step 2: The engineers edit the svg file, replace the changed parts with template variables

<svg>
  <rect />
  <img src="{{ img | fetch }}">
  <img src="{{ qr | to_qr }}">
  <text>{{ code }}</text>
</svg>

step 3: Run dynimgen, make sure the svg template is in the dynimgen workdir

$ ls data/
Times New Roman.ttf   poster1.svg

$ dynimgen data/
[2022-06-05T14:51:53Z INFO  dynimgen::generator] Mount `/poster1`
[2022-06-05T14:51:53Z INFO  dynimgen] Listen on 0.0.0.0:8080

step 4: Build image url according to the following rules

<domain> + <template path> + ? + <template variables>

For example:

Build url:

https://localhost:8080/poster1?img=https://picsum.photos/250&qr=dynimgen&code=12345

If you request this url, dynimgen will response a png image.

what dynimgen does:

  1. Extract path and variables from the request
  2. Pass variables to template engine to generate a svg
  3. Render the svg to a png then response

Advantages

Server-side rendering advantages

  • No browser compatibility, platform compatibility and other issues
  • Code reusability is high, and the poster generation service of h5, applet, and app can be used.
  • Can be updated in a timely and convenient manner

SVG Template Advantages

  • Export SVG directly from design software such as PS/AI
  • No need to reproduce the implementation with html/canvas/dsl
  • Lossless restoration of the design draft without losing any details
  • Free use of font styles

RUST Advantage

  • High performance, high concurrency
  • Single executable program, cross-platform, easy to deploy

Install

With cargo

cargo install dynimgen

With docker

docker run -v `pwd`/data:/data  -p 8080:8080 --rm -it sigoden/dynimgen /data

Binaries on macOS, Linux, Windows

Download from Github Releases, unzip and add duf to your $PATH.

Template Engine

dynimgen uses Tera as the template engine. It has a syntax based on Jinja2 and Django templates.

See the Tera Documentation for more information about control structures, built-ins filters, etc.

Custom built-in filters that dynimgen uses:

fetch

Fetch remote resource and encode as data-url

Example: {{ img | fetch }} {{ img | fetch(timeout=10000) }}

to_qr

Convert text to qrcode

Example: {{ qr | to_qr }} {{ qr | to_qr(bg='#fff', fg='#000') }}

License

Copyright (c) 2022 dynimgen-developers.

dynimgen is made available under the terms of either the MIT License or the Apache License 2.0, at your option.

See the LICENSE-APACHE and LICENSE-MIT files for license details.