I have an issue with getting imagefttext() and imagettftext() working in php.
This is a similar question to imagettftext() not working
however I am not given an error. I will simplify my code here to show what the basis of it is.
I have tried each line below.
No errors:
imagefttext($image2, 20, 0, 0, 20, $white , 'Alien-Encounters-Regular', "testing");
imagefttext($image2, 20, 0, 0, 20, $white , 'Alien-Encounters-Regular.ttf', "testing");
imagefttext($image2, 20, 0, 0, 20, $white , './Alien-Encounters-Regular.ttf', "testing");
imagettftext($image2, 20, 0, 0, 20, $white , 'Alien-Encounters-Regular', "testing");
imagettftext($image2, 20, 0, 0, 20, $white , 'Alien-Encounters-Regular.ttf', "testing");
imagettftext($image2, 20, 0, 0, 20, $white , './Alien-Encounters-Regular.ttf', "testing");
All of the above produce a series of white boxes where there should be a letter. To further troubleshoot I took each line above and uploaded it and tried it. I then changed the "A" in the font file name to "a" and none of them worked telling me this is a Unix based server. (caps matter) Below I'll post a working bit of code that produces an image with text.
imagestring( $image2, 4, 10, 5, "testing" , $white );
I know that FreeType is installed to the server else it would not even give me an image at all throwing an error when I try to use it. I have not tried another method of setting the font file as that is not needed in this case. (It reads the file anyway. Else it would give me an error.)
UPDATE: This is now resolved thanks to Elias, I had a small part of code out of about ~1500 lines of code that was a fragment of old code. This code was imagealphablending($image2, false) and imagettftext() as well as imagefttext() do not work with alphablending off (Set to false.) I removed the code and it now works as expected. See below for how it looks now.
These are some issues which might be relevant:
font file not found
the font file cannot be processed
the blending mode is set incorrect (imagealphablending())
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.
I am using imagettftext() function to put some text on image file but its not working with specific characters. As an ex:'1234567890' is my text but am getting '123(some dummy symbol)567890' on image. I don't know what that dummy symbol is.
Here is my code:
imagettftext($image, 22, 0, 100, 100, $black, '../ttf/Fontspring-DEMO-bwmodelica-regularultracondensed.otf', $phone_number);
Can anyone please help me that what is the issue? Do I need to change .otf file?
I am trying to create an image with text by using imagettftext. It is telling me Warning: imagettftext(): Invalid font filename in C:\xampp\htdocs\recentpost.php on line 32. Here is the code on line 32 I am using to add the text
imagettftext($img, 12, 0, 20, 1, $black, "../fonts/arial.ttf", "News!");
I copied the font right out of the C:/Windows/Fonts folder so it is a valid font.
Try something like:
$font = dirname(__FILE__) . '/arial.ttf';
//OR
$font = dirname(__FILE__) . '/fonts/arial.ttf';
imagettftext($img, 12, 0, 20, 1, $black, $font, "News!");
Hope it helps
Have you tried using
imagettftext($img, 12, 0, 20, 1, $black, "../fonts/arial.TTF", "News!");
instead? (.TTF instead of .ttf)
and #Fab, you can use both \ and / on windows.
Oh wow, stupid me... Its too late for me to be up working on php :p I was trying to get to get to a non existing folder. The real code should be
imagettftext($img, 12, 0, 20, 1, $black, "fonts/arial.ttf", "News!");
Thank you everyone for trying to fix my silly mistake :p
If you are on Windows, you should use "..\fonts\arial.ttf" as path I think
Try to use double slash:
imagettftext($img, 12, 0, 20, 1, $black, "..\\fonts\\arial.ttf", "News!");
I tried using the suggestions above but none worked for me.
So, I will be adding what worked for me in case, someone else was in my shoes.
I used
imagettftext($img, 12, 0, 20, 1, $black, getcwd()."\fonts\\arial.ttf", "News!");
Please note that the extra backslash before 'arial.ttf' is to escape the backslash in the original font file directory.
getcwd() is used for getting the current working directory. You can read more here.
You can use echo getcwd()."\fonts\\arial.ttf" to get the full directory and change all the backslash to forward slash if you wish. It will still work fine
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.