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="[[email protected]#$%^&*~-.+_/=?|{}}]+"/','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.

Leave Your Comment