PDA

View Full Version : GD Align



Verrou
23-12-2008, 10:43 AM
If i wanted to align some text in a GD image to the right so that it expanded to the left like a word doc. Like so,

Normal GD:
Hai

Right align:

Hai




I'm guessing I have to get the width of each character and then the number of characters and then subtract that from the point where I want to align it to? If so can I get some help writing that into a GD document.

Thanks.

Jackboy
23-12-2008, 12:24 PM
http://www.codingtuts.com/coding-tutorials/php/image-manipulation/center-align-text-with-gd

Verrou
23-12-2008, 07:59 PM
Yeah, I've seen that, but thats for center alignment.

To be honest their source doesn't even work:


<?php
// ######################################
// This was written by Michael Morgan of
// CodingTuts (http://www.codingtuts.com)
// ######################################

// Some basic setup
$imageWidth = 300;
$imageHeight = 200;
$textFont = "Cyclo.ttf";
$textSize = 15;
$textString = "CodingTuts.com";

// Set the headers content type
header("Content-type: image/png");

// Create the image
$image = imagecreatetruecolor($imageWidth, $imageHeight);

// Create some colors
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);

// Fill the background of our image
imagefilledrectangle($image, 0, 0, $imageWidth, $imageHeight, $black);

// Get box info
$box = imagettfbbox($textSize, 0, $textFont, $textString);

//Find out the width and height of the text box
$textW = $box[2] - $box[0];
$textH = $box[5] - $box[3];

// Calculate the positions
$positionLeft = ($imageWidth - $textW)/2;
$positionTop = (($imageHeight - $textH)/2);

// Add some text
imagettftext($image, $textSize, 0, $positionLeft, $positionTop, $white, $textFont, $textString);

// Output the image to the browser
imagepng($image);

// Destroy the image
imagedestroy($image);
?>

Verrou
24-12-2008, 01:50 AM
Ok sorry for double post, I can't edit the above. Have figured out how to do the right alignment, thanks to the article provided by Jackboy +rep. Now does anyone know how to make it so that at a certain amount of characters/letters it enters a line space and continues on the next line?

Dentafrice
29-12-2008, 03:29 PM
$string = wordwrap( $string, 15 );


Replace 15 with the length of the string that you would like to break, it will replace it with '\n' and start a new line.

(this will break in the middle of sentences though, so be careful on breaking words)

Want to hide these adverts? Register an account for free!