Results 1 to 2 of 2
  1. #1
    CwDirectly's Avatar
    Join Date
    Feb 2016
    Gender
    male
    Location
    Arizona, USA
    Posts
    15
    Reputation
    10
    Thanks
    3

    Post PHP Random Text to Image (Captcha Generator)

    So this is both a release and tutorial for the people newer to PHP. Below is the source code and it is very well commented. The function only creates a random string of characters from a source and puts it into an image using a font file as well. This also allows you to expand on and change things around such as colors, characters used, the length of the text etc.

    Like I stated it does not currently have a direct use but makes for a good base to learn a little on and plenty of room to expand on it. This is just one of my many projects I jump between. I do intend on making an admin page to make changes and a demo contact form that will validate the input so it will be more directly and immediately usable. Pretty basic right now.

    (The link to the font file used is included in the comments or you can use any .ttf file. As long as it supports all the characters used.

    Code:
    <?php
    
    function Generate() {
    
        // Set the content-type to display the image properly.
        header('Content-type: image/png');
    
        // Create the image. imagecreatetruecolor(Width, Height);
        $image = imagecreatetruecolor(400, 30);
    
        // Set the colors to be used. These use RGB color codes.
        // View the following link to easliy find RGB color codes:
        // https://www.color-he*****m/
        $white = imagecolorallocate($image, 255, 255, 255); // Used for the background fill, see below. (Default White = 255, 255, 255)
        $text_shadow = imagecolorallocate($image, 128, 128, 128); // Used for the text shadowing, see below. (Default Gray = 128, 128, 128)
        $text_color = imagecolorallocate($image, 0, 0, 0); // Used for the text color, see below. (Default Black = 0, 0, 0)
        
        imagefilledrectangle($image, 0, 0, 399, 29, $white); // Fills the background in white.
    
        // Call for the random text to be used and set the value.
        $text = RandomText();
    
        // You may change this to your own .ttf font file.
        // Thanks and credit to the following website and author for this great default font!
        // https://www.dafon*****m/chaos-times.font
        $font = 'ChaosTimes_lig.ttf';
    
        // Adds gray shadowing to the text, using the $text_shadow color.
        // This also adds slightly more distortion to the image, making it
        // slightly more difficult.
        imagettftext($image, 20, 0, 11, 21, $text_shadow, $font, $text);
    
        // Random text and colors have been set, time to put it all together.
        // This is also where we use $text_color to set the font color.
        imagettftext($image, 20, 0, 10, 20, $text_color, $font, $text);
    
        // Using imagepng() results in clear text compared to imagejpeg().
        // If you wanted to distort the image even more, you could use
        // imagejpeg($im); in place of the following.  Be sure to update
        // header('Content-type: image/png'); TO header('Content-type: image/jpeg');
        imagepng($image);
        imagedestroy($image);
    }
    
    // Randomly generates the text to be shown.
    // The following line is also where to edit the different values,
    // such as the $length which is how long the captcha will be.
    // Also $chars which contains all the things to use in the captcha.
    function RandomText($length = 8, $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") {
        $charl = strlen($chars); // Here we get the length of $chars, used to create random text from it.
        $string = ''; // Make sure the string is empty. Shouldn't normally be required but a good habit.
        
        for ($i=0; $i<$length; $i++) { // If you require some explaining here please view: https://goo .gl/yYjC2j
            $string .= $chars[rand(0, $charl - 1)]; // Set $string to random text using $chars as the text options.
        }
        return $string; // Return the random text generated.
    }
    // Output the finished image
    echo Generate();
    ?>
    Last edited by CwDirectly; 03-12-2016 at 09:37 PM.

  2. #2
    michaelfraz's Avatar
    Join Date
    Mar 2016
    Gender
    male
    Posts
    4
    Reputation
    10
    Thanks
    1
    thanks for sharing

Similar Threads

  1. Text title+Image Title
    By Jov in forum Profile Edit Requests
    Replies: 1
    Last Post: 03-24-2014, 05:54 PM
  2. text and image title
    By AirCanada in forum Profile Edit Requests
    Replies: 3
    Last Post: 12-02-2012, 05:33 PM
  3. Text title + Image Title
    By Janitor in forum Profile Edit Requests
    Replies: 2
    Last Post: 05-05-2012, 03:22 PM
  4. [SOLVED] image code generator ?
    By allie966 in forum CrossFire Help
    Replies: 7
    Last Post: 09-08-2010, 05:53 PM
  5. [Release] xD Captcha Generator
    By Hassan in forum Visual Basic Programming
    Replies: 11
    Last Post: 05-27-2010, 02:22 PM

Tags for this Thread