PHP – Converting Strings To Images

One of the most primal needs of displaying strings as an image is during form submission and to make sure that a human being (although there is a finite probability that a monkey can accidentally type the same string correctly) types in the string. This form submission can be to accomplish one of many things – posting comments, placing web orders, displaying email addresses, etc. For my recent, still on-going, attempts to write a personally satisfying photoblog software/application, I need this as part of comment-submission process.


Requirements

It is my practice that I do a full/maximal installation of any linux distribution and that takes care of installing Apache (with all the required modules), PHP, MySQL, ImageMagick, etc. I bet there are tons of documents online that you can refer and install them if you don’t already have them.


SecureImage.php

Let us suppose that all the required code goes into a file called SecureImage.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
 
# Length of security phrase
$phrase_length   = 6;
 
$security_phrase = SecurityPhrase($phrase_length);
 
function SecurityPhrase ($length) {
 
  # Start with a blank security_phrase
  $security_phrase = "";
 
  # Define possible characters
  # Exlude vowels to prevent offensive phrases
  $characters = "0123456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"; 
 
  # A counter to keep track of number of characters 
  $i = 0;
 
  # Add random characters to $security_phrase until $length is reached
  while ($i < $length) { 
    # Pick a random character from the possible ones
    $character = substr($characters, mt_rand(0, strlen($possible)-1), 1);
 
    # Avoid repetition of characters
    if (!strstr($security_phrase, $character)) { 
      $security_phrase .= $character;
      $i++;
    }
  }
 
  # Temporary location to store the image
  # /var/www/html is the default DocumentRoot in Red Hat like distributions
  # The folder, 'tmp' must have read and write permissions for the apache (or the web) user
  $tmp_image_location = "/var/www/html/tmp/";
 
  # Filename of the image (PNG)
  # Security Phrase itself will be the filename
  $tmp_image_filename = $tmp_image_location . $security_phrase . ".png";
 
  # ImageMagick's 'convert' utility and options
  $text2img  = '/usr/bin/convert ';
  $text2img .= ' -background white ';
  $text2img .= ' -fill black ';
  $text2img .= ' -pointsize 30 ';
  $text2img .= ' -gravity West ';
  $text2img .= ' -wave 2x80';
  $text2img .= ' label:'.$security_phrase.' ';
  $text2img .= $tmp_image_filename;
 
  # Run the command
  system($text2img);
 
  # Display the image
  print "<img src=\"/tmp/$security_phrase.png\" border=\"0\">
         <br>";
 
  return $security_phrase;
 
}
 
?>


Using This In A Form

Let us suppose that the form looks something like following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<form method="POST" action="APPROPRIATE URL">
<table border="0" cellpadding="5" cellspacing="3" align="center">
 
<tr>
<td valign="top"><b>Name</b></td>
<td valign="top">
  <input type="text" name="name" size="25">
  <font size="1" color="#000066">
  (Required)
  </font>
</td>
</tr>
 
<tr>
<td valign="top"><b>Email</b></td>
<td valign="top">
  <input type="text" name="email" size="25"> 
  <font size="1" color="#000066">
  (Required; must be valid; will not be shared or made public)
  </font>
</td>
</tr>
 
<tr>
<td valign="top"><b>URL</b></td>
<td valign="top">
  <input type="text" name="url" size="25">
  <font size="1" color="#000066">
  (Optional; please include http://)
  </font>
</td>
</tr>
 
<tr>
<td valign="top"><b>Security Phrase</b></td>
<td valign="top">
  <?php include('SecureImage.php'); ?>
  <input type="text" name="security_phrase" size="25"> 
  <font size="1" color="#000066">
  (Required; enter the characters shown above)
  </font>
</td>
</tr>
 
<tr>
<td valign="top"><b>Comments</b></td>
<td valign="top">
  <textarea name="comments" rows="6" cols="40"></textarea>
</td>
</tr>
 
<tr>
<td class="noborder">
&nbsp;
</td>
<td class="noborder">
  <input type="submit" value="Submit">
  <input type="reset" value="Clear">
</td>
</tr>
 
</table>
</form>

APPROPRIATE URL refers to perhaps a PHP script that will process the information submitted by this form.


Screenshot

SecureImage