I'm trying to draw a simple image with GD on PHP 7.2.5 on my Server (Debian 4.9.88-1+deb9u1).
My source code looks simply like that:
<?php
error_reporting(E_ALL);
header("Content-type: image/png");
$string = $_GET['text'];
$im = imagecreatefromjpeg("test.jpg");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
?>
test.jpg is available in the same folder.
PHP-Version: 7.2.5
I can't see anything related in the log files.
GD is activated on the server:
Result looks like that:
Result of the previous shown source code
Problem solved by following this answer:
header('Content-Type: image/png'); not working anymore?
The problem was that my .php file was not in ANSI but in UTF-8, after converting it to ANSI it worked.
Related
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.
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 '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.
I have a Ubuntu server and PHP5, and the PHP script files, and all output are in UTF-8.
I'm trying to send an image to the output stream, but just garbled chinese characters shows up in the output:
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
any suggestions?
Your code works perfectly fine on my machine :
<?php
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
die;
?>
Are you sure you are not outputing anything before or after that code ? Even any kind of whitespace would be a source of troubles.
Or maybe your script is doing something else somewhere ?
If it still doesn't work, maybe trying with imagettftext, to use a "better" / more complete font than the ones used by imagestring might help ?
Using something like this, for instance :
$font = '/usr/share/fonts/truetype/msttcorefonts/arial.ttf';
imagettftext($im, 20, 0, 10, 20, $text_color, $font, 'A Simple éléphant String');
BTW, did you try without those line :
header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
If there is an error/warning/notice, removing those lines might help you seeing those.
And, as a sidenote : using JPEG for images that contain some text generally doesn't give great results, as JPEG is a destructive compression mechanism. Using PNG, in that kind of situation, might get you better results ;-)
Try removing the UTF-8 byte order mark, because it gets prepended to the contents of your JPEG image, rendering it invalid.