Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,896 members, 7,814,027 topics. Date: Wednesday, 01 May 2024 at 01:49 AM

Problem: Centering Text Using Php's Imagettftext - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Problem: Centering Text Using Php's Imagettftext (3748 Views)

How To Write To Or Print Through Thermal Printer Using Php / Does Anyone Know How To Send Emails As Sms To Mobile Phones Using PHP / Writing A Time Based Expiration Script Or Function Using Php And Mysql (2) (3) (4)

(1) (Reply) (Go Down)

Problem: Centering Text Using Php's Imagettftext by DualCore1: 4:23pm On Sep 18, 2013
I need to align texts of varying length on a background image with known (fixed) width and height and have everything converted to an image all in PHP. I am still combing the WWW for a logical approach to achieve this.

Here's my code so far (you can use it as a base)
<?php
function make_text($text, $fontfile, $fontsize, $width)
{
$words = explode(' ', $text);
$lines = array($words[0]);
$currentLine = 0;
for($i = 1; $i < count($words); $i++)
{
$lineSize = imagettfbbox($fontsize, 0, $fontfile, $lines[$currentLine] . ' ' . $words[$i]);
if($lineSize[2] - $lineSize[0] < $width)
{
$lines[$currentLine] .= ' ' . $words[$i];
}
else
{
$currentLine++;
$lines[$currentLine] = $words[$i];
}
}

return implode("\n", $lines);
}




$font_file = 'ambient.ttf';
$image_file = 'image.png';
$font_size = 17 ;

$text = make_text("hey knlkner dafds dsfdsf df dfdf dfd fd fdf dfdfd fdf dfdfd fdf dfd fdfd fd f d f dfffewr er erqewrw fw", $font_file, $font_size, "267" ) ;


$font_color = '#000';
$mime_type = 'image/png' ;
$extension = '.png' ;
$image = imagecreatefrompng($image_file);
$font_color = ImageColorAllocate($image,0,0,0) ;

imagettftext($image, $font_size, 0, 10,20, $font_color, $font_file, $text);

header("Content-type: $mime_type" ) ;
ImagePNG($image) ;
ImageDestroy($image) ;
exit ;
?>


The output of the code above is this image attached. Looking at it I have been able to break the string into different lines already. What's needed is to change the left alignment to center alignment.

Note: You must have a TTF font file (ambient.ttf) and a png image (image.png) all in the same directory as the code.

#PHProcksLOL embarassed

Re: Problem: Centering Text Using Php's Imagettftext by nollyj: 8:11pm On Sep 18, 2013
@Dual Core,
I just tried your code and I was able to produce the result show on the attached image.


I also add this code snippet to get the width and height boundaries of the text.

$width = abs($lineSize[4] - $lineSize[0]); 
$height = abs($lineSize[5] - $lineSize[1]);

$x = $width/2;

$y = $height/2;

//echo $x


I found that the output result of the width boundary of each text varies since there are not of the same length.

49 58.5 52.5 38 30 34.5 25.5 25.5 39 39 39 39 30 34.5 30 16.5 12 12 39.5 44 49 50.5

I think for the text to be centered or align-center, you can also return the width boundary of each text and compute the center positioning of the text with the main image width.

This is my thought. I will give it a try after champions league.

Re: Problem: Centering Text Using Php's Imagettftext by dhtml4: 7:01am On Sep 19, 2013
Keep up the good work dudes!
Re: Problem: Centering Text Using Php's Imagettftext by DualCore1: 9:55am On Sep 19, 2013
Thank nollyj, I was thinking along the line of treating each line of text independently. Still scouring through ideas. The present nature of the script now will suffice for the project while I try out ideas.

dhtml4: Keep up the good work dudes!
You try.
Re: Problem: Centering Text Using Php's Imagettftext by dhtml4: 1:52pm On Sep 19, 2013
Have you guys solved the problem already? I hav a suggestion which might work, but I prefer to code it fast.
Re: Problem: Centering Text Using Php's Imagettftext by nollyj: 2:09pm On Sep 19, 2013
^^^ I think Dual Core said he has found a temporal solution for the time been.
Re: Problem: Centering Text Using Php's Imagettftext by DualCore1: 3:02pm On Sep 19, 2013
dhtml4: Have you guys solved the problem already? I hav a suggestion which might work, but I prefer to code it fast.
What's the suggestion?

I decided to work with the present output pending when I get a better idea or suggestion because I had to move on to complete the next phase of the task which is to filter the background image "instagram style" so that the background has an old school, retro feel which will make the text stand out better. Looking at the output now with the filter done, its good to go.
Re: Problem: Centering Text Using Php's Imagettftext by dhtml4: 7:14pm On Sep 19, 2013
Uhm, why not try to know the width of characters of the text.

I AM A BOY
MY
NAME IS ALLI
MY
FATHER IS MR
GOAT
If we can assume that each character is 1 pixel
then, the first line, I AM A BOY = 11 characters
second line MY is 2 characters
that way, you can apply space to centralize the various lines in your output.
I hope you get the picture or should i be more explicit?

Also, you can calculate the length a given block of text should give you - of course based on the size of the texts you are using.

To get that calculation, you may generate a long text - and measure the length of pixels. . . .i am too tired to explain clearer, just getting back from tedious work

(1) (Reply)

(c# Asp.net)how Do I Know The Number Of Visitors On My Site / Hi, I Am Emmanuel; The Owner Of Tradelify, Ask Me Anything / My Request When Elvis10ten Is Cleared

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 19
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.