Connect with Facebook Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Tutorials > PHP Tutorials

PHP Tutorials PHP Tutorials

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-02-2006, 06:17 PM
John's Avatar   
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..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 12-03-2006, 09:50 AM
Jordan's Avatar   
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 18,359
Blog Entries: 90
Rep Power: 20
Jordan is a glorious beacon of lightJordan is a glorious beacon of lightJordan is a glorious beacon of lightJordan is a glorious beacon of lightJordan is a glorious beacon of light
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Default

Thank you, very nice code!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-03-2006, 10:04 AM
TcM's Avatar   
TcM TcM is offline
Code Warrior
 
Join Date: Aug 2006
Posts: 9,814
Blog Entries: 6
Rep Power: 82
TcM is a name known to allTcM is a name known to allTcM is a name known to allTcM is a name known to allTcM is a name known to allTcM is a name known to all
Default

Nice Tutorial ;0 good code
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB 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
Generating Random Numbers with PHP Paradine PHP Tutorials 4 08-27-2007 07:09 PM
Return random numbers without duplicates Paradine PHP Tutorials 0 08-26-2007 02:07 PM
How do I program a random generator? eddiewillers General Programming 4 07-24-2007 02:26 PM
Get and Set Functions Chan C# Programming 3 08-25-2006 11:44 AM
I need to generate a random number in PHP dirkfirst PHP Forum 7 07-05-2006 09:58 PM


All times are GMT -5. The time now is 09:22 PM.

Freelance Jobs

XML/XSL: Need code for Book with Chapers using XML
Create an XML file for a book of your creation, and a basic CSS file that will format it to display ...
Earn: $40.00


C++/C: Simple firework cue sequencer
What I require is a rework of a simple cue sequencer. I have a piece of hardware (an Arduino boar...
Earn: $50.00


HTML/XHTML: Menu Rework - ASCIIBin
I'm placing this in the HTML/XHTML section of the Freelance site but you are not limited to HTML. Wh...
Earn: $20.00



CodeCall Goal

Goal #1: 1,000 Blogs
Goal #2: 1,000 Wiki Pages
Goal #3: 300,000 Posts
Goal #4: 20,000 Threads
Done: 30%, 23%, 55%, 75%

Ads