|
Tutorial: Introduction to Images (Registration Img Auth Tut)
Tutorial: Introduction to Images (Registration Img Auth Tut)
Currently viewing this thread: 1 (0 members and 1 guests)
 July 30th, 2003 07:47 AM
http://www.freetype.org/
___________________
http://celerondude.com
 July 30th, 2003 08:05 AM
Neverside Newbie
Status: Offline!
one more question, hopefully you don't mind the off topic nature, but if I do have to download the gd lib. Where would I extract it to?
___________________
Andy
 July 30th, 2003 08:24 AM
the extensions directory, usually \php\extensions
___________________
http://celerondude.com
 July 30th, 2003 09:03 AM
Neverside Newbie
Status: Offline!
thanks a bunch!
___________________
Andy
 July 30th, 2003 09:27 PM
with Mr. Jones
Status: Offline!
Originally posted by CDude
TTFtext requires freetype2. You'll have to use imagestring if you r server doesn't have it installed.
Ill come up with a version that uses imagestring in a few min
___________________
http://www.philbrodeur.com - Expert PHP Development and Tutorials
 July 30th, 2003 10:42 PM
with Mr. Jones
Status: Offline!
OK, this is version 2, more or less.
+ Automatically checks for TTF enableness and adjusts
+ random text position
+ random background, but text is still always ledgible
The circles are GD2; if you dont have it edit out the loop
<?php
define('X_SIZE', 135);
define('Y_SIZE', 60);
define('BORDER_THICKNESS', 5);
define('LEGNTH', 8);
# ttf settings
define('FONT', 'arialbd.ttf');
define('FONTSIZE', 15);
# nonnTTF settings
define('SAFEFONT', 5); # number 1-5
# detect TTF
function_exists('imagettfbbox') ? define('TTF_ENABLED', TRUE) : define('TTF_ENABLED', FALSE);
# if we don't want TTF, we'll need this function
function ImageStringAlign(&$image, $font, $x, $y, $s, $col) {
switch ($x) {
case 0: $x = 2; break;
case 1: $x = floor((ImageSX($image) / 2) - ((ImageFontWidth($font) * strlen($s)) / 2)); break;
case 2: $x = (ImageSX($image) - (ImageFontWidth($font) * strlen($s))) - 2; break;
}
switch ($y) {
case 0: $y = 2; break;
case 1: $y = floor((ImageSY($image) / 2) - (ImageFontHeight($font) / 2)); break;
case 2: $y = (ImageSY($image) - ImageFontHeight($font)) - 2; break;
}
ImageString($image, $font, $x, $y, $s, $col);
}
# let the browser know its getting a image, not a text file or something
header("Content-type: image/png");
# lets make the image
# First Argument is X Axis, Second is Y
$image = imagecreate(X_SIZE,Y_SIZE) or die ('Cannot Initialize new GD image stream');
# now we have to specify the colors we'll use
# the first color is background color (automatically)
$grey = imageColorAllocate($image,170,170,170); # background - its actually going to be our border!
$lightBlue = imageColorAllocate($image,102,153,255);
$black = imageColorAllocate($image,0,0,0);
# int ImageFilledRectangle (int im, int x1, int y1, int x2, int y2, int col)
imageFilledRectangle($image, BORDER_THICKNESS, BORDER_THICKNESS, (X_SIZE-BORDER_THICKNESS), (Y_SIZE-BORDER_THICKNESS), $lightBlue);
# some random circles
for ($i=1;$i<=9;$i++) {
$randomcolor = imagecolorallocate ($image , rand(150,255), rand(150,255),rand(150,255));
imagefilledellipse($image,rand(0,X_SIZE-BORDER_THICKNESS),rand(0,Y_SIZE-BORDER_THICKNESS), rand(20,70),rand(20,70),$randomcolor);
}
# word
$word = substr(md5((time()/rand(1,2147483647))), 0, LEGNTH);
# thats nice, but people get confused with 0 and O and o so0O
$w = str_replace('O', 0, strtoupper($word));
if(TTF_ENABLED) {
$text_bbox = ImageTTFBBox(FONTSIZE, 0, FONT, $w);
$text_pos_x = (X_SIZE - ($text_bbox[2] - $text_bbox[0])) / 2;
$text_pos_y = (Y_SIZE - ($text_bbox[1] - $text_bbox[7])) / 2;
$text_pos_y -= $text_bbox[7];
$text_pos_x += rand(-10,10);
$text_pos_y += rand(-10,10);
# ImageTTFText (int im, int size, int angle, int x, int y, int col, string fontfile, string text)
imageTTFText($image, FONTSIZE, 0, $text_pos_x, $text_pos_y, $black, FONT, $w);
} else {
# dont use TTF
# image handle, font = 1 to 6 or file, $x - see next lines, $y - see next lines, text, color
# $x and $y values
# 0 align left or top
# 1 align center
# 2 align right or bottom
ImageStringAlign($image, SAFEFONT, rand(1,3), rand(1,3), $w, $black);
}
# send the image
imagePNG($image);
# free memory
imageDestroy($image);
?>
___________________
http://www.philbrodeur.com - Expert PHP Development and Tutorials
Last edited by Phil, July 30th, 2003 10:55 PM (Edited 1 times)
 December 31st, 2003 06:32 PM
Nice Tutorial!
___________________
http://lukeblackman.com
 January 8th, 2004 12:46 AM
Neverside Newbie
Status: Offline!
Thanks Phil for this script, it's very usefull!
I need some more help with it so I'm hoping someone can!
I have the script working as it is and would like to use it in a registration script, so i'm guesing you would need to do something like if "code from imput box on form = code from image continue with registration"
To check that the varible $w does infact contain the code on the image I tried to echo it near the end of the script.
<?php
# send the image
imagePNG($image);
echo $w;
# free memory
imageDestroy($image);
?>
but it does not echo, so would someone be able to tell me 1) why wont it echo and 2) am I on the right lines to use it in a registration script.
Many thanks
doo
___________________
Comming soon.....The Lord of The Links
 January 9th, 2004 03:16 PM
Neverside Newbie
Status: Offline!
TTT
Please anyone
___________________
Comming soon.....The Lord of The Links
 January 9th, 2004 10:32 PM
with Mr. Jones
Status: Offline!
remember, you sent an image header.
Heres what to do:
when you create the file, use a normal header
change imgpng to store a file (2nd param, http://us2.php.net/imagepng) in a tmp dir
in the form, put a field "confirm" thats a normal text box
put a hidden field "real_word" which is an md5($w) (so that a robot cant just check the code).
On form proccess page, do
if(md5($_POST['confirm'])===$_POST['real_word']){
thats it! 
___________________
http://www.philbrodeur.com - Expert PHP Development and Tutorials
|