Jump to content

imagecreate, imagettftext and font path error


icekat83

Recommended Posts

Hi,

 

I am creating an image with a random number to use as a capcha. I've done the image creation before and had it working but now I have a font path error.

 

However......I know my font path is the same...I didn't change it and it was working perfectly. What I changed was I made the image creation piece of code into a function...so I could return a value and use the function as a capcha....can you not use this image creation stuff in a function?

 

The difficulty is getting hold of the random number to verify someone typed it correctly. If you have any ideas I'm open to other methods.

 

My code is:

function capcha(){

$capchatxt = rand(10000,99999);

 

$font = '/includes/balloon.ttf';

$textarray = imagettfbbox(30, 0, $font, $capchatxt);

$width = $textarray[2]-$textarray[0];

$height = $textarray[3]-$textarray[7];

 

 

$image = imagecreate($width+10,$height+10);

$background = imagecolorallocate ($image, 255,227,232);

$colour = imagecolorallocate($image,  0, 154, 239); // blue;

$pink = imagecolorallocate($image,  239, 0, 144); // crimson pink;

$peach = imagecolorallocate($image,  255, 227, 232); // pale pink;

 

// Add the text

imagettftext($image, 30, 0, 5, $height+5, $pink, $font, $capchatxt);

 

header('Content-type: image/png');

imagepng($image);

imagedestroy($image);

return $capchatxt;

}

 

At the moment: The function and the font file are in the same folder. The file which is calling the function is in a different folder.

 

BTW - Forgot to mention that right now I'm using localhost (xampp)...not a real server.

 

Thanks for the help.

 

IceKat

Link to comment
Share on other sites

Hey Guys,

 

Thanks for the help...The font path error is now gone (yea!) but it's been replaced by another issue. I thought the accompanying header error was just a by-product....turns out it's not.

 

I'm using a file with a function (which I posted before). However because the image needs to be displayed after the heading/nav menu etc the code doesn't like it. I can display the image differently of course but that doesn't allow me to hold onto the random number I'm using for the capcha. Can I get the number another way or get around this header issue?

 

I'm not sure if storing the variable number in a session variable will defeat the purpose of having a capcha in the first place. Or are sessions the only other option in this kind of case?

 

Thanks,

IceKat.

Link to comment
Share on other sites

To output an image on a web page you must use an <img src="URL that results in the image being output to the browser" alt=""> HTML tag. See this link for the definition of an <img ...> tag - http://w3schools.com/html/html_images.asp

 

The "URL that results in the image being output to the browser" would be where your function you have posted would be at and where you would call it. You cannot use that function on the HTML page where you want the image to be displayed. That's not how web pages work.

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.