Captcha

Hi Guys,

Im trying to write a script that takes variables from mysql database and adds them to an image. The easiest way i could think of doing this is modifying a captcha script I made (with help from here, which works perfectly).

I’m having a problem though. I have included the code below. I have assumed we are already connected to the database.

[php]<?php

function getNames()
{
$result=mysql_query(“select screen_name from login”);

while ($row = mysql_fetch_array($result)) {
echo $row[‘screen_name’];
}
}

function printNames()
{
echo getNames();
}

$string= printNames();

/*
Now for the GD stuff, for ease of use lets create
the image from a background image.
*/

$captcha = imagecreatefrompng("./frankie.png");

/*
write content from mysql to image
*/
imagestring($captcha, $string);

/*
Output the image
*/
header(“Content-type: image/png”);
imagepng($captcha);
?>[/php]

The problem is with my functions. if i change my $string to

[php]<?
$string=“word1, word2, word3”;[/php]

It works perfectly, I just can’t seem to get it to work with variables.

Any help would be appreciated!

Regards,

Sam

In both of your functions, change the “echo” to “return”:
[php]return $row[‘screen_name’];
return getNames();[/php]

This will only do one name at the moment though, you may want to change the first function to this:
[php]$result=mysql_query(“select screen_name from login”);
$names = array();

while ($row = mysql_fetch_array($result)) {
$names[] = $row[‘screen_name’];
}

return implode(’, ',$names);[/php]

seems odd you’ve created a function just to call another function just call names directly, the text string has , in them but the variable don’t you can add a comma to the end if it not the last name in the loop you can find this out my creating an int var $i=0; with a value of 0 that increments on each loop $i++; see the example below:

[php]<?php

function getNames()
{
$i =0;
$result=mysql_query(“select screen_name from login”);
$n - mysql_num_rows($result);

while ($row = mysql_fetch_array($result)) {
	
	if($i == $n){
		echo $row['screen_name'];
	} else {
		echo $row['screen_name'].',';	
	}
	
$i++;}

}

$string= getNames();

/*
Now for the GD stuff, for ease of use lets create
the image from a background image.
*/

$captcha = imagecreatefrompng("./frankie.png");

/*
write content from mysql to image
*/
imagestring($captcha, $string);

/*
Output the image
*/
header(“Content-type: image/png”);
imagepng($captcha);
?>[/php]

Thank you both. Works :slight_smile:

Now for some styling…

How do i control the look of the generated text… can i change the font, colour, size and position?

I take it this can’t be done in css prior to generation of the image?

Regards,

Sam

this should do it:

[php]
$captcha = imagecreatefrompng("./frankie.png");
$font = 14;
$x=190;
$y=242;
$text_color = imagecolorallocate ($captcha, 0, 0,0);//black text
imagestring ($captcha, $font, $x, $y, $string, $text_color);
[/php]

Thank you!

How about the font family? I would really like to use a font that ties in with the rest of the image?

Regards,

Sam

just had a quick look at the manual and it looks like you can change the font by using imageloadfont

http://php.net/manual/en/function.imageloadfont.php

Thank you, I have edited and tested a script and the text works perfectly…

[php]<?php
// Set the content-type
header(‘Content-Type: image/png’);

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
// background colour — imagefilledrectangle($im, 0, 0, 399, 29, $grey);

// The text to draw
$text = ‘Testing…’;
// Replace path by your own font path
$font = ‘Arial.ttf’;

/Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
/

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>[/php]

How do I corporate this with my previous script tho? I still want to use my png file and my function to get the variables from mysql. The only real aspect I want from this script is the font type.

Regards,

Sam

Sponsor our Newsletter | Privacy Policy | Terms of Service