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.
Related
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 useing Joomla 1.5. I have the script for create image with text over It, but It doesn't work for me:
<?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.TFF";
// 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);
?>
I don't understand why this doesn't work. I found many examples in google, I used It, but always the same.
This error in english means: Image "http://juokoera.lt/a.php" can't be shown, because It have problems (errors).
I found in google, that can be fault by my hosting, I changed It, but the same problem. Help me, please if you can. Thank you very much.
UPDATED:
I got the same error when, code looks like:
dasfasdf
dfas
<?php
header('Content-Type: image/png');
$im = imagecreatetruecolor(400, 30);
$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);
$text = 'Testing...';
$font = "ARIAL.TTF";
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im); ?>
How can I use additional text in the same php?
I think your problem is here:
$font = "ARIAL.TFF";
in fact, on your server a file named "ARIAL.TFF" doesn't exist, the font extension you are looking for is TTF, not TFF, and in fact "ARIAL.TTF" on your server exists and i just downloaded it, correct that line to:
$font = "ARIAL.TTF";
After that, you should be able to write text on your image
Hope this helps
UPDATE
After question update, i noticed that an header is sent after printing out some text.
Nothing has to be printed out before the header() function, in order for headers to be correctly sent.
In the PHP manual>header() it is the first thing that is explained:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
I am designing my own PHP MVC
in which any Library file can be used by following steps:
Loading the File $this->registry->load->lib('Image');
Accessing Method from the file $this->registry->Image->anyMethod();
The first line loads the file Image.php located in lib folder and returns an instance
as $this->registry->Image
then using that instance, method anyMethod() from the file can be accessed as$this->registry->Image->anyMethod(); fromController
PROBLEM is that, I am not able to Output any image !
the following code do not work if accessed from Controller but works if directly used !
codes taken from http://in2.php.net/imagettftext
public function anyMethod()
{
// 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);
}
Please Help , I am stuck.
Note: By adding ob_clean(); before imagepng($im); seems working but without text on the Image .
Add an exit; call after destroying the image to prevent any further output being added by your MVC system:
imagepng($im);
imagedestroy($im);
exit;
Also check there are no other Content-type headers being issued before your anyMethod() call.
I've tried to write text in an image just for testing purposes because my Zabbix install is not writing text in the graphs. I've copied the code bellow from the php.net website (http://php.net/manual/en/function.imagettftext.php)
<?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 = '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.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);
?>
Does anyone have an idea?
I've figure out what was the problem here. Permissions to access the truetype file. PHP could not access the file so it couldn't write.
I was not seeing the problem because I was not running with E_ALL. Now everything is running smoothly
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);
It is a GD function and you will need php-gd installed with gettext & ttf support.
-$
I want to create images for my dynamic text.
My problem is that I am reading folder name from some specific directory.
So folder name will be what ever but client wants that folder name should come as the images only.
So I thought that I will place one image with the same name as folder on any unique name.
But client don't want to create new images if they create a new folder.
So how can I do this with PHP?
I need to use some specific background color for image and also want to use some specific font for the text in image.
See imagefttext() in the examples section
From php.net:
http://pl2.php.net/manual/en/function.imagettftext.php
<?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);
?>