Generate Post With Img for Hugo
Some days ago I find a new post from a blog I follow about “Open Graph Meta Tags on Hugo and WordPress Blogs ” as a toot
New #blogpost
โก๏ธ https://www.burgeonlab.com/blog/hugo-and-wordpress-open-graph-meta-tags/
I often see bloggers on Mastodon with no images for their blog links or sometimes no link card at all (this was me not too long ago).
I've been sorting out Open Graph meta tags for my three blogs (Hugo and WordPress) and I'm happy to say it's all working great now! Read my post to get some tips! ๐
#BurgeonLab
#blog #blogging #blogs #hugo #wordpress #webdev #opengraph
and I find I do the same thing and some more. I generate a default cover because I prefer to have a “standard” generated image or a specific image as the feature image. The image set in the settings is a default, a “if all other image fall use this” kind of stuff.
How I generate the post?
When I write a post I launch a makefile command and it launch two command for me:
- Generate a new post with hugo new
- Generate a cover with my python script
Some code
For this project I use a font (Futura Book font ) and a image where put the title and other thing of the post (for now only the title). This is the image “clean”, without the text.
from PIL import Image, ImageDraw, ImageFont # ๐๏ธ Import modules from PIL
def generate_img(message: str, path: str):
font_path = "Futura Book font.ttf" # ๐๏ธ Font .ttf Path
font_size = 100 # ๐๏ธ Font Size
img = Image.open("cover-blank.jpg") # ๐๏ธ Open Image
dr = ImageDraw.Draw(img) # ๐๏ธ Create New Image
my_font = ImageFont.truetype(font_path, font_size) # ๐๏ธ Initialize Font
text_x = (img.width) // 2
text_y = (img.height) // 2
dr.text((text_x, text_y), message, font=my_font, fill=(255, 255, 255), anchor="mm")
print("Generated content/" + path + "/cover.png")
img.save("content/" + path + "/cover.png")
This script use Python (the library is PIL) and I set the fill parameters for a standard size OpenGraph image.
If you use a python script for generate new post you can implement this code inside of the script or you can add some code to make a command line to launch after your “new post” command.
This post is part of the Hugo Tricks series
- Hugo With Lazy Loading and Webp
- WebP and Avif With Hugo's Static Site Generator
- How I implement Indieweb, Webmention and H Entry in My Blog
- More Stuff I Do With Webmention, Micropub and Brid.gy
- New Render Image For Hugo
- Add Photo Page in your Hugo Site
- Github Action for Syndication Links
- Generate Post With Img for Hugo
Comments
To reply to this post, you can send a Webmention or you can toot me at [email protected]
With an account on the Fediverse or Mastodon, you can respond to this post. Since Mastodon is decentralized, you can use your existing account hosted by another Mastodon server or compatible platform if you don't have an account on this one. Known non-private replies are displayed below.
Learn how this is implemented here.