IT Community - Software Programming, Web Development and Technical Support

Code:PHP Random Functions

This is a discussion on Code:PHP Random Functions within the PHP Programming forums, part of the Web Development category; As I mentioned in another post, im working on a little something as an alternative to the generic captcha which ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-24-2007, 01:01 AM
pranky pranky is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 51
pranky is on a distinguished road
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

Code:
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(100, 50)
   or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 10, 5, 5,  $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 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(400, 400); //creates drawing canvass
    $bg = imagecolorallocate($image, 0, 0, 0); //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!!!!!

?>
Im actually pretty satisfied how this came out, concidering its one of the few scripts I've made using polymorphic functions.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to create random number in c? Murali C and C++ Programming 6 07-23-2008 12:26 AM
To get random number in mysql Murali Database Support 7 11-05-2007 11:34 PM
How do I generate a random number from php? itbarota PHP Programming 2 09-25-2007 08:57 AM
How do I generate a random number from php? kingmaker PHP Programming 2 07-24-2007 02:56 AM
How to set Random image selector in asp.net ? kingmaker ASP and ASP.NET Programming 1 07-23-2007 06:31 AM


All times are GMT -7. The time now is 04:51 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0