Generating Open Graph Images with SVG partial and Vips

Curated ago by @jeremysmithco

Description

I found a way to generate custom Open Graph images for Sample files on this site using an SVG partial that is rendered and converted to PNG by ImageProcessing::Vips.

1
2
3
4
5
6
7
class SampleFile < ApplicationRecord
  has_one_attached :open_graph_image

  def update_open_graph_image
    self.open_graph_image.attach(io: CreateSampleFileImage.new(self).create, filename: "sample_file_#{id}.png")
  end
end

The #update_open_graph_image is called from a background job to create and attach the OG image.