Giter Site home page Giter Site logo

tcardgen's Introduction

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 version < 1.16

go get github.com/Ladicle/tcardgen@latest

Go 1.16+

go install github.com/Ladicle/tcardgen@latest

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)

tcardgen's People

Contributors

iberianpig avatar ladicle avatar maco avatar peaceiris avatar sanposhiho avatar superbrothers avatar uh-zz avatar yyh-gl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

tcardgen's Issues

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.

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.

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..

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

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!

Disable some fields : tags, category, …

Hi, thanks for this project.
To suit my needs, I forked it to be able to deactivate certain fields. I believe other people would be happy to benefit from this. I'm not familiar with Go, so here's my commit if you'd like to cherry-pick it or if I should create a pull request in that direction, feel free to let me know !

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.