My latest PHP creation

KentonBomb

New Member
Messages
42
Reaction score
0
Points
0
I'm going into hardcore PHP mode now. Since linux has taken my life over, I find web based scripting much more appealing that writing windows programs to use over Wine.

This is my latest PHP Creation. It's a random image generator.

RandomText.php


Refresh to see a new image.

This is the source:
PHP:
<?php
/*
Random Textalizer
*/
$img = imagecreate(220, 20);
$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $black);
$file = "random.txt";
$hnd = fopen($file, "r");
$strings = fread($hnd, filesize($file));
$array = explode(",", $strings);
$ret = count($array);
$num = rand(1, $ret);

$string = $array[$num];    
imagestring($img, 2, 2, 2, $string, $white);    
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);

?>
As you can see, it reads the number of entries in a txt file named "random.txt". It dynamically generates the image based on the number of entries in the text file (Each entry is separated by a comma)

An example random.txt would be:

Code:
Hello!, Bon Jour, Hola!, Konnichiwa!, Ni Hao!
This would randomly say hello in a few different languages (Note:: I don't care if I spelled any of those incorrectly :p)

Feedback?
 

VPmase

New Member
Messages
914
Reaction score
0
Points
0
Not bad for a first. You can end up making Captcha images with GD too.
(Demo) The site isn't done yet but the captcha works perfectly.

Page containing captcha
PHP:
<?
	session_cache_limiter("private_no_expire, must-revalidate");
	session_start();
	$ip = $_SERVER['REMOTE_ADDR'];
	$alphabet = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0');
	srand(time());
	$randstr = "";
	for ($i=0; $i<10; $i++){
		$random = (rand() % sizeof($alphabet));
		$randstr .= $alphabet[$random];
	}
	$_SESSION["capval" . $ip] = $randstr;
?>
<html>
<head>
<title>Captcha</title>
</head>
<body>
<img src="capimg.php" />
</body>
</html>

capimg.php
PHP:
<?
	session_cache_limiter("private_no_expire, must-revalidate");
	session_start();
	$ip = $_SERVER['REMOTE_ADDR'];
	$randstr = $_SESSION["capval" . $ip];
	header ("Content type: image/jpeg");
	$im = @imagecreatetruecolor(100, 25) or die("Cannot Initialize new GD image stream");
	$text_color = imagecolorallocate($im, 255, 255, 255);
	imagestring($im, 2, 10, 5,  $randstr, $text_color);
	imagejpeg($im, '', 100);
	imagedestroy($im);
?>
 
Last edited:

medphoenix

New Member
Messages
354
Reaction score
0
Points
0
Try this....

can you make the background as noisy-one like in captcha images. (instead of pure black - you can use noisy or grain background)...
 
Last edited:

sunils

New Member
Messages
2,266
Reaction score
0
Points
0
Good one. These days members are trying to create dynamic signature........
 

Brandon

Former Senior Account Rep
Community Support
Messages
19,181
Reaction score
28
Points
48
Mines going to be pretty cool when it's done I hope.
 

DefecTalisman

Community Advocate
Community Support
Messages
4,148
Reaction score
5
Points
38
uhm... am I the only one that sees a red cross where that image is supposed to be ?

P.S. Check out my dynamic sig ;p
 

KentonBomb

New Member
Messages
42
Reaction score
0
Points
0
Sorry. I'm using a different host to host that image, and that certain hosts seems to be a small bit suckish and the script timed out.

Looks like it's fine now though.
 

cse07

New Member
Messages
3
Reaction score
0
Points
0
Regarding PHP tutorial

Hey guys i wnat to edit my php based forum...so i want to know how to
write php & what soft u use...Please anyone suugest what to do
 

Dan

Active Member
Messages
1,258
Reaction score
0
Points
36
Looks good!

@cse07
You shouldn't be asking that here...
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
You should also put some lines through the text so the "smarter" bots can't get passed it with the ImageLine() function. Good for you first time. Also, check out the PHP Manual for images.
 
Last edited:
Top