Tech Note: A Graphic Display of All Installed Imagemagick Fonts

All at once
LGF • Views: 67,957

As I’ve mentioned previously, every once in a while I manage to come up with a bit of code that doesn’t seem to exist elsewhere on the web, and if that code is useful I see it as a duty to share it with the world, as a way of saying thanks to all the coders who’ve helped me over the years by sharing similar bits.

Here’s one of those bits. While I was coding the Donald Trump Bumper Sticker Generator, I wanted to find a close match for the font used in the original graphic, and one way to do that is to look at a display of all the installed fonts. Since I was using Imagemagick to create these graphics, I needed to see which fonts were installed in a way that Imagemagick could use.

On a Linux system, when you install Imagemagick it includes some popular fonts like Avant Garde and Helvetica, but I ended up also installing a bunch of fonts I have on my office system, some of which came from an Adobe Font Folio CD I bought many eons ago. And in those fonts I eventually discovered the exact font used for the word “TRUMP” in our bumper sticker graphic: it’s “Akzidenz Grotesk.”

But I couldn’t find a script anywhere on the web that would just dump out a list of all the Imagemagick fonts, so I ended up writing one. (The screenshot at the top of this article shows a small section of the resulting web page.) And that’s the code I’m going to share today.

In order to run this PHP script you’ll need to have the Imagick extension installed; I won’t get into doing this because the installation method varies greatly depending on your server’s OS and the version of PHP you’re using. My advice is to Google “Installing Imagick for PHP” and find a method that suits your system.

The Font Display script is actually quite simple; so simple and short I haven’t included comments in this code. It’s just an HTML page with a small section of embedded PHP code to render the fonts when it’s loaded. The only really tricky part: to display the font samples I’m using an tag with a “data URI,” with the actual image data embedded directly inside the src attribute as a base 64-encoded string.

Also, because displaying all these fonts (I have a lot installed now!) can take a while, I used the set_time_limit() function to increase the amount of time to 300 seconds, so PHP doesn’t decide the script has been running too long and abort.

So without further ado… the code.





Display All Installed Imagick Fonts




set_time_limit(300);
$fonts = \Imagick::queryFonts();
$text = 'Grumpy wizards make toxic brew for the evil Queen and Jack.';
foreach ($fonts as $font) {
	$image = new \Imagick();
	$draw = new \ImagickDraw();
	$draw->setFont($font);
	$draw->setFillColor('black');
	$draw->setFontSize(32);
	$draw->annotation(0, 32, $text);
	$image->newImage(1200, 48, 'none');
	$image->setImageFormat('png');
	$image->drawImage($draw);
	$imgData = base64_encode($image->getImageBlob());
	echo <<
{$font}

FONT; }

Here’s a live demo of the font display script, so you can see it in all its glory. The name at the top left of each font’s display area is the name you would pass to Imagick’s setFont() call.

Jump to top

Create a PageThis is the LGF Pages posting bookmarklet. To use it, drag this button to your browser's bookmark bar, and title it 'LGF Pages' (or whatever you like). Then browse to a site you want to post, select some text on the page to use for a quote, click the bookmarklet, and the Pages posting window will appear with the title, text, and any embedded video or audio files already filled in, ready to go.
Or... you can just click this button to open the Pages posting window right away.
Last updated: 2023-04-04 11:11 am PDT
LGF User's Guide RSS Feeds

Help support Little Green Footballs!

Subscribe now for ad-free access!Register and sign in to a free LGF account before subscribing, and your ad-free access will be automatically enabled.

Donate with
PayPal
Cash.app
Recent PagesClick to refresh
The Pandemic Cost 7 Million Lives, but Talks to Prevent a Repeat Stall In late 2021, as the world reeled from the arrival of the highly contagious omicron variant of the coronavirus, representatives of almost 200 countries met - some online, some in-person in Geneva - hoping to forestall a future worldwide ...
Cheechako
4 days ago
Views: 130 • Comments: 0 • Rating: 1
Texas County at Center of Border Fight Is Overwhelmed by Migrant Deaths EAGLE PASS, Tex. - The undertaker lighted a cigarette and held it between his latex-gloved fingers as he stood over the bloated body bag lying in the bed of his battered pickup truck. The woman had been fished out ...
Cheechako
2 weeks ago
Views: 294 • Comments: 0 • Rating: 1