PHP GD - Align text center-horizontally and decrease font size to keep it inside the image
Solution 1
You need the width of the image and the width of the text to relate both.
// get image dimensions
list($img_width, $img_height,,) = getimagesize("fabian.jpg");
// find font-size for $txt_width = 80% of $img_width...
$font_size = 1;
$txt_max_width = intval(0.8 * $img_width);
do {
$font_size++;
$p = imagettfbbox($font_size, 0, $font_path, $text);
$txt_width = $p[2] - $p[0];
// $txt_height=$p[1]-$p[7]; // just in case you need it
} while ($txt_width <= $txt_max_width);
// now center the text
$y = $img_height * 0.9; // baseline of text at 90% of $img_height
$x = ($img_width - $txt_width) / 2;
imagettftext($jpg_image, $font_size, 0, $x, $y, $white, $font_path, $text);
Solution 2
You can use stil/gd-text class to align the text. Disclaimer: I am the author.
<?php
use GDText\Box;
use GDText\Color;
$jpg_image = imagecreatefromjpeg('fabian.jpg');
$textbox = new Box($jpg_image);
$textbox->setFontSize(75);
$textbox->setFontFace('fabian.TTF');
$textbox->setFontColor(new Color(255, 255, 255));
$textbox->setBox(
50, // distance from left edge
50, // distance from top edge
200, // textbox width
100 // textbox height
);
// text will be aligned inside textbox to center horizontally and to top vertically
$textbox->setTextAlign('center', 'top');
$textbox->draw(strtoupper($nombre));
However it is not a complete answer, because it can't decrease font size automatically.
i'llbdevsomeday
Updated on June 13, 2022Comments
-
i'llbdevsomeday almost 2 years
Hope you are doing great.
I’m still a newbie with php so after making some reading and while checking some posts here I was able to put some text over an image with the imagecreatefrompng() function using the PHP GD, users will come to a form and they will be able to enter their name and the name will be written over the image , unfortunately I have been unable to align the text center horizontally, I tried all ways possible (my ways obviously and must be wrong) with imagettfbbox but I failed in all my attempts, could you please guys help me out a little bit to align the string center horizontally? Also since I’m using a kind of alternative big font I need that the size decrease if the name entered is kind of long so this way it will not surpass the image limits and will stay at the center. I’m getting the value of the text from a form as you may check at the beginning of my code:
<?php $nombre=$_POST['nombre']; //Set the Content Type header('Content-type: image/jpeg'); // Create Image From Existing File $jpg_image = imagecreatefromjpeg('fabian.jpg'); // Allocate A Color For The Text $white = imagecolorallocate($jpg_image, 255, 255, 255); // Set Path to Font File $font_path = 'fabian.TTF'; // Set Text to Be Printed On Image , I set it to uppercase $text =strtoupper($nombre); // Print Text On Image imagettftext($jpg_image, 75, 0, 50, 400, $white, $font_path, $text); // Send Image to Browser imagepng($jpg_image); // Clear Memory imagedestroy($jpg_image); ?>
Your help will be highly appreciated, later on I will break my head trying to save the image by clicking a submit button since I do not want the users to save the image by right clicking on it.
Thanks pals!
-
i'llbdevsomeday about 11 yearsGreat michi!!!, it worked perfectly thank you very much!!! You cannot imagine how many times I tried to achieve that, you are a genius pal I really appreciate it. Will analize and memorize every line of code and keep it in mind for future reference. Thanks!
-
JayKandari over 8 yearsThis works like a charm for One Line Text. This won't serve purpose for Multiple lines of text. Thanks
-
michi over 8 years@JayKandari thanks for the compliment, as a note the OP's objective was to center a person's name on their photo, which is a one liner ... at least with most names :-)