imagettftext() function is not working in PHP - php

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?

Related

How to increase size of the text in imagestring() php without using imagettftext() in PHP

I have searched many questions on internet that show how to increase the size of text in imagestring(). They suggest to use imagettftext() but actually this doesn't work for me. Is there any way that I can increase the text size in imagestring() itself. And if that is not possible help me solve the black screen error I am getting.
Here is the PHP code:
`header('content-type:image/jpeg');
$image = imagecreatefromjpeg("sample.jpg");
$color = imagecolorallocate($image, 225, 0, 0);
imagestring($image, 40, 100, 1000, 'sample', $color);
imagejpeg($image);
imagedestroy($image);
Thanks in advance...
OK! So for some reasons I wasn't able to use imagettftext() So I thought of using imageftttext(). Hopefully that's the right one :D. It works the same as you would expect imagettftext() to work. Cool! Hopefully this helps to the ones those who were stuck.

imagefttext() and imagettftext() giving strange results

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())

Bold text using imagefttext() in PHP

I have the below code and I want to bold the output.
Example:
This code;
$info['users_online']);
Outputs the users online count from my database. I want the number that is being output on the image to be bold.
Full code:
<?php
/db info all here/
$getInfo = mysql_query("SELECT users_online FROM server_status");
$info = mysql_fetch_array($getInfo);
{
$image = imagecreatefrompng("banner.png");
$black = imagecolorallocate($image, 1, 1, 1);
imagefttext($image, 6.5, 1, 293, 68, $black, 'volter.ttf', $info['users_online']);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
}
?>
Don't mind the "/db info all here/" part. I just need the users_online to be bold when it is outputted on the image. Right now, it's not bold.
Thanks.
Use a bold font. For example a bold version of volter.ttf.
You'll want to replace the font file you're currently using (volter.ttf) with one which contains the same font, but in bold weight. If you're unable to do so, you might be able to produce a "pseudo-bold" effect by writing the string several times with a slight X-coordinate offset each time, but frankly at that point it's a lot easier just to change the font to something you can find in bold.

Include PHP code in html error

Am working with an small php project . in creating an text generating image. took some sample codes and tired.
<?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);
?>
i made some changes and it works good . when i tried to insert this php file in other html file using it results broken image. how can i solve this why it happens.
Please make sure that you don't have any whitespace before your <?php statment. In your example above there are 4 spaced before the <?php. Note that this also is important for empty lines.
You can't have the same page being both HTML and an image. The way you do what you want is by having the HTML file contain an IMG tag that references this script.
<img src="yourscript.php">
An HTML file cannot have multiple header sections. Once you have rendered content on the page the call to header() will generate an error.

PHP imagettftext invalid font filename

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

Categories