PHP Accurate Font Width - php

I am trying to generate a text image.
So I create a new font:
$font = '../fonts/Arial.ttf';
Then I add some background for the text
imagefilledrectangle($image, 0, 0, ?, 12, $green);
And add text
imagettftext($image, 12, 0, 0, 12, $white, $font, $text);
The problem is right now I don't know how to get the width of my font with size 12.
Thanks.

Using ImageTTFBBox()

Related

PHP - imagettftext not working

I am trying to generate images containing data from my database, and I want to use a custom font for it, so I looked up the imagettftext. I am following its syntax, but it's not printing anything. What am I doing wrong?
When I use imagestring it works fine. But that does not use any custom font.
I have placed my font.tff file in the same folder as my script. Here is my code:
header('Content-Type: image/png');
$image = #imagecreate(400, 110);
$backgrund = imagecolorallocate($image, 70, 130, 180);
$textcolor = imagecolorallocate($image, 255, 255, 255);
$font = 'font.ttf';
// TTF
imagettftext($image, 10, 0, 15, 15, $textcolor, $font, 'Test TTF');
// Normal text
imagestring($image, 2, 15, 30, 'Test Normal Font', $textcolor);
imagepng($image, 'images/test.png');
imagedestroy($image);
Am I missing something? The first line is completely blank. I also tried adjusting the font size (it maybe is different than regular imagestring?) but still nothing. The font is a .ttf font file.

How to add text shadow to overlay text using PHP

I want to add an overlay text with text shadow to an image. So far I have been able to achieve this
// top text
imagettftext($im, $font_size+2, 0,
$text1_params['centered_start'],
$font_size+$margin,
$black, $font, $text1_params['text']
);
imagettftext($im, $font_size, 0,
$text1_params['centered_start'],
$font_size+$margin,
$white, $font, $text1_params['text']
);
// bottom text
imagettftext($im, $font_size, 0,
$text2_params['centered_start'] ,
$image_height-$text2_params['height']+$font_size+$margin,
$black, $font, $text2_params['text']
);
imagettftext($im, $font_size, 0,
$text2_params['centered_start']-2,
$image_height-$text2_params['height']+$font_size+$margin-2,
$white, $font, $text2_params['text']
);
This is what the result looks like-
But I want it to look like this(Notice the subtle difference in both the text shadows)-
A hackish way is to render the black text 8 times. First time with an offset to the top left, then top center, then top right, then left, and so on. It may leave an outline that's missing some pixels in a few places but it likely won't be noticeable.

How to make transparent text to jpeg?

Everywhere is talking about adding text on transparent image. I have successfully added text like watermark (so mean transparent text). Can you suggest me how to give opacity 50% ?
Here is code -
$text = "GIF-KING";
$font = "arial.ttf";
$image = imagecreatefromgif('tmpimg/myimage.gif');
$text_color = imagecolorallocate($image, 198, 60, 147);
//imagestring($image,36, 10, 20, $text, $text_color);
imagettftext($image, 30, 0,10, 290, $text_color, $font, $text);
You'd need imagecolorallocatealpha instead of imagecolorallocate,
alpha blending is not available 100%, so take care of error messages.
imagecolorallocatealpha() behaves identically to imagecolorallocate()
with the addition of the transparency parameter alpha.
http://php.net/imagecolorallocatealpha

Adding inner shadow to text

I'm looking for the way to add inner shadow to text using PHP. I'm not looking for a solution, which includes HTML and/or CSS. Image must be generated using only PHP.
For examle: http://i.imgur.com/jYGvM.png
From the text above ('Original') I want to create modify text, so it will look like text at the bottom ('Shadow').
Effect must be achieved using only GD library or Imagemagick, because I can't install new libraries to the server.
One way is to draw your text twice with different colors and a small offset.
Below is the sample code, mainly taken from the php manual, with some modifications to let the shadow appear. The resulting image is here.
Code:
<?php
// Create a 300x100 image
$im = imagecreatetruecolor(300, 100);
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$gray = imagecolorallocate($im, 0x55, 0x55, 0x55);
$gray2 = imagecolorallocate($im, 0xDD, 0xDD, 0xDD);
// Make the background red
imagefilledrectangle($im, 0, 0, 299, 99, $gray2);
// Path to our ttf font file
$font_file = './ariblk.ttf';
// the text without shadow
imagefttext($im, 40, 0, 10, 45, $white, $font_file, 'Original');
// the shadow for "Shadow"
imagefttext($im, 40, 0, 10, 89, $gray, $font_file, 'Shadow');
// and the word itself
imagefttext($im, 40, 0, 10, 90, $white, $font_file, 'Shadow');
// Output image to the browser
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>

error while converting text to image using php

// 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 = 'अन्ग्रेज़ी से हिन्दी';
// Replace path by your own font path
$font = 'mangal.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);
?>
The output text is not same as the input text
Yes, you can use <canvas> for that, see: https://developer.mozilla.org/en/Drawing_text_using_a_canvas
Not directly; javascript isn't supposed to be allowed to poke around in the guts of binary elements like images. You could render the text using a canvas element, or alternatively apply WebGL, but this is probably not what you want. If it absolutely has to be an image, the most portable way to do that is to make a web app that renders the text to an image and echos it back to a client. Using this, you can easily take a blob of text, create an image element that pulls the object back and then display that in the browser.

Categories