icekat83 Posted December 4, 2010 Share Posted December 4, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/220657-imagecreate-imagettftext-and-font-path-error/ Share on other sites More sharing options...
ignace Posted December 4, 2010 Share Posted December 4, 2010 I have encountered the same problem. Making the path absolute instead of relative solved it. Quote Link to comment https://forums.phpfreaks.com/topic/220657-imagecreate-imagettftext-and-font-path-error/#findComment-1142936 Share on other sites More sharing options...
Zurev Posted December 4, 2010 Share Posted December 4, 2010 As far as font capability goes, if it still gives you issues after ignaces solution, typing in "text" will use GD's default font I believe, which should help you in troubleshooting stages. Quote Link to comment https://forums.phpfreaks.com/topic/220657-imagecreate-imagettftext-and-font-path-error/#findComment-1142950 Share on other sites More sharing options...
icekat83 Posted December 5, 2010 Author Share Posted December 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/220657-imagecreate-imagettftext-and-font-path-error/#findComment-1143105 Share on other sites More sharing options...
.josh Posted December 5, 2010 Share Posted December 5, 2010 read the sticky about header errors in this forum. Quote Link to comment https://forums.phpfreaks.com/topic/220657-imagecreate-imagettftext-and-font-path-error/#findComment-1143120 Share on other sites More sharing options...
PFMaBiSmAd Posted December 5, 2010 Share Posted December 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/220657-imagecreate-imagettftext-and-font-path-error/#findComment-1143205 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.