Jump to content

Captcha code not showing the image in register.php


justin7410

Recommended Posts

Hey guys,

 

im trying to create a 'captcha' image for registration security for my site.

 

currently i have 2 separate pages working together, my captcha.php file & my register.php

 

now, what i am doing is trying to get a generate code that will be random and create an image with this string.

 

this image will then be linked in the registration form where the user will have to match that random string image.

 

sounds simple except , for some reason the code i have supplied from the tutorial is not outputting the image in the form.

 

i have GD's installed on my server, and i have the font file saved also in the directory/

 

my code is :

 

captcha.php

<?session_start();
header('Content-type: image/jpeg');


$text  = $_SESSION['secure'];


$font_size  = 30;


$image_height  = 200;
$image_width  = 40;


$image  = imagecreate($image_width, $image_height);


imagecolorallocate($image, 255, 255, 255); // white


$text_color = imagecolorallocate($image, 0, 0, 0); //black


imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font.ttf', $text);
imagejpeg($image);
?>

so in this i have created the image captcha.php should now be a jpeg..

 

i then go to my register.php

 

declare the $_SESSION and input the src="captcha.php''

 

register.php

 

 

 

<?php 

include('include/init.php');
$_SESSION['secure'] = rand(1000,9999);

?>
and the form :
 
<tr>
    <td class="register_td_left2"><span class="">Security Code:</span></td>
    <td valign="middle" style="padding-left:2px"><input id="security_code" name="security_code" type="text" maxlength="4" size="5"></td>
    <td align="left" valign="middle"><img src="generate.php" alt="Your Security Code"></td>
    </tr>

this is my first time adding a captcha and going through this process. 

 

the only thing i can think that would not be allowing  it output the image, would be the 'font.tff' not properly working, or for some odd reason i dont have the GD functions installed correctly.

 

any suggestions ? or ideas ?

 

 

Link to comment
Share on other sites

Thanks Jessica,

 

i did as you suggested ,

 

so what i have figured out so far is that

 

i was getting a fatal error of : 

 

Fatal error: Call to undefined function imagettftext() in /home/justin/public_html/captcha.php on line 19

 

 

This made me think , hmmm maybe the GD function is not properly installed on my server and is not registering the image.

 

i then went to my phpinfo() to see if GD was enabled

 

gd.info.png

 

as you can see it is installed and enabled.

 

i then am wondering do i need to have FreeType library also installed and enabled ?

 

i cant seem to find that in my phpinfo().

 

but im guessing this is the issue right here.

 

any suggestions 

Link to comment
Share on other sites

You're right. How did you enable GD2? Enabling GD2 enabled FreeType by default for me on both my Windows and Linux boxes.

 

The configuration switch is explained here:

http://us1.php.net/manual/en/image.installation.php

 

Gd was already installed and enabled by default, i never had to do anything, i am assuming that FreeType is not installed or enabled , simply because i can not find it in phpinfo.

 

i dont want to ruin my server , any suggestions as to best install this ? i tried looking into my cpanel x to see if i can find the option:

 

was told to go into the WHM > which i cant seem to find anywhere.

 

 

 

 

 

 

 

Q695
  •  

Posted Today, 03:17 PM

So you see the image, because I found that tricky when I first started out with the captcha?

 

 

 

 

 

 

no the captcha comes as a missing image box on the form

 

 

 

one thing i am wondering is also , i took a random font from my fonts folder. the font is an OTF file not TTF , i then changed the font file name from whatever it was to "font.ttf" and saved this into my web directory. i then refer to this file when i call the function imagettftext() , this should not be a problem right ?

 

i still am very certain due to the fatal error i am getting, the library is not enabled correctly.

Link to comment
Share on other sites

ok if i cant convert the font , does OTF.file  work in the  imagettftext() function ? 

 

my guess is no ? but it seems that most supported fonts are in OTF form.

 

you guys know of any good sites to get free fonts with TFF ext ?

 

regardless of the font file being improper, i still know that it has not even reached that point, due to the fatal error of the  imagettftext() not even being able to be called.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.