Pages: [1]   Go Down
Send this topic | Print
Author Topic: How to make a image generator in PHP  (Read 1801 times)
idasfoto
Member
*
Offline Offline

Posts: 36


WWW
« on: December 07, 2008, 01:16:13 AM »

How to make a image generator in PHP
A image generator makes a image with randmly choosen letters/numbers that the visitor have to fill in correctly when you sign up or post something just to make sure he is not some kind of spam-bot. This method is very common on professional pages so this will probably make your site look more professional. It could be kinda hard to understand how it works so this script is for those who are good at PHP, not for them who just copies code and paste it on their page.

generator.php:
Code:
<?php
/*
HOW TO USE:
first of type include("generator.php");
in index.php. When you've done that type
$code=generate(4,"image.jpg");
Now the code will be stored in $code
and the image will be saved as image.jpg.
The output format will always be JPG/JPEG
so don't type anything else. The first
argument is set to 4 wich means that the code
will be 4 letters or numbers long. Here's
an example how to use it:

SESSION_START();
include("generator.php");
if(isset($_POST['code']))
{
if($_POST['code']==$_SESSION['code'])
{
echo "<br>CORRECT!";
} else {
echo "<br>WRONG!";
}
}
if(!isset($_POST['code']))
{
$_SESSION['code']=generate(4,"image.jpg");
echo "<form method='post'>
Type <img src='image.jpg'> here: <input type='text' name='code'>
</form>";
}


*/
//======================== CODE ====================
function generate($arg1,$arg2)
{
// Create the image
$image=imagecreate(16*$arg1,16);

// Set the background color to white
$bg=imagecolorallocate($image,255,255,255);

// Set the output string to ""
$str="";

// Starting a loop that will repeat as many times as you type in argument 1
for($i=0;$i<$arg1;$i++)
{
// Randomly choose a letter (a-z)
$add[0]=chr(rand(97,122));

// Randomly choose a letter (A-Z)
$add[1]=chr(rand(65,90));

// Randomly choose a number (0-9)
$add[2]=rand(0,9);

// Randomly choose between $add[0], $add[1] and $add[2]
$add[3]=$add[rand(0,2)];

// Draw the letter/number on the image
imagestring($image,rand(2,4),$i*16+rand(0,8),rand(0,2),$add[3],imagecolorallocate($image,rand(0,200),rand(0,200),rand(0,200)));

// Adds the randomly choosen letter/number to the output string
$str=$str.$add[3];
}

// Saves the image as what ever you wrote in argument 2
imagejpeg($image,$arg2);

// Return the string
return $str;
}
?>
I've tried to explain as good as I could, just tell me if you wonder something!
« Last Edit: December 07, 2008, 01:23:48 AM by idasfoto » Logged
M0ZZA
A Man In A Trance...
Loyal 110MB Member
*******
Online Online

Posts: 2542


So yeah, I would like to be a moderator.


WWW
« Reply #1 on: December 12, 2008, 01:58:08 AM »

You should explain how to use the $_SESSION that the script generates, to check whether the users input is correct  wink
Logged

avalon-nation-ms
Hyper-Active Member
***
Offline Offline

Posts: 105


WWW
« Reply #2 on: December 13, 2008, 05:46:38 AM »

Thank you, I've been trying to figure out how to do one of these things.
Logged
manicgames
Visual Basic Programmer!
Official 110mb Guru
********
Online Online

Posts: 8573


Ubuntu Is The Shizz.


WWW
« Reply #3 on: December 13, 2008, 12:52:54 PM »

The correct term for this is called a CAPTCHA. It's a spam-bot prevention system.
Logged

DOWNLOAD eoCMS NOW!

Nothing else to really say.
studentluck
Here to help!!!
Hyper-Active Member
***
Offline Offline

Posts: 135

Ask me when you need my help!


WWW
« Reply #4 on: January 08, 2009, 01:07:11 AM »

Thnx! A really good one! But it would be more secure if letters would be rotated.....but that is if your site is very popular and receives a lot of spam.
Logged

1) Add "[RESOLVED]" when your problem is solved
2) Ask for help!! Don't be shy! You are not the only one who does not know something!
3) Thank people when they help you

MOST IMPORTANT : GET YOUR FREE GIFTS HERE: http://freemrrgiveaway.com
manicgames
Visual Basic Programmer!
Official 110mb Guru
********
Online Online

Posts: 8573


Ubuntu Is The Shizz.


WWW
« Reply #5 on: January 08, 2009, 11:32:33 AM »

Actually, it's fine as is. Most bots cannot read images well, but can use plain text well.
Logged

DOWNLOAD eoCMS NOW!

Nothing else to really say.
Zell_ff8
Hyper-Active Member
***
Offline Offline

Posts: 154



WWW
« Reply #6 on: January 12, 2009, 05:14:40 AM »

I actually don't use session anymore, sometimes it gets noisy.
BTM, a static captcha image will do it cuz there's no robot that can read images (not onw worthy of spammer).

I have this captcha in my forms:

Code: ((in the form))
<img alt="Captcha" src="captcha.php?hash=<? $tmp=time();echo $tmp;?>" width="80" height="20" style="border:1px solid #808080" align="middle" />
<input type="text" name="captcha" /></td>
<input type="hidden" name="captcha_ori" value="<? echo $tmp;?>" />

Code: ((in the form action php script))
if (strtoupper($_POST[captcha])!=substr(strtoupper(md5("a random string".$_POST[captcha_ori])), 0,6)) { die("Captcha error");}

Code: (captcha.php)
<?php
$im
=imagecreatefromgif("../img/captcha.gif"); //bg image
$textcolor = imagecolorallocate($im, 255, 0, 0); //code color
imagestring($im, 5, 15, 2,$_GET[hash] ? substr(strtoupper(md5("a random string".$_GET[hash])),0,6) : "ERROR!",  $textcolor);
header('Content-type: image/png');imagepng($im);imagedestroy($im);
exit(
0);
?>

What this does:
 * The form generates a random number -or timestamp in this case xD- ($tmp) and include an image with that number in GET vars
 * The image file (captcha.php, who uses captcha.gif as background) prints 6 numbers/letters bassed on a md5 hash from a string and the random number.
 * The form action scripts checks it. The trick is that both the form action script and the image php script does the same operation to get the code.

This one doesn't use sessions, wich means that if you've made a mistake you can go backwards and try again, but it will change on each reload.
« Last Edit: January 12, 2009, 05:17:18 AM by Zell_ff8 » Logged
bheddir
Active Member
**
Offline Offline

Posts: 55


« Reply #7 on: March 20, 2009, 09:47:55 AM »

hello! im a newbie. Can you gave me exact details on how to create a "form","form.php", and "captcha.php"? coz I have a problem on my site bout captcha. This is the link to my registration page http://www.datezter.com/signup.php  :-)please help!
Logged

Kelly2
Super Authority member
******
Offline Offline

Posts: 1902



« Reply #8 on: March 20, 2009, 10:20:15 AM »

BTW, a static captcha image will do it cuz there's no robot that can read images (not onw worthy of spammer).

There are a lot of bots and spamming companies that have programs to read exactly whats in an image, thats why you see hardcore CAPTCHA images that humans can hardly read.
Logged

mobilego
Member
*
Offline Offline

Posts: 45


I love 110mb


WWW
« Reply #9 on: March 20, 2009, 10:46:20 AM »

Click Here to know how to create image verification during registration.
Logged


Share reviews about web hosts and domain registrars. Visit Hosting Newbies
Kelly2
Super Authority member
******
Offline Offline

Posts: 1902



« Reply #10 on: March 20, 2009, 11:40:58 AM »

Click Here to know how to create image verification during registration.

..or read the first post?
Logged

Pages: [1]   Go Up
Send this topic | Print
Jump to: