I have the following code for my captcha. The lines in the image are generated but the 4 digit number is not displayed. I assume there is some problem in
imagettftext($image,$font_size,0,15,30,$text_color,'font.TTF',$text);
function of code. I tried setting the path to the font file using $_server['DOCUMENT_ROOT']; yet didn't work.
here's the code:
<?php
session_start();
header('Content-type: image/jpeg');
$text= $_SESSION['secure'];
$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);
for($x=1; $x<=30; $x++){
$x1 = rand(1,100);
$x2 = rand(1,100);
$y1 = rand(1,100);
$y2 = rand(1,100);
imageline($image, $x1, $x2, $y1, $y2, $text_color);
}
imagettftext($image,$font_size,0,15,30,$text_color,'font.TTF',$text);
imagejpeg($image);
?>
Below code Perfectly working in my pc
Remember font path should be correct.
output-
<?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);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// 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);
Related
This is generate.php code:
<?php
ob_start();
session_start();
header('content-type:image/jpeg');
$text=$_SESSION['secure'];
$font_size=30;
$width=100;
$height=40;
$image=imagecreate($width, $height);
imagecolorallocate($image, 0, 0, 0);
$text_color=imagecolorallocate($image, 255, 255, 255);
$font='Consolas.ttf';
for($x=1; $x <= 30; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $text);
}
imagettftext($image, $font_size, 0, 15, 30, $text_color, $font, $text);
$imgSrc="out.png";
imagepng($image, $imgSrc);
?>
This is HTML code to display an image, here out.png is successfully generated and loaded into the page but numbers cannot be found.
<div class="form-group mx-auto text-center">
<input type="hidden" name="cap_hidden" value="<?php echo $_SESSION['secure']; ?>">
<img src="out.png">
</div>
I have found one similar question but it didnt help me, thats why I have posted it here.
The problem is the image is generated with lines only but no numbers are visible.
Looks like the font didn't load correctly.
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. PHP: imagettftext - Manual
I suppose there is Text on the Image but it is white.
Perhaps you could change the Colors of the Text.
http://php.net/manual/de/function.imagecolorallocate.php
These parameters are integers between 0 and 255 or hexadecimals
between 0x00 and 0xFF.
0 would be Black and 255 would be White.
You might want to look at the Example at
http://php.net/manual/en/function.imagettftext.php
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// 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);
You also might have a look at your Script at:
$image=imagecreate($width, $height);
imagecolorallocate($image, 0, 0, 0);
$text_color=imagecolorallocate($image, 255, 255, 255);
The Result of imagecolorallocate($image, 0, 0, 0); isnt assigned anywhere.
this is my code:
header('Content-Type: image/jpeg');
$image_width = 400;
$image_height = 30;
$im = imagecreatetruecolor($image_width, $image_height);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$text = utf8_encode("test 123 abc");
$font = 'myfont.ttf';
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagejpeg($im);
imagedestroy($im);
can anyone help me find where i am wrong.
I used the following code to create Captcha to my form. Captcha creating well. Now I want to change font-size, and Font character spaces. I don't know how to change in the following code.
<?php
session_start();
$code= substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 6);
$_SESSION["code"]=$code;
$im = imagecreatetruecolor(150, 35);
$bg = imagecolorallocate($im, 255, 255, 255);
$fg = imagecolorallocate($im, 0, 0, 0);
imagefill($im, 5, 5, $bg);
imagestring($im, 5, 8, 8, $code, $fg);
header("Cache-Control: no-cache, must-revalidate");
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
You can use the code below to implement a simple captcha using gd library of PHP. As you are beginner, here is a sample code for quick testing, it covers font-sizing also:
<?php
session_start();
header('Content-type: image/jpeg');
$text = rand(1000, 9999);
$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);
for ($x=1; $x<=40; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $text_color);
}
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'FREESCPT.ttf', $text);
imagejpeg($image);
?>
In imagettftext function:
imagettftext($image, $font_size, $angle, $x, $y, $text_color, '$font-family', $text);
$image is the $imagecreate function
$font_size is the size of font you want.
$angle is the angle of the fonts tilted
$x and $y are coordinates.
$text_color is the imagecolorallocate function
$font-family is the family of font you want to use
$text is the text or random text to be displayed
Here is the good tutorial on how to build captcha in php -> link
you can change font using imagestring function
I have this code
<?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);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing... a very long text';
// 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);
?>
the problem here is, when I have a very long text, the other won't show.
I've searched that it can be done by
imagettfbbox()
but I don't know how to apply it here.
Any help?
try this code:
<?php
// Set the content-type
header('Content-type: image/png');
$img_width = 400;
$img_height = 30;
// Create the image
$im = imagecreatetruecolor($img_width, $img_height);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, $img_width, $img_height, $white);
// The text to draw
$text = 'Testing... a very long text bla.. ';
// Replace path by your own font path
$font = 'arial.ttf';
$fontSize = $img_width / strlen($text) * 1.8;
// Add some shadow to the text
imagettftext($im, $fontSize, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, $fontSize, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
New update code :
<?php
// Set the content-type
header('Content-type: image/png');
$img_width = 400;
$img_height = 30;
$font = 'arial.ttf';
// Create the image
$text = 'Testing... a very long text.. Testing... a very long text.. Testing... a very long text.. Testing... a very long text..';
$curTextLen = strlen($text);
$limit = 35;
$totalLine = ceil($curTextLen / $limit);
$img_height = $img_height * $totalLine;
$im = imagecreatetruecolor($img_width, $img_height);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, $img_width, $img_height, $white);
for($i = 1; $i <= $totalLine; $i++){
$y = $i * 27;
$textN = substr($text,($limit * ($i-1)),$limit);
imagettftext($im, 20, 0, 11, $y, $grey, $font, $textN);
// Add the text
imagettftext($im, 20, 0, 10, $y, $black, $font, $textN);
}
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
All the examples I've found on the web seem to create pngs with text from an existing png. Is it possible to create a transparent png from scratch and then add text?
The code ive got so far follows (but it doesnt work. just outputs a blank image source)
<?php
$width = 150;
$height = 30;
$text = "My Text";
$fontsize = 5;
$im = imagecreate($width, $height);
$transcolor = imagecolortransparent($im);
imagestring($im, $fontsize, 0, 0, $text, $transcolor);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
<?php
$font = 25;
$string = 'My Text';
$im = #imagecreatetruecolor(strlen($string) * $font / 1.5, $font);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
$lime = imagecolorallocate($im, 204, 255, 51);
imagettftext($im, $font, 0, 0, $font - 3, $lime, "droid_mono.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>
Use imagestring instead of imagettftext if you don't want custom font.
Here's the solution based on your original code.
<?php
$width = 640;
$height = 480;
$text = "My Text";
$fontsize = 5;
$img = imagecreate($width, $height);
// Transparent background
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
// Red text
$red = imagecolorallocate($img, 255, 0, 0);
imagestring($img, $fontsize, 0, 0, $text, $red);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>
I Think GD is one of the most popular for generating images, with imagettftext.
<?php
$text = 'SOME TEXT';
$font="c:/windows/Fonts/Latinwd.ttf"; //Load font file for windows
$im = ImageCreate(700, 140);
$bla = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $bla); //transparent background
$black = imagecolorallocate($im, 255,255,255);
ImageTTFText ($im, 38, 0, 10, 40, $black, $font, $text);
header('Content-Type: image/png');
ImagePNG($im, 'name.png');
imagedestroy($im);
?>