Trying to display a font using the GD library. There is indeed an image there, it's just that theres nothing displaying.
PHP:
header('Content-Type: image/png');
$font = $_GET['font'];
// Create the image
$image = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($image, 255, 255, 255);
$grey = imagecolorallocate($image, 128, 128, 128);
$black = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, 399, 29, $white);
// The text to draw
$text = 'The Quick Brown Fox Jumps over the Lazy Dog';
$font = '/Aller/' . $font;
// Add the text
imagettftext($image, 20, 0, 10, 20, $black, $font, $text);
imagepng($image);
HTML:
<img src="fontgen.php?font=Aller_Rg.ttf" alt="" />
The font resides in fonts/Aller/Aller_Rg.tff
What am I doing wrong?
The problem seems to be the $font variable. From the 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.
When using versions of the GD library lower than 2.0.18, a space character, rather than a semicolon, was used as the 'path separator' for different font files. Unintentional use of this feature will result in the warning message: Warning: Could not find/open font. For these affected versions, the only solution is moving the font to a path which does not contain spaces.
In many cases where a font resides in the same directory as the script using it the following trick will alleviate any include problems.
<?php
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));
// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont';
?>
You also said that the font resides in fonts/Aller/ directory. Whereas, in your script, there is no reference to the fonts directory.
The code is all correct except this part
$font = '/Aller/' . $font;
It tries the absolute path '/Aller/Aller_Rg.tff' not 'Aller/Aller_Rg.tff'
Changing it to $font = 'Aller/' . $font; should work.
Also you should check the error log, it should mention Invalid font filename
When in doubt remove header('Content-Type: image/png'); for debugging.
Related
I want to create a logo or profile avatar from first letters of name, like Google. Is there any method or service to do it?
I am tried to learn the code about make image with php but it's to hard. One time I found a website about this dynamic image text but I don't find.
Most easy examples you will find online are going to use PHP's imagecreate and imagestring functions, such as this one:
https://phppot.com/php/how-to-convert-text-to-image-using-php/
Here is a quick example-code I've put together based on the above link, that creates an image similar to the Google avatars:
$img = imagecreate(250, 250);
$textbgcolor = imagecolorallocate($img, 52, 152, 219);
$textcolor = imagecolorallocate($img, 255, 255, 255);
$txt = "AB";
$fontfile = "/arial.ttf";
imagettftext($img, 100, 0, 35, 170, $textcolor , $fontfile, $txt);
ob_start();
imagepng($img);
printf('<img src="data:image/png;base64,%s"/ width="100">', base64_encode(ob_get_clean()));
You will need place the arial.ttf fontfile in the same directory as your PHP file for this to work.
However, in most fonts that are aesthetically pleasing, letters do not have the same width. So you will find it difficult to center the text, since you can't use the same X value for the anagram "MM" and "II". I would advise you to use a library that has extended functions like aligning text to the middle, and my bet would be on gd-text.
How do I get this to load? Right now, it's not showing any image at all... I'm not really that great at creating captcha's because I usually never do this.
captcha.php:
<?php
$string = '';
for ($i = 0; $i < 5; $i++) {
// this numbers refer to numbers of the ascii table (lower case)
$string .= chr(rand(97, 122));
}
$image = imagecreatetruecolor(170, 60);
$color = imagecolorallocate($image, 200, 100, 90); // red
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 30, 0, 10, 40, $color, ASSETPATH."arial.ttf", $string);
header("Content-type: image/png");
imagepng($image);
?>
register.php:
<?php echo '<img src="'.ASSETPATH.'img/captcha.php" />'; ?>
My asset path IS correct because I'm using it elsewhere and it loads perfectly fine. Is the MVC format of this project messing it up somehow??
How about using one of the captcha services like recaptcha?
Judging by your use of this constant ASSETPATH both for you image URI as well as the fontfile path in imagettftext I can only assume you have your fontfile stored in the same path as that of your image?
If this is not the case, or the file could not be opened there, PHP will throw a Warning of imagettftext(): Invalid font filename. If display_errors is set to On in your php.ini (you can check phpinfo to verify this) this means the error message will be sent to the output stream along with your image data (i.e. corrupting the image data and cause your browser not to display the image). This would also prevent the headers from being modify since the error would have occurred before your call to header.
However, if display_errors is not turned on and PHP could not find the fontfile you supplied or it could not be open for any reason (e.g. permissions) the result will be a blank 170x60 PNG image.
If I test your code locally it proves to work as expected, as long as I supply PHP with the correct absolute path to my truetype font file.
For example, I have some truetype fonts on my system stored at /usr/share/fonts/truetype/, which is definitely not in my webroot as I don't normally keep my font files there. Note, also, that my PHP user has sufficient permissions to read from this path.
Now, if I supply PHP with the correct absolute path to the truetype font file that I would like to use I get the following image using your code...
$string = '';
for ($i = 0; $i < 5; $i++) {
// this numbers refer to numbers of the ascii table (lower case)
$string .= chr(rand(97, 122));
}
$image = imagecreatetruecolor(170, 60);
$color = imagecolorallocate($image, 200, 100, 90); // red
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 30, 0, 10, 40, $color, '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-ExtraLight.ttf', $string);
header("Content-type: image/png");
imagepng($image);
What you should try to do in order to debug this further is run the PHP script that generates the image by itself, with display_errors turned on and error_reporting set to -1 and if it is indeed the case that your font file is the problem you will see this in your error_log or in the error output displayed during testing that script with display_errors on.
/*create watermark*/
// Create the image
$im = imagecreate(460, 50);
// Create some colors
$grey = imagecolorallocate($im, 230, 231, 232);
$dark_grey = imagecolorallocate($im, 128, 130, 133);
// The text to draw
$text = "foobar";
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));
$font = 'Tondu_beta';
// Add the text
imagettftext($im, 15, 0, 15, 35, $dark_grey, $font, $text);
$wm_w = imagesx($im); //get width
$wm_h = imagesy($im); //get height
$wmresource = $im; //watermark resource
//imagejpeg($wmresource);
/*end watermark*/
The font file is Tondu_Beta.ttf. The code above worked just fine in my local machine, but it only gave me grey box after uploading to live server. What's wrong here? Thanks ^^
UPDATE: I remember it gave me this error: Could not find/open font bla.bla..bla...
try using
"./Tondu_beta.ttf"
worked for me when both font and php file were in root directory
The error is self-explanatory. Your live server doesn't have the font in question (Tondu_Beta.ttf) installed. Install the font onto your server, or choose a font your server does have.
Straight from the doc:
fontfile
The path to the TrueType font you wish to use.
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.
When using versions of the GD library lower than 2.0.18, a space character, rather than a semicolon, was used as the 'path separator' for different font files. Unintentional use of this feature will result in the warning message: Warning: Could not find/open font. For these affected versions, the only solution is moving the font to a path which does not contain spaces.
In many cases where a font resides in the same directory as the script using it the following trick will alleviate any include problems.
<?php
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));
// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont';
?>
I am using this code to creating images with text written in it with transparent backgrounds.
<?php
// Set the content-type
header('Content-Type: image/gif');
// Create the image
$im = imagecreatetruecolor(400, 150);
// Create some colors
$black = imagecolorallocate($im, 0, 0, 0);
$acolor = imagecolorallocate($im, 153, 204, 153);
imagecolortransparent($im, $black);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($im, 50, 0, 10, 100, $acolor, $font, $text);
// Using imagegif()
imagegif($im,"img.gif");
imagedestroy($im);
?>
But text which is written in img.gif has some unwanted color(Black) on borders of alphabets('e,s,n,g'). How can i finish that color.The generated image is
The arial font download site is http://code.google.com/p/ireader/downloads/detail?name=arial.ttf
The GIF format cannot handle alpha transparency. It can have only 100% transparent (= invisible), or 100% opaque (= visible) pixels.
Your text seems to have anti-aliased, soft edges.
Those edges aren't 100% transparent, but they're also not 100% opaque - they are a mixture of foreground and background. That makes them appear softer.
Because the GIF format can't deal with these nuances, the library uses a mixture of green and black to simulate a "weaker" green.
If you use the PNG format, the problem should go away: PNG supports alpha transparency. There are some issues that you need to look into if you still need to support IE6, but for every other browser, this will work fine straight away.
I 'm trying to add text on a specific image. Its working perfectly but the quality of image is low especially for papyrus.ttf font. How can i improve the quality of text in the image. But i need high quality to print the output.
Here is my code.. its very simple.
header("Content-Type: image/jpeg");
$im = imagecreatefromjpeg("cosmos.jpg");
$black = ImageColorAllocate($im, 0, 0, 0);
Imagettftext($im, 14, 0, 10, 15, $black, 'papyrus.ttf', "Corey and Lisa ");
Imagettftext($im, 14, 0, 10, 35, $black, 'papyrus.ttf', " 1994, june");
Imagejpeg($im, '', 100);
ImageDestroy($im);
Download: http://wneeds.com/gdtest.zip
If imageTTFText fails in Quality, the next step to try is ImageFTText(). It uses the FreeType library to render fonts, which usually means significantly better quality. It needs to be installed and compiled into PHP to work, which it most often is. Try calling the function and you'll see whether that is the case.
If that doesn't do, the next step is using ImageMagick either through the command line, or through the appropriate PHP extension. But try Freetype first, it could already be enough.