View Single Post
  #1 (permalink)  
Old 12-02-2006, 06:17 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 5,306
Blog Entries: 24
Rep Power: 20
John is just really niceJohn is just really niceJohn is just really niceJohn is just really niceJohn is just really nice
Send a message via AIM to John Send a message via MSN to John
Default 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

PHP 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

PHP 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..
Reply With Quote