Can anyone tell me why I can't use the function imagettftext when I'm trying to create a captcha? It works fine with imagestrings, but the font-size is to small so I have to use imagettftext... Is this a problem with Wamp or what is it? Thanks for your time!
PHP code(so far):
<?php
session_start();
header("Content-Type: image/png");
$text = $_SESSION['secure'];
$font_size = 30;
$image_width = 200;
$image_height = 40;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'arial.ttf', $text);
imagepng($image);
?>
Index.php:
<?php
session_start();
$_SESSION['secure'] = rand(1000,9999);
?>
<img src="createanimage.php" />
From what little info you've given, the code initially didn't work for me, I changed the font path from arial.ttf to ./arial.ttf. It worked!
An exact replica of your code: http://tekinozbek.net/so_imagettftext.php
Beware some spammers use image analysis to recognize text paths and determine the CAPTCHA image! I would certainly suggest you use something like reCAPTCHA for better protection against spam.
Related
I am using a GD library to generate captcha image. But the image is not showing up. Can you please help me with it.
This is a text converted to captcha image
index.php
<?php
session_start();
$_SESSION['secure']=rand(1000,9999);
?>
<img src="generate.php" />
generate.php
<?php
session_start();
header('Content-type: image/jpeg');
$text = $_SESSION['secure'];
$font_size=30;
$image_width=200;
$image_height=40;
$image = imagecreate($image_width,$image_height );
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font.ttf', $text);
imagejpeg($image);
?>
These above are the two files with which I am trying to generate a captcha image. But the image is not showing up. My GD library is enabled, i have had a check on it. Any help would be appreciated.
From documentation
Depending on which version of the GD library PHP is using, when fontfile does not begin with a leading / then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.
So try something like:
$font = "/path_to_folder/font.ttf";
imagettftext($image, $font_size, 0, 15, 30, $text_color, $font, $text);
This work for me.
I need help in my PHP image code. I'm trying to make Captcha, but all I get is broken image icon ?
This is my code:
<?php
header('Content-type: image/jpeg');
$text = "1111";
$font_size = 30;
$image_width = 110;
$image_height = 40;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font.ttf', $text);
imagejpg($image);
?>
"font.ttf" is next to my php file, and names are matching
Use
imagejpeg($image);
Instead of
imagejpg($image);
As imagejpg() doesn't exist.
When debugging you should comment out the header to see the errors.
I want to make an app where user can see their meaning of name. I am done with the php part its just I am stuck how to output it to good looking image. I have used PHP image magician library before. But I am still not able to get it in the format I wanted.
Any help will be useful.
CHECK the image format below
https://fbexternal-a.akamaihd.net/safe_image.php?d=AQDPCj8lZUELWsXD&w=470&h=246&url=http%3A%2F%2Fs3.alegra.me%2Fname%2Fnombre.php%3Fnombre%3Djayanta&cfs=1&upscale
$im = imagecreatefromjpeg('xc7ci4ptuxmgy3nd0brz.jpg');
$font="tahoma.ttf";
$red = imagecolorallocate($im, 255,0,0);
$string="JAYANTA";
$last_string= substr( $string, 1 );
$temp_x = 105;
$down_string[0]="JUST";
$down_string[1]="AMUSING";
$down_string[2]="YOUNG";
$down_string[3]="AMUSING";
$down_string[4]="NICE";
$down_string[5]="TOUGH";
$down_string[6]="AMUSING";
$bbox = imagefttext($im, 80, 0,90, 190, $red, $font, $string);
$temp_y=205; //y cord. of $down_string
for($x=1;$x < count($down_string);$x++){
for($y=0;$y < count($down_string);$y++){
$bbox = imagefttext($im, 12, 0, $temp_x, $temp_y, $red, $font, $down_string[$y][$x]);
$temp_x += 56+$y*1.8;
}
$temp_x = 105;
$temp_y+=15;
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
As you can see, ther is a lot of problems, but the solution is in the imagefttext function.
I have a captcha Code which works perfectly but I really dont know why it displays funny.
Can anyone help me figure out what might be the cause of this? Here is the code:
<?php
session_start();
$text = rand(10000,99999);
$_SESSION["vercode"] = $text;
$height = 25;
$width = 65;
$image_p = imagecreate($width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font_size = 14;
imagestring($image_p, $font_size, 5, 5, $text, $white);
imagejpeg($image_p, null, 80);
?>
This is what is displays:
kX’OýÓx£Ç> ñ=¨µÖ/b{ct÷¯½¬6É-ÀiH¡ä csdòyäæcÆ^"Ö|7¥è¦ss¤i™û%³‘¶>02q–Ú2q;A!p+Ÿ¢€:¿øX>'þÂþÈþÓÿDûöo™öx¾Ñö]Û¼?o›ågø7mÇËŒqF‡ñÄú•Ÿ¦j~M½·öWkx¤šÓÎ]²ù²™!Ü:ìeä“Ô“\¥QEQEÿÙ
You're missing the header. Add the following after session_start():
header('Content-Type: image/jpeg');
The output will be something like:
When you're creating an image like this, you need to tell the browser what type of content you're wanting to display. Hence the reason:
header("Content-type: image/jpeg");
is needed. Read the Wiki article on MIME if this confuses you. You'll also find that you'll need to specify the content type whenever you're generating other file types as well (for example: PDF files generated on the fly with PHP will require header('Content-type: application/pdf'); )
I would like to have the ImageString in this script (that generates a captcha code) return the 5 digit numeric captcha code plus the text "is the code" (to help confuse spambots). Where would I modify this code to include my custom text in the image that is generated?
Thanks for any help on this.
<?php
session_start();
$md5_hash = md5(rand(0,99999));
$security_code = substr($md5_hash, 25, 5);
$enc = md5($security_code);
$_SESSION['captcha'] = $enc;
$width = 165;
$height = 30;
$image = ImageCreate($width, $height);
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$grey = ImageColorAllocate($image, 200, 200, 200);
ImageFill($image, 0, 0, $white);
ImageString($image, 10, 7, 7, $security_code, $black);
header("Content-Type: image/png");
ImagePng($image);
ImageDestroy($image);
?>
Perhaps change
$security_code = substr($md5_hash, 25, 5);
to
$security_code = substr($md5_hash, 25, 5);
$security_code .= " is the code";
That will append your text onto the string, being passed into ImageString(). Try that & see how you go. You might want to increase the image width, as well.
One alternative: if your custom CAPTCHA's weak, you could go with a library -- which might be more secure, and provide you with updates & increased security as new versions are released.
I used SecurImage -- http://www.phpcaptcha.org/