<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Christoph Werner</title>
    <description>The latest articles on DEV Community by Christoph Werner (@codepunkt).</description>
    <link>https://dev.to/codepunkt</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F11333%2Fa01b9c50-9ee4-4e75-adc5-237ad9bf65a2.jpg</url>
      <title>DEV Community: Christoph Werner</title>
      <link>https://dev.to/codepunkt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codepunkt"/>
    <language>en</language>
    <item>
      <title>Generating beautiful Open Graph images with Node.js</title>
      <dc:creator>Christoph Werner</dc:creator>
      <pubDate>Fri, 01 Jan 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/codepunkt/generating-beautiful-open-graph-images-with-node-js-2b67</link>
      <guid>https://dev.to/codepunkt/generating-beautiful-open-graph-images-with-node-js-2b67</guid>
      <description>&lt;p&gt;&lt;em&gt;Open graph images provide nice previews when sharing your website. This article explains how to automatically generate open graph images with Node.js and integrate the results into a Gatsby site.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Open Graph?
&lt;/h2&gt;

&lt;p&gt;When you share a link on a social network or chat app, &lt;a href="https://ogp.me/"&gt;Open graph&lt;/a&gt; makes it "unfold" into an image, title, and description. This happens on a lot of platforms like Facebook, Twitter, WhatsApp, Discord, Slack and others.&lt;/p&gt;

&lt;p&gt;Open graph was created by Facebook in 2010 and &lt;strong&gt;standardizes the use of metadata representing the content of a website&lt;/strong&gt;. If your website is shared, you’ll want to present the contents of your page in an optimal way to encourage people to pay a visit.&lt;/p&gt;

&lt;p&gt;If you don't have open graph metadata, the platforms where your website is shared will make a best effort to create a preview of your content. A blog post from this site shared on &lt;a href="https://dev.toTwitter"&gt;https://twitter.com/code_punkt&lt;/a&gt; &lt;strong&gt;without open graph metadata&lt;/strong&gt; looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CI1Z8RGE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/x5btrbfhg8zik9pvdssk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CI1Z8RGE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/x5btrbfhg8zik9pvdssk.png" alt="Twitter share without open graph meta tags"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Unfortunately, your content will not stand out next to other content that is posted with open graph metadata. A link to the same blog post, but this time &lt;strong&gt;with open graph metadata&lt;/strong&gt; could look like this instead:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xGVgeCeg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/k0kelpbu1ahygpipu73j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xGVgeCeg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/k0kelpbu1ahygpipu73j.png" alt="Twitter share with open graph meta tags"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Compared to the previous one, this will catch people's attention.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to create open graph metadata
&lt;/h3&gt;

&lt;p&gt;The open graph metadata is provided as &lt;code&gt;&amp;lt;meta&amp;gt;&lt;/code&gt; tags in your website's &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;. The four required properties for every page are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;og:title&lt;/strong&gt; is the title of your page.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;og:type&lt;/strong&gt; is the type of page. The default is &lt;em&gt;"website"&lt;/em&gt;, but alternatives like &lt;em&gt;"article"&lt;/em&gt;, &lt;em&gt;"profile"&lt;/em&gt; or &lt;em&gt;"video"&lt;/em&gt; are also available.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;og:image&lt;/strong&gt; is an image URL that represents your page.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;og:url&lt;/strong&gt; is the URL of the page.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other tags that can be worth adding are &lt;strong&gt;og:description&lt;/strong&gt;, &lt;strong&gt;og:locale&lt;/strong&gt; and &lt;strong&gt;og:site_name&lt;/strong&gt;. Some platforms have their own extensions with additional tags like &lt;strong&gt;twitter:card&lt;/strong&gt;, others have &lt;a href="https://developers.facebook.com/tools/debug/"&gt;validator tools&lt;/a&gt; that you can use to check how your links would look like when shared.&lt;/p&gt;

&lt;p&gt;Most of the open graph tags are text-based and thus easy to add. The one with the largest impact however, is the &lt;strong&gt;og:image&lt;/strong&gt; tag.&lt;/p&gt;

&lt;h2&gt;
  
  
  Auto-generating open graph images
&lt;/h2&gt;

&lt;p&gt;If you decide to use open graph meta tags for your site and would like to have &lt;strong&gt;images that match the content of specific pages&lt;/strong&gt;, manually creating all of these images would be time consuming and error prone:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You'd have to remember to create a new open graph image for every post that you write and every new page that you add.&lt;/li&gt;
&lt;li&gt;Open graph images would be likely to run out of sync with your content whenever you change something.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To avoid these drawbacks, I was looking for a way to generate open graph images for blog posts and other content that I publish to this &lt;a href="https://www.gatsbyjs.com/"&gt;Gatsby&lt;/a&gt; site. After throwing together a design in Adobe Illustrator, I knew the open graph images for my blog posts should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VFf_ilTz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wbrd05vki3w11hai8xxh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VFf_ilTz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wbrd05vki3w11hai8xxh.jpg" alt="Codepunkt.de open graph image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I derived some requirements from this design. I want to provide a background image that stays the same across all open graph images. I want to be able to add flexible text parts on top, set in a specific font and color to match this website's typography:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The title (large font)&lt;/li&gt;
&lt;li&gt;The date (small font, &lt;em&gt;optional&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Libraries that generate open graph images exist both in a Gatsby-specific context and as generic Node.js implementations. Most of them fall into one of these two buckets:&lt;/p&gt;

&lt;h3&gt;
  
  
  Option 1: Take screenshots in a browser
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://pptr.dev/"&gt;Puppeteer&lt;/a&gt; and &lt;a href="https://playwright.dev/"&gt;Playwright&lt;/a&gt; are great browser automation tools for Node.js. They can be used to &lt;strong&gt;start a browser and open a website&lt;/strong&gt;. If the website is designed to look like the desired open graph image, we can then tell the browser to &lt;strong&gt;take a screenshot&lt;/strong&gt; of it.&lt;/p&gt;

&lt;p&gt;If we want to create multiple open graph images for different posts, we can create a templated website for our open graph image and &lt;strong&gt;fill in each posts metadata&lt;/strong&gt; as we need. From a design perspective, this approach is &lt;em&gt;very&lt;/em&gt; flexible―you can basically design everything that's possible with HTML and CSS. Admittedly, this approach has some downsides:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Performance suffers&lt;/strong&gt;. Spawning and then automating a lot of browser instances is a rather unwieldy approach when you create hundreds of open graph images.&lt;/li&gt;
&lt;li&gt;The browser automation tools &lt;strong&gt;don't run seamlessly in every environment&lt;/strong&gt;. Both your development and your production build pipeline will have to work with the tool of your choice.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Several libraries and services use this approach, most notably &lt;a href="https://www.npmjs.com/package/gatsby-plugin-printer"&gt;gatsby-plugin-printer&lt;/a&gt; and Vercel's &lt;a href="https://github.com/vercel/og-image"&gt;og-image service&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The browsers automated by these packages are native dependencies, which can be hard to set up with a single configuration in a variety of environments. Because I want to be able to generate my open graph images on macOS, Windows and Linux, I decided to not pursue this direction any further.&lt;/p&gt;

&lt;h3&gt;
  
  
  Option 2: Use an image processing library
&lt;/h3&gt;

&lt;p&gt;We can use Node.js image processing libraries that support typography to create the open graph images for us. &lt;a href="https://www.npmjs.com/package/jimp"&gt;Jimp&lt;/a&gt; is written entirely in JavaScript &lt;strong&gt;without any native dependencies&lt;/strong&gt;. However, it only supports bitmap fonts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A4bRCpLj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1x3bxa95pdptp6b0dk41.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A4bRCpLj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1x3bxa95pdptp6b0dk41.png" alt="TrueType vs bitmap font"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is problematic, because bitmap fonts can’t be styled or scaled without quality loss―a font file is needed for every combination of font size, color and weight. &lt;a href="https://www.npmjs.com/package/gatsby-remark-social-cards"&gt;gatsby-remark-social-cards&lt;/a&gt; is one of the libraries that uses Jimp to create open graph images.&lt;/p&gt;

&lt;p&gt;Other image processing libraries require native dependencies or use native Node.js addon modules. On installation, these native addon modules are either compiled or pre-compiled binaries for the target platform are downloaded. They can be as hard to set up in a variety of environments as native dependencies are, so all libraries using one of these approaches are out of the question.&lt;/p&gt;

&lt;p&gt;These include &lt;a href="https://www.npmjs.com/package/gm"&gt;gm&lt;/a&gt;, which is an API wrapper that requires one of the native dependencies &lt;code&gt;imagemagick&lt;/code&gt; or &lt;code&gt;graphicsmagick&lt;/code&gt; to be installed and &lt;a href="https://www.npmjs.com/package/canvas"&gt;node-canvas&lt;/a&gt;, a cairo-backed Canvas implementation for Node.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing my own solution
&lt;/h2&gt;

&lt;p&gt;I couldn’t find an existing solution that met my requirements, so I planned on building my own solution.&lt;/p&gt;

&lt;p&gt;Then I came across Alessia Bellisario's article &lt;a href="https://aless.co/gatsby-wasm-plugin/"&gt;"Building a Gatsby Plugin with Wasm"&lt;/a&gt; and was intrigued: She ran into the same problems and also implemented her own solution, &lt;a href="https://www.npmjs.com/package/gatsby-remark-twitter-cards"&gt;gatsby-remark-twitter-cards&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It allows us to render two texts on top of a background image, one at the top, the other one at the bottom. We can configure TrueType fonts as well as custom colors and font-sizes for both of them.&lt;/p&gt;

&lt;p&gt;Awesome! 💜&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nSemSsFD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hjfgcnp3r6hart8rznui.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nSemSsFD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hjfgcnp3r6hart8rznui.png" alt="Codepunkt open graph image outline"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, to be able to generate open graph images based on my design, this is not enough.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We need both texts at the bottom&lt;/li&gt;
&lt;li&gt;The first text needs to grow upwards when it spans multiple lines&lt;/li&gt;
&lt;li&gt;Both texts are not allowed to grow to the width of the full image&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The text rendering and layout in &lt;code&gt;gatsby-remark-twitter-cards&lt;/code&gt; is done by &lt;a href="https://www.npmjs.com/package/wasm-twitter-card"&gt;wasm-twitter-card&lt;/a&gt;. This package is written in &lt;a href="https://www.rust-lang.org/"&gt;Rust&lt;/a&gt;, compiled to &lt;a href="https://webassembly.org/"&gt;Wasm&lt;/a&gt; and published as an NPM package. It uses a &lt;em&gt;crate&lt;/em&gt; (which, in the Rust world, is another name for "package") named &lt;code&gt;glyph_brush_layout&lt;/code&gt; to do the actual text rendering.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compiling Rust to WebAssembly
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;wasm-twitter-card&lt;/code&gt; has rather inflexible API. As the underlying crate can layout text inside a bounding box both horizontally and vertically, I decided to implement my own Wasm package on top of it.&lt;/p&gt;

&lt;p&gt;To do so, I had to learn a tiny bit of Rust and the basics of both &lt;a href="https://crates.io/crates/wasm-pack"&gt;wasm-pack&lt;/a&gt; and &lt;a href="https://crates.io/crates/wasm-bindgen"&gt;wasm-bindgen&lt;/a&gt;, which are at the core of compiling Rust to Wasm.&lt;/p&gt;

&lt;p&gt;In order to create a more flexible API, there would've been a total of 12 parameters. Rust neither supports default argument values nor overloading method signatures with a different number of parameters, so I decided to implement a few structs to &lt;em&gt;group&lt;/em&gt; related parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[wasm_bindgen]&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Dimension&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;#[wasm_bindgen]&lt;/span&gt;
&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Dimension&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// function being bound should translate to&lt;/span&gt;
    &lt;span class="c"&gt;// calling the "new" operator in JavaScript&lt;/span&gt;
    &lt;span class="nd"&gt;#[wasm_bindgen(constructor)]&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Dimension&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Dimension&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using these structs as parameters, the &lt;code&gt;render&lt;/code&gt; method is called with 5 arguments, which is a lot clearer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[wasm_bindgen]&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Dimension&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;bounds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Dimension&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Position&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;alignment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Alignment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I published the resulting package as &lt;a href="https://www.npmjs.com/package/@codepunkt/wasm-layout-text"&gt;@codepunkt/wasm-layout-text&lt;/a&gt;. It offers a &lt;code&gt;render&lt;/code&gt; method that you can use to get rendered text as an &lt;code&gt;UInt8Array&lt;/code&gt; of pixels and supports:&lt;/p&gt;

&lt;p&gt;✓ TrueType fonts with configurable size and color&lt;br&gt;
✓ Text positioning and a customizable text bounding box&lt;br&gt;
✓ Any combination of vertical and horizontal alignment&lt;br&gt;
✓ Custom image dimensions&lt;/p&gt;
&lt;h3&gt;
  
  
  Using &lt;code&gt;@codepunkt/wasm-layout-text&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;In order to use the package, call the &lt;code&gt;render&lt;/code&gt; method and create new configuration objects for text, image dimensions, text bounding box, text position and text alignment with the &lt;code&gt;new&lt;/code&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;readFileSync&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;join&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wlt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@codepunkt/wasm-layout-text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;wlt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;RgbColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;91&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;214&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;font&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myfont.ttf&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;wlt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;wlt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello world&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;font&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;wlt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Dimension&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;630&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="c1"&gt;// image dimension&lt;/span&gt;
  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;wlt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Dimensions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;530&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="c1"&gt;// text bounds&lt;/span&gt;
  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;wlt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Position&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;wlt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Alignment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;wlt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;HorizontalAlign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;wlt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;VerticalAlign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Top&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are unsure where to get TrueType fonts, take a look at the &lt;a href="https://github.com/google/fonts"&gt;Font file repository&lt;/a&gt; of &lt;a href="https://fonts.google.com/"&gt;Google Fonts&lt;/a&gt;. They have a lot of open source fonts hosted there.&lt;/p&gt;

&lt;p&gt;You can then proceed to write the resulting buffer to a file, for example with &lt;code&gt;jimp&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Jimp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jimp&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Jimp&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;630&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;quality&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;writeAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;`result.png`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using &lt;code&gt;gatsby-remark-opengraph&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;In order to generate open graph images for my &lt;a href="https://dev.to/"&gt;homepage&lt;/a&gt; as well as individual ones for every blog post, &lt;strong&gt;I created a Gatsby plugin&lt;/strong&gt; that uses &lt;code&gt;@codepunkt/wasm-layout-text&lt;/code&gt; to layout texts and &lt;code&gt;jimp&lt;/code&gt; to compose any number of them with a background color or image.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It is called &lt;a href="https://www.npmjs.com/package/gatsby-remark-opengraph"&gt;gatsby-remark-opengraph&lt;/a&gt;&lt;/strong&gt; and is added to the remark plugins array of your &lt;code&gt;gatsby-config.js&lt;/code&gt; to generate open graph images for your markdown content.&lt;/p&gt;

&lt;p&gt;This is a simplified version of what I'm using for this site:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gatsby-remark-opengraph&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/assets/og-image-background.png&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;outputPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./public&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;
    &lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;texts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;frontmatter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#ffffff&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;font&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/assets/Montserrat.ttf&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="na"&gt;verticalAlign&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bottom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
          &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;frontmatter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;published&lt;/span&gt;
          &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toLocaleDateString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;en&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;day&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;numeric&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;month&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;long&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;year&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;numeric&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;}),&lt;/span&gt;
        &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#ffffff&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;fontSize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;font&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/assets/OpenSans.ttf&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="na"&gt;verticalAlign&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bottom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to use the plugin without Gatsby or generate additional open graph images for your Gatsby site, use the &lt;code&gt;createImage&lt;/code&gt; export.&lt;/p&gt;

&lt;p&gt;It takes the same options as the remark plugin, but function options are called with &lt;code&gt;null&lt;/code&gt; instead of a markdownNode so it's a good idea to provide strings for &lt;code&gt;outputPath&lt;/code&gt; and &lt;code&gt;text&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createImage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gatsby-remark-opengraph&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;createImage&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="c1"&gt;// if no outputPath and filename are defined, image is&lt;/span&gt;
  &lt;span class="c1"&gt;// created at ./public/og-image.jpg&lt;/span&gt;
  &lt;span class="na"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/assets/og-image-background.png&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;texts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello world!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#ffffff&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;font&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/assets/Montserrat.ttf&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="na"&gt;verticalAlign&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bottom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code snippet above is a simplified version of the one I use in my &lt;code&gt;gatsby-node.js&lt;/code&gt; to generate a generic open graph image for my homepage. What's left to do is actually inserting the open graph &lt;code&gt;meta&amp;gt;&lt;/code&gt; tags into your site, in Gatsby this is typically done using &lt;code&gt;react-helmet&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Thanks to Alessia for paving the way and sparking my interest at Rust! 🦀&lt;/p&gt;

&lt;p&gt;I implemented a wrapper around an existing Rust crate. This wrapper is compiled to Wasm using &lt;code&gt;wasm-pack&lt;/code&gt; and &lt;code&gt;wasm-bindgen&lt;/code&gt; and has been published as NPM package &lt;a href="https://www.npmjs.com/package/@codepunkt/wasm-layout-text"&gt;@codepunkt/wasm-layout-text&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In order to generate open graph images for this site, I implemented the Gatsby plugin &lt;a href="https://www.npmjs.com/package/gatsby-remark-opengraph"&gt;gatsby-remark-opengraph&lt;/a&gt; that can combine a background image with multiple texts.&lt;/p&gt;

&lt;p&gt;My key takeaways are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rust is a very interesting language with which I've only scratched the tip of the iceberg. It has introduced me to quite a few new concepts.&lt;/li&gt;
&lt;li&gt;If your site is statically generated, it is a good idea to generate open graph images at build time.&lt;/li&gt;
&lt;li&gt;You can use &lt;code&gt;@codepunkt/wasm-layout-text&lt;/code&gt; as a native, dependency-free solution to generate images with TrueType text in Node.js&lt;/li&gt;
&lt;li&gt;If you are working with Gatsby, you can use &lt;code&gt;gatsby-remark-opengraph&lt;/code&gt; to create beautiful open graph images&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I learned a lot and hope you also learned a thing or two! &lt;/p&gt;

&lt;p&gt;If you are interested in more content like this &lt;a href="https://twitter.com/intent/follow?screen_name=code_punkt"&gt;follow me on Twitter&lt;/a&gt; and check out &lt;a href="https://codepunkt.de/"&gt;my blog&lt;/a&gt; where updates will not be delayed. 👍&lt;/p&gt;

&lt;p&gt;Thank you for reading!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>gatsby</category>
      <category>node</category>
      <category>rust</category>
    </item>
  </channel>
</rss>
