Generate a TwitterCard(OGP) image for your Hugo posts.

Twitter Card Image Generator

Generate Twitter card (OGP) images for your blog posts. Supported front-matters are title, author, categories, tags, and date. Also, both toml and yaml formats are supported.

sample

Installation

go get github.com/Ladicle/tcardgen

Getting Started

  1. Install tcardgen command
  2. Download your favorite TrueType fonts (the above sample use KintoSans)
  3. Create template image (The easyest way is to replace the author image of the template in the example directory.)
  4. Run the following command

NOTE: tcardgen parses a font style from the file name, so the font file must follow the naming rule (<name>-<style>.ttf), and arrange font files as follows:

$ tree font/
font/
├── KintoSans-Bold.ttf
├── KintoSans-Medium.ttf
└── KintoSans-Regular.ttf

0 directories, 3 files

$ tcardgen -f path/to/fontDir \
           -o path/to/hugo/static/imgDir \
           -t path/to/templateFile \
           path/to/hugo/content/posts/*.md

After successfully executing the command, a PNG image with the same name as the specified content name is generated in the output directory.

Advanced Generation

If you want to change the color, style, or position of text, you can pass a configuration file with the --config(-c) option. Refer to the example/template3.config.yaml to see how to configure it.

$ tcardgen -c example/template3.config.yaml example/blog-post2.md
Load fonts from "font" directory
Load template from "example/template3.png"
Success to generate twitter card into out/blog-post2.png

Result

OGP setting for Hugo Theme

On my blog, I place the generated images in the static/tcard directory. In order to load this image, I set the following OGP information for my blog theme. If the thumbnail is defined in the post, it is used first. Otherwise, the generated Twitter Card is used. If the page is not blog post, to set the default image.

<!-- General -->
<meta property="og:url" content="{{ .Permalink }}" />
<meta property="og:type" content="{{ if .IsHome }}website{{ else }}article{{ end }}" />
<meta property="og:site_name" content="{{ .Site.Title }}" />
<meta property="og:title" content="{{ .Title }}" />
<meta property="og:description" content="{{ with .Description -}}{{ . }}{{ else -}}{{ if .IsPage }}{{ substr .Summary 0 300 }}{{ else }}{{ with .Site.Params.description }}{{ . }}{{ end }}{{ end }}{{ end }}" />
<meta property="og:image" content="{{ if .Params.thumbnail -}}{{ .Params.thumbnail|absURL }}{{ else if hasPrefix .File.Path "post" -}}{{ path.Join "tcard" (print .File.BaseFileName ".png") | absURL }}{{ else -}}{{ "img/default.png" | absURL }}{{ end -}}" />
<!-- Twitter -->
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@{{ .Site.Params.twitterName }}" />

Generate images of updated articles

You can generate only the image of the updated article by using git diff and tcardgen.

$ git diff --name-only HEAD content/post |\
    xargs tcardgen -o static/tcard -f assets/fonts/kinto-sans -t assets/template.png

Usage

$ tcardgen -h
Generate TwitterCard(OGP) images for your Hugo posts.
Supported front-matters are title, author, categories, tags, and date.

Usage:
  tcardgen [-f <FONTDIR>] [-o <OUTPUT>] [-t <TEMPLATE>] [-c <CONFIG>] <FILE>...

Examples:
# Generate a image and output to the example directory.
tcardgen --fontDir=font --output=example --template=example/template.png example/blog-post.md

# Generate a image and output to the example directory as "featured.png".
tcardgen --fontDir=font --output=example/featured.png --template=example/template.png example/blog-post.md

# Generate multiple images.
tcardgen --template=example/template.png example/*.md

# Genrate an image based on the drawing configuration.
tcardgen --config=config.yaml example/*.md

Flags:
  -c, --config string     Set a drawing configuration file.
  -f, --fontDir string    Set a font directory. (default "font")
  -h, --help              help for tcardgen
      --outDir string     (DEPRECATED) Set an output directory.
  -o, --output string     Set an output directory or filename (only png format). (default "out")
  -t, --template string   Set a template image file. (default example/template.png)
Owner
Aya Igarashi
Software Engineer @zlabjp
Aya Igarashi
Comments
  • Specifying the output file name

    Specifying the output file name

    Hi @Ladicle, thanks for your work of developing this tool.

    Today, I have a proposal.

    Proposal

    I want a feature of specifying the output file name.

    So, if you set "directory" (ex. ./path/to/directory) as the value of -f option, then output image file name is default one. On the other hand, if you set "file name" (ex. ./path/to/directory/ogp.png) as the value of -f option, then output image file name is "ogp.png".

    This way is compatible. And, of course, I accept other way. I hope you will consider it.

  • Issue while creating with multiple image

    Issue while creating with multiple image

    It is a great tool for me. I created a single twitter card by specifying the MD file name specifically. And my command was like this. tcardgen -f next.medigy.com/static/font/ \ -o news-content/ \ -t tcardgen/example/template.png \ news-content/healthtechmagazine-why-telehealth-solutions-are-vital-in-a-disaster.md

    But I tried the same for multiple images creation which I have a lot of MD files in my content folder. And my command was like this. tcardgen -f next.medigy.com/static/font/ \ -o news-content/ \ -t tcardgen/example/template.png \ news-content/*.md

    And I got the following error message.

    Load fonts from "next.medigy.com/static/font/" Load template from "tcardgen/example/template.png" directory Failed to generate twitter card for news-content.png: failed to read page content: read : The handle is invalid. Failed to generate twitter card for news-content.png: failed to read page content: read : The handle is invalid. Failed to generate twitter card for news-content.png: failed to read page content: read : The handle is invalid. Failed to generate twitter card for news-content*.png: open news-content/*.md: The filename, directory name, or volume label syntax is incorrect. 2020/06/30 19:25:27 failed to generate 4 twitter cards

    Could you please help me to resolve this issue? Please check the screenshot of the same.

    twitter-card

  • Accept single author

    Accept single author

    Hi @Ladicle,

    I'm bringing the proposal again.

    Proposal

    I want to accept both single author and multiple one.

    author = ["yyh-gl"]
    

    author = "yyh-gl"
    

    On some hoge themes, the UIs change whether it is an array or not. So, I have to identify A or B.

  • "author" type of string can not convert to []interface{}

    Hi Ladicle, thanks for this amazing library!

    I'm having a problem when I hit the generate command, it says "author" type of string can not convert to []interface{}

    I'm putting regular string into my markdown frontmatter like this:

    author: nsebhastian
    

    Could you help me figure out what went wrong here? Thanks again!

  • Add the installation guide for go1.16+

    Add the installation guide for go1.16+

    In go1.18, the installation with go get is no longer working. This PR adds the installation way with go install.

    $ go version
    go version go1.18.3 darwin/arm64
    $ go get github.com/Ladicle/tcardgen
    go: go.mod file not found in current directory or any parent directory.
            'go get' is no longer supported outside a module.
            To build and install a command, use 'go install' with a version,
            like 'go install example.com/cmd@latest'
            For more information, see https://golang.org/doc/go-get-install-deprecation
            or run 'go help get' or 'go help install'.
    
  • make name/date separator configurable

    make name/date separator configurable

    The font I wanted to use doesn't include the "・" character, so the resulting image had a unicode error ? thing instead. This PR keeps the "・" as the default but lets the user configure a different separator if they want. (It also adds that to the sample config so users can know the option is there.)

  • Extend the way to accept author data

    Extend the way to accept author data

    Overview

    This PR resolves issue https://github.com/Ladicle/tcardgen/issues/9.

    I extend the way to accept author data.

    Changes

    • Add isArray()
      • isArray() determines if A is an array
    • Branching out the process by author data format

    Tests

    • [x] Generate the image
      • [x] With single author
      • [x] With multiple author
  • Add feature to specify output file's name

    Add feature to specify output file's name

    Overview

    This PR resolves issue https://github.com/Ladicle/tcardgen/issues/6.

    I added the feature to specify output file's name.

    Changes

    • Add output flag
    • outDir flag is deprecated

    Tests

    • [x] Generate the image using --outDir
      • [x] Show warning
      • [x] Accept both single file and multiple files
    • [x] Generate the image using --output (-o)
      • [x] Specify directory as output destination.
        • [x] Accept single file
        • [x] Accept multiple files
      • [x] Specify filename as output destination.
        • [x] Accept single file
        • [x] Can't accept multiple files
    • [x] --output takes priority over --outDir
    • [x] Show help
      • [x] --outDir's deprecated message
      • [x] output's message
  • Fix runtime error

    Fix runtime error "index out of range"

    If authors or categories in frontmatter are empty, then length of the return value of getFirstString function will be zero. This occurs "index out of range" runtime error.

    $ tcardgen -f hack/card/font -t hack/card/template.png content/test.md
    Load fonts from "hack/card/font"
    Load template from "hack/card/template.png" directory
    panic: runtime error: index out of range [0] with length 0
    
    goroutine 1 [running]:
    github.com/Ladicle/tcardgen/pkg/hugo.getFirstStringItem(...)
            /Users/ksuda/src/github.com/Ladicle/tcardgen/pkg/hugo/frontmatter.go:137
    github.com/Ladicle/tcardgen/pkg/hugo.ParseFrontMatter(0x7ffeefbfefd5, 0x1f, 0x0, 0x0, 0x0)
            /Users/ksuda/src/github.com/Ladicle/tcardgen/pkg/hugo/frontmatter.go:50 +0x53a
    github.com/Ladicle/tcardgen/cmd.generateTCard(0x7ffeefbfefd5, 0x1f, 0xc000024380, 0x1c, 0x15bff40, 0xc000090080, 0xc0000c5900, 0xc0000fdcd0, 0x0, 0x0)
            /Users/ksuda/src/github.com/Ladicle/tcardgen/cmd/cmd.go:143 +0x50
    github.com/Ladicle/tcardgen/cmd.(*RootCommandOption).Run(0xc0000cf4a0, 0x15b7360, 0xc0000cc008, 0x15b7360, 0xc0000cc010, 0x11d2120, 0xc0000fdd48)
            /Users/ksuda/src/github.com/Ladicle/tcardgen/cmd/cmd.go:128 +0x4b6
    github.com/Ladicle/tcardgen/cmd.NewRootCmd.func1(0xc0001a4500, 0xc0000d31e0, 0x11, 0x15, 0x0, 0x0)
            /Users/ksuda/src/github.com/Ladicle/tcardgen/cmd/cmd.go:75 +0xa7
    github.com/spf13/cobra.(*Command).execute(0xc0001a4500, 0xc0000d2010, 0x15, 0x15, 0xc0001a4500, 0xc0000d2010)
            /Users/ksuda/pkg/mod/github.com/spf13/[email protected]/command.go:826 +0x460
    github.com/spf13/cobra.(*Command).ExecuteC(0xc0001a4500, 0x0, 0x0, 0x0)
            /Users/ksuda/pkg/mod/github.com/spf13/[email protected]/command.go:914 +0x2fb
    github.com/spf13/cobra.(*Command).Execute(...)
            /Users/ksuda/pkg/mod/github.com/spf13/[email protected]/command.go:864
    main.main()
            /Users/ksuda/src/github.com/Ladicle/tcardgen/main.go:17 +0x27
    

    content/test.md:

    ---
    title: "My First Post"
    date: 2019-03-24T00:22:11Z
    draft: false
    tags: ["hugo", "hugo theme", "netlify"]
    ---
    
    Hello world
    
  • How to use on macOS?

    How to use on macOS?

    I have installed go using brew install go and then ran go install github.com/Ladicle/tcardgen@latest. But that does not seem to make tcardgen work. I get:

    > tcardgen
    zsh: command not found: tcardgen
    

    The version of go:

    > go version
    go version go1.19.2 darwin/amd64
    

    What should I do to get this working on macOS?

  • Specifying template with .JPEG extension

    Specifying template with .JPEG extension

    Is it possible to give the template name with a .jpeg extension or is it possible to generate the output as .jpeg? When I tried with the .png extension, the output folder size became too large. So I want to reduce the output folder size when I'm dealing with a large number of MD files.

  • Configuring default author info

    Configuring default author info

    I think it's inconvenient for users that the author's icon used in the template image is fixed but the name is variable.

    I want to set default author information from config file.

    author:
     - name: foo
    
  • Arabic Font Issue

    Arabic Font Issue

    Hi @Ladicle , Its a great tool and worked well for English content. I am having Arabic text too as description in my MD file and the image generated is having boxes in place of Arabic text. I tried by using Arabic font "Traditional Arabic" in front directory then it throws an error "Failed to load Traditional Arabic". Please help..

darkroom - An image proxy with changeable storage backends and image processing engines with focus on speed and resiliency.
darkroom - An image proxy with changeable storage backends and image processing engines with focus on speed and resiliency.

Darkroom - Yet Another Image Proxy Introduction Darkroom combines the storage backend and the image processor and acts as an Image Proxy on your image

Dec 6, 2022
Easily customizable Social image (or Open graph image) generator

fancycard Easily customizable Social image (or Open graph image) generator Built with Go, Gin, GoQuery and Chromedp Build & Run Simply, Clone this rep

Jan 14, 2022
An API which allows you to upload an image and responds with the same image, stripped of EXIF data

strip-metadata This is an API which allows you to upload an image and responds with the same image, stripped of EXIF data. How to run You need to have

Nov 25, 2021
Imgpreview - Tiny image previews for HTML while the original image is loading
Imgpreview - Tiny image previews for HTML while the original image is loading

imgpreview This is a Go program that generates tiny blurry previews for images t

May 22, 2022
Generate image plots of processes' memory usage very quickly, within a single binary.
Generate image plots of processes' memory usage very quickly, within a single binary.

memplot A small utility written in golang to quickly plot memory usage of processes. memplot constantly samples memory usage of a process, for a given

Apr 17, 2021
code2img can generate image of source code
code2img can generate image of source code

code2img code2img can generate image of source code. This was inspired by carbon and silicon Features Doesn't need browser & Internet Copy image of so

Nov 15, 2022
API for generate image to ASCII Art

ASCII API Generate ASCII art from image. You can try this API here: ascii.projec

Jul 1, 2022
:football: Generate your own O'RLY animal book cover to troll your colleagues | 生成你自己的O'RLY动物书封面,让你的同事惊掉下巴

English | 中文 O'RLY Cover Generator O'RLY Cover Generator is a parody book cover generator, implemented in Golang and Vue.js, supporting a wide range o

Dec 24, 2022
📸 Clean your image folder using perceptual hashing and BK-trees using Go!
📸 Clean your image folder using perceptual hashing and BK-trees using Go!

Image Cleaner ?? ?? ➡ ?? This tool can take your image gallery and create a new folder with image-alike-cluster folders. It uses a perceptual image ha

Oct 8, 2022
Image processing algorithms in pure Go
Image processing algorithms in pure Go

bild A collection of parallel image processing algorithms in pure Go. The aim of this project is simplicity in use and development over absolute high

Jan 6, 2023
Go package for fast high-level image processing powered by libvips C library

bimg Small Go package for fast high-level image processing using libvips via C bindings, providing a simple programmatic API. bimg was designed to be

Jan 2, 2023
Image processing library and rendering toolkit for Go.

blend Image processing library and rendering toolkit for Go. (WIP) Installation: This library is compatible with Go1. go get github.com/phrozen/blend

Nov 11, 2022
Decode embedded EXIF meta data from image files.

goexif Provides decoding of basic exif and tiff encoded data. Still in alpha - no guarantees. Suggestions and pull requests are welcome. Functionality

Dec 17, 2022
A lightning fast image processing and resizing library for Go

govips A lightning fast image processing and resizing library for Go This package wraps the core functionality of libvips image processing library by

Jan 8, 2023
Fast, simple, scalable, Docker-ready HTTP microservice for high-level image processing

imaginary Fast HTTP microservice written in Go for high-level image processing backed by bimg and libvips. imaginary can be used as private or public

Jan 3, 2023
Imaging is a simple image processing package for Go
Imaging is a simple image processing package for Go

Imaging Package imaging provides basic image processing functions (resize, rotate, crop, brightness/contrast adjustments, etc.). All the image process

Dec 30, 2022
Pure golang image resizing
Pure golang image resizing

This package is no longer being updated! Please look for alternatives if that bothers you. Resize Image resizing for the Go programming language with

Jan 9, 2023
smartcrop finds good image crops for arbitrary crop sizes
smartcrop finds good image crops for arbitrary crop sizes

smartcrop smartcrop finds good image crops for arbitrary sizes. It is a pure Go implementation, based on Jonas Wagner's smartcrop.js Image: https://ww

Jan 8, 2023
Go package for decoding and encoding TARGA image format

tga tga is a Go package for decoding and encoding TARGA image format. It supports RLE and raw TARGA images with 8/15/16/24/32 bits per pixel, monochro

Sep 26, 2022