+ Reply to Thread
Results 1 to 3 of 3

Thread: Code:PHP Random Functions

  1. #1
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    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 03:50 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest
    Thank you, very nice code!

  4. #3
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101
    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. Replies: 2
    Last Post: 11-12-2010, 01:33 PM
  2. Using a random function to generate another random funtion
    By Roger in forum General Programming
    Replies: 11
    Last Post: 08-23-2010, 08:07 AM
  3. Code: Random numbers game
    By LostinVB in forum Visual Basic Programming
    Replies: 1
    Last Post: 12-17-2008, 05:13 AM
  4. MASM 8086 simple 4 functions calculator code help!!
    By monocube in forum General Programming
    Replies: 1
    Last Post: 05-10-2008, 05:16 PM
  5. Replies: 0
    Last Post: 05-14-2007, 05:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

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