bottom
wait
Valid XHTML 1.0!
Get Firefox!
Programming tutorials - Random text on image (PHP)
In this tutorial I will show how to create a random text whitch will insert into an image for couldn't be copied or user by certain programs. You surety saw when you register on some web sites that it ask you to enter a text from an image and if the insered text is the same with text from the image then you can access the next step.
For the begining we need two pages: onein which will show the image and one in which we'll create the text and the image. Let say that the text is 10 chars length, then we set variable $length to 10 then we loop with a for sintax from 0 to $length and for each loop we obtain an random number between 65 an 90 using function rand(). With function chr() we transform the obtained numbers from ascii value into letter that correspond with this value. We stick the preceding value with the one obtained on every loop and in the end we obtain an 10 letters string. The ascii values you can find on http://www.asciitable.com. The ascii values between 65 and 90 are represented by upper letters, between 97 and 122 the small letters and between 48 and 57 are numbers.
We repeat the for sintax for every type of character and we stick every obtained string with the preceding. In the end we'll have an string of 30 characters length.
$length = 10;
for($i = 0; $i < $length; $i++){
$str .= chr(rand(65, 90));
}
for($j = 0; $j < $length; $j++){
$str .= chr(rand(97, 122));
}
for($k = 0; $k < $length; $k++){
$str .= chr(rand(48, 57));
}
From the obtained 30 characters length string we take only 10 random characters using function rand() but from this time with the values between 0 and $lenght*3-1 because the first string's character have key 0 not 1. We make this operation with an for sintax looping so many times as $length value. The obtained string will be a value of $str_output variable.
for($l = 0; $l < $length; $l++){
$cut = rand(0, $length*3-1);
$str_output .= $str{$cut};
}
How we make the image? We declare what to result by appealing header() function with "Content-type: image/gif" value for an GIF image, then we create an image with 130px width and 30px height with function imagecreate(). We set a background color and a color for text. The colors could be set with function imagecolorallocate() setting RGB values in integer values from 0 to 255. Next we put the text on image using imagettftext() function and we set the font, angle inclination, padding left, padding right, text color, font, and text.
We can use any font. Just put the font in current directory and modify $font's value.
random_string.php
  1. <?php
  2. $length = 10;
  3. for($i = 0; $i < $length; $i++){
  4. $str .= chr(rand(65, 90));
  5. }
  6. for($j = 0; $j < $length; $j++){
  7. $str .= chr(rand(97, 122));
  8. }
  9. for($k = 0; $k < $length; $k++){
  10. $str .= chr(rand(48, 57));
  11. }
  12. for($l = 0; $l < $length; $l++){
  13. $cut = rand(0, $length*3-1);
  14. $str_output .= $str{$cut};
  15. }
  16. header("Content-type: image/gif");
  17. $im = imagecreate(130, 30);
  18. $background_color = imagecolorallocate($im, 230, 230, 230);
  19. $text_color = imagecolorallocate($im, 0, 0, 0);
  20. $font = 'arial.ttf';
  21. imagettftext($im, 12, 0, 15, 20, $text_color, $font, $str_output);
  22. imagegif($im);
  23. imagedestroy($im);
  24. ?>
Now we show the image created in other page. We set the value of src atribute from img tag the page where we were created the image, this is random_string.php.
image.php
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Image with random text</title>
  6. </head>
  7. <body>
  8. <img src="random_string.php" alt="Random text" width="130" height="30" border="0" />
  9. </body>
  10. </html>
Print this page
bottom