Colorful Source Code Images

You’ve surely seen the colorful, Carbon-generated code snippets all over social media. They can be generated in all sorts of ways, too: online, from the command line, or straight outta your favorite code editor.

Anyhoo, I started looking into having similar images generated automatically, to accompany some of my more technical blog posts, and quickly found out carbon-now-cli wouldn’t work for me.

So I sat down and created Source Snap, a PHP-only code snippet image generator in WordPress plugin form. Based on highlight.php, Dompdf, and Imagick, it turns a couple lines of code—from a custom field, though that may change—into a PNG file, then sets it as the post’s Featured Image.

Of note: ugly code! (Getting Dompdf to comply proved no easy feat.) Also: supports the TinyPNG API for optimal image compression.