2 min read

Using Signwriter to Obscure Email Addresses

Using Signwriter to Obscure Email Addresses

Signwriter is an excellent Drupal module that "allows you to use TrueType fonts to replace text in headings, blocks, menus and filtered text. It does this by replacing text with an image generated from a TrueType font file which you provide." One of the neat things you can use this module for is to obscure email addresses by displaying them as an image. However, it does require some special tweaks to accomplish this and make it worthwhile. First, install Signwriter, and also a font file such as arial.ttf. Signwriter says on its settings page that it will look in several places for font files. I placed mine in the theme directory. To use it for this purpose, you need to first create a signwriter profile in your code:

// Set up signwriter profile
$profile->fontfile = 'arial';
$profile->fontsize = 10;
$profile->imagetype = 'png';
$profile->disable_span = true;

You can create a profile with just the fontfile (it will use default settings for any you do not explicitly set), but I have also found if you do not set imagetype, it will create your cached image file without an extension. That last setting is undocumented and very important. Without it, the signwriter theme function will prefix your data with a that contains the text you are converting to an image, in this case, an email address. If you leave it alone, there's no point to doing any of this, because bots will still see the email! So set disable_span=true to remove the tag. Next, call signwriter_theme_text() and pass it the string you want to have displayed as an image, and the signwriter profile:

$email = signwriter_theme_text($string_to_obscure, $profile);

$email now contains the HTML that signwriter produced to display the image. Signwriter turned your text into an image and returned the HTML to display the image -- the tag. But we're not done. Signwriter is coded to set the alt attribute of the image to the text you just encoded. Like the above, if we don't do anything about this, there's no point, bots will still see it. We'll use a regular expression to replace the email with the string "Email Address". You can then print or return it (if you place this code into a function). Updated. See comments below.

// Change the alt tag because it contains the address
$email = preg_replace('/alt="[0-9a-zA-Z!@#$%^&*~-.+_/=?|
'alt="Email Address"',$email);

And that's it! BTW, there are a myriad of different regexes you can find out there for email addresses. But I'm not interested in any sort of validation here -- I just want to replace an email address if it appears. I've probably included more characters than I really had to for our purposes, but according to the RFC these can all appear.

Related Posts

2 min read

Writing ALT Tags for Images

What are ALT Tags? ALT tags or ALT attributes are "alternative text" for an image. ALT tags are used to describe the image or what the image is representing. One of the main purposes of ALT tags is...

7 Marketing Images You Want to Send, but Never Will

What ever happened to the concept of truth in advertising? Is it dead? Did it ever truly exist? After watching a week's worth of marketing Tweets, posts, and shares, I decided that all marketers...
9 min read

Create "Simple" Drupal Modules and Fill the Functionality Gap

How many times as developers have we been working on a project, and ran across an issue that seriously affected the way we were interacting with the website? Maybe it was something related simply to...
1 min read

10 Things We Love About Drupal

It's no secret that our team LOVES using Drupal to build unique solutions for our customers. There are so many reasons to love Drupal; just ask anyone at DrupalCon. Here are the top 10 reasons as to...