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 &lt; $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 "&lt;img src=\"/tmp/$security_phrase.png\" border=\"0\"&gt;
         &lt;br&gt;";
 
  return $security_phrase;
 
}
 
?-->

Using This In A Form

Let us suppose that the form looks something like following:

Name (Required)
Email (Required; must be valid; will not be shared or made public)
URL (Optional; please include http://)
Security Phrase (Required; enter the characters shown above)
Comments

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

Screenshot

SecureImage

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.