Jump to content

Create an image with a random number when the function is called upon


HoTDaWg

Recommended Posts

hey guys,

im trying to create an image in a function, but the image on its own doesnt even work. I get a feeling it is because i took out the:
[code]
header(Content-Type: image/jpg);//or something like that
[/code]
command. I cant include that command because the script where i call the function to already has header info set up, and when i put that command it only shows the page's url. Here is the code to the function:
[code]
<?php
$text = rand(0,100000);
$pic=ImageCreate(100,40);

$coll=ImageColorAllocate($pic,0,0,0);
$coll2=ImageColorAllocate($pic,255,255,255);

ImageFilledRectangle($pic,0,0,500,30,$coll2);
ImageString($pic,3,5,8,$text,$coll);

ImageGIF($pic);

ImageDestroy($pic);

?>
[/code]
basically I want to a function that creates an image with a random number.
thanks.

HoTDaWg
[code]<?php
ob_start();
header('Content-type: image/png');
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');

$img = array();

//////////
// Image settings...
//////////
# Image width in pixels:
$img['width'] = 65;
# Image height in pixels:
$img['height'] = 30;

# Use TTF?
$img['use_ttf'] = true;
# Link to image true type font (only if above value is true):
$img['ttf_font'] = "verdana";
# TTF pt size?
$img['ttf_size'] = 12;
# Not using TTF? Still need a font size. (1-5)
$img['normal_font_size'] = 4;

# Image handle:
$img['handle'] = imagecreatetruecolor($img['width'],
$img['height']);

//////////
// Colors...
//////////
# Black:
$colors['black'] = imagecolorallocate($img['handle'],
0, 0, 0);
# White:
$colors['white'] = imagecolorallocate($img['handle'],
255, 255,
255);
# Gray:
$colors['gray'] = imagecolorallocate($img['handle'],
(255 / 2), (255 / 2),
(255 / 2));
# Red:
$colors['red'] = imagecolorallocate($img['handle'],
255, 0, 0);
# Transparent:
$colors['transparent'] = imagecolorallocate
($img['handle'], 1, 1, 1);
$colors['transparent'] = imagecolortransparent
($img['handle'], $colors['transparent']);

//////////
// Time to get drawing!
//////////
# Background:
imagefilledrectangle($img['handle'], 0, 0,
$img['width'], $img['width'], $colors['black']);
# Left border:
imageline($img['handle'], 0, 0, 0,
$img['height'], $colors['gray']);
# Top border:
imageline($img['handle'], 0, 0,
$img['width'], 0, $colors['gray']);
# Bottom border:
imageline($img['handle'], 0, $img['height'] - 1,
$img['width'], $img['height'] - 1, $colors['gray']);
# Right border:
imageline($img['handle'], $img['width'] - 1, 0,
$img['width'] - 1, $img['height'] - 1, $colors['gray']);

//////////
// Create the string and draw it.
//////////
$img['random_string'] =
strtoupper(substr(md5(uniqid(rand(),true)),0,6));

if($img['use_ttf'] ==
true && file_exists($img['ttf_font'].".ttf"))
{
$img['random_angle'] = rand(-45/4, 45/4);
imagettftext($img['handle'],$img['ttf_size'],$img['random_angle'],
(($img['width'] / 2) - (imagefontwidth($img['ttf_font'])
* strlen($img['random_string']))) + 2,($img['height'] / 2)
+ 2,$colors['gray'],$img['ttf_font'], $img['random_string']);
imagettftext($img['handle'],$img['ttf_size'],
$img['random_angle'],($img['width'] / 2) -
(imagefontwidth($img['ttf_font'])
* strlen($img['random_string'])),$img['height'] / 2,
$colors['white'],$img['ttf_font'],$img['random_string']);  
} else {
imagestring($img['handle'],$img['normal_font_size'],(
$img['width'] / 2) - $img['normal_font_size']
* strlen($img['random_string']),$img['height'] /
2.5,$img['random_string'],$colors['white']);
}

//////////
// Finish up...
//////////
imagepng($img['handle']);
imagedestroy($img['handle']);
?>[/code]
Just save the code into a new file, and then use:

<img src="filename.php">

Should work.

Note: It produces an alphanumeric string, so you may need to change it to suit your needs.
thanks tandem! The only thing is, and its my fault for not elaborating on my task, but i need to include that kind of an image in a registration form, let the user type in the code, and then check if that code was correct. i want to keep spam bots out of my pretty basic and insecure script.lol. great script though, where did you learn GD?

HoTDaWg

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.