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.
Related
I have a PHP source code to draw an image (a dynamic image to draw a captcha).
It was displaying a png file dynamically generated.
But it is not displayed after I moved the php to a free web server that supports PHP 5.x,
instead of my company's old PHP 4.x server.
I had changed .htaccess file to make 'html' file contain 'php' code before.
Do I need to change or add the settings in .htaccess to display 'php' as an image file in the new server?
It does not seem work with just a line:
header('Content-Type: image/png');
in the php file.
I added the php file source code below for your reference:
<?php
session_start();
// Create the image
$im = imagecreatetruecolor(150, 50);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$gray = imagecolorallocate($im, 198, 198, 198);
//$black = imagecolorallocate($im, 0, 0, 0);
$textcol = imagecolorallocate($im, 38, 38, 38);
imagefilledrectangle($im, 0, 0, 399, 129, $gray);
// The text to draw
$text = empty($_SESSION['security_number']) ? 'error' : $_SESSION['security_number'];
$font = 'Georgia.ttf';
// Add some shadow to the text
//imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 30, 0, 10, 35, $textcol, $font, $text);
imagepng($im);
imagedestroy($im);
?>
You have to add header('Content-Type: image/png'); before you generate PNG in PHP to make it display PNG (similar for JPG).
Also, you have to check whether imagepng() successfully generates the PNG before setting header. More importantly, you have to verify whether GD Library is installed or not.
Also, it does not make sense that using .htaccess to make HTML file contains PHP codes; Use PHP extension instead.
I'm trying to use the imagettftext() to add text to an image via PHP. So far I've gone through about 6 different tutorials, trying to get it to work, and I have had no success. I'm currently trying this code from the php documentation page.
<?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);
?>
However, nothing shows up. I have the GD library installed, as well as freetype enabled. I have arial.ttf in the same directory as the php file, and I cannot figure out why it wont work. All I get is a blank image.
EDIT: This is the error message "[19-Jan-2014 16:31:07] PHP Warning: imagettftext(): Could not find/open font in /var/www/php/bb/test.php on line 21"
I was about about to tell you to type the real path of the ttf instead of the relative path.
putenv('GDFONTPATH=' . realpath('.'));
or
$font = '/home/user/arial.ttf';
I'm using the following code in the test.php file to generate an image from a text.
<?php
error_reporting(E_ALL);
// 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 = '/home/axxxxxxx/public_html/font.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);
?>
Then I'm trying to display the image in the test2.php as follows
<?php
echo "<img src=\"/test.php\" />";
?>
All I get is the default broken image icon. The path to the font file and image url is correct. All file permission are at 777. The servers do have the GD library.
What might I be doing wrong?
This is caused by the missing font. Please copy the font file in the test.php directory and change code:
$font = '/home/axxxxxxx/public_html/font.ttf';
to
$font = 'font.ttf';
Hope it helps.
My issue was a wrong offset. The image was showing nothing, no text, no errors in the source code, just a blank file. The paths were correct. I thought there was an error in the ttf font but turns out it was just wrong positioning.
Here's what helped me to see a bit of the text:
imagettftext($im, 20, 0, 20, 20, $fg, $font, $text);
This shows a bit of text on the top right.
Full working code:
putenv('GDFONTPATH=' . dirname(__FILE__));
$font = 'arial'; // located next to the script
imagettftext($im, 20, 0, 20, 20, $fg, $font, $text);
Found the answer. As Danak suggested, I saved the file as UTF-8 Without BOM using the notepad++. Then Is simply started displaying the image correctly.
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.
What is the best way to display underlined text and output the result as image with GD or any other library?
You can try using the Unicode underline combining character U+0332.
<?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);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = "̲U̲d̲e̲r̲l̲i̲n̲e";
// Replace path by your own font path
$font = 'arial.ttf';
// 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);
?>
There are lots of FREE PHP CAPTCHA out there that come with a lot of customization, download one and see what exactly happens behind the scene. Also have a look at this link
HTH
I am using this...
$font = imageloadfont($font_file);
$font_width = ImageFontWidth($font);
$font_height = ImageFontHeight($font);
$str_width = strlen($text)*$font_width;
ImageLine($image, $left, $top+$font_height, $left+$str_width, $top+$font_height, $color);