+ Reply to Thread
Results 1 to 3 of 3

Thread: Code:PHP Random Functions

  1. #1
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,885
    Blog Entries
    25

    Code:PHP Random Functions

    As I mentioned in another post, im working on a little something as an alternative to the generic captcha which looks something like this

    Code:
    <?php
    function RandomStringGenerator($length){
    global 
    $string;
        
    $pattern "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    if(empty(
    $length)){
        
    $length "10";
        }
    for(
    $i=0$i<$length$i++){
        
    $string .= $pattern{rand(0,61)};
        }

    return 
    $string;
    }

    function 
    CaptchaGenerator(){
    global 
    $string;
    RandomStringGenerator();
    header("Content-type: image/png");
    $im = @imagecreate(10050)
       or die(
    "Cannot Initialize new GD image stream");
    $background_color imagecolorallocate($im255255255);
    $text_color imagecolorallocate($im000);
    imagestring($im1055,  $string$text_color);
    imagepng($im);
    imagedestroy($im);
    }

    CaptchaGenerator();
    ?>

    an alternative might be like what color is this shape? or how many sides does this shape have? So I created another random function that creates a random shape a random color at a random size

    Code:
    <?php

    function RandomColorGenerator(){
    global 
    $red$green$blue$color;
        
    $color = array(rand(50,255), rand(50,255), rand(50,255)); //generates random RGB color;
        
    $red $color[1]; //red value;
        
    $green $color[2]; //green value;
        
    $blue $color[3]; //blue value;
    }

    function 
    ShapeGenerator($vertices$radius) {
    global 
    $points;
    $points = array();
    $degrees 360/$vertices//calculates the degrees per angle;


    for ($i=0$i<$vertices$i++) {
        
    $cos cos(deg2rad($degrees*$i)); //generates x points;
        
    $sin sin(deg2rad($degrees*$i)); //generates y points;
        
    $cosTranslated round($cos*$radius)+$radius//translates x points;
        
    $sinTranslated round($sin*$radius)+$radius//transpates y points;
        
    $points[] = $cosTranslated//Adds the x and y values to the array;
        
    $points[] .= $sinTranslated//Adds the x and y values to the array;
        
    }
    }
       
    function 
    RandomShape($sides,$radius) {
    global 
    $points$red$green$blue;
    RandomColorGenerator();
    ShapeGenerator($sides$radius);
        
    $vertices count($points)/2;
        
    $image imagecreatetruecolor(400400); //creates drawing canvass
        
    $bg imagecolorallocate($image000); //sets the background color
        
    $color imagecolorallocate($image$red$green$blue); //sets the polygon color
        
    imagefilledpolygon($image$points$vertices$color); //draws the polygon
        
    header("Content-type: image/png");
        
    imagepng($image);
    }

    RandomShape(rand(3,10), rand(25,200)); //DO IT!!!!!
    //@credits: onion2k - ShapeGenerator
    ?>
    Im actually pretty satisfied how this came out, concidering its one of the few scripts I've made using polymorphic functions.
    Last edited by John; 08-22-2007 at 05:50 PM.

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97
    Thank you, very nice code!

  3. #3
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,461
    Blog Entries
    6
    Nice Tutorial ;0 good code

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Generating Random Numbers with PHP
    By Paradine in forum PHP Tutorials
    Replies: 4
    Last Post: 08-27-2007, 07:09 PM
  2. Return random numbers without duplicates
    By Paradine in forum PHP Tutorials
    Replies: 0
    Last Post: 08-26-2007, 02:07 PM
  3. How do I program a random generator?
    By eddiewillers in forum General Programming
    Replies: 4
    Last Post: 07-24-2007, 02:26 PM
  4. Get and Set Functions
    By Chan in forum C# Programming
    Replies: 3
    Last Post: 08-25-2006, 11:44 AM
  5. I need to generate a random number in PHP
    By dirkfirst in forum PHP Forum
    Replies: 7
    Last Post: 07-05-2006, 09:58 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts