Jump to content

justify text using imagettftext?


_SAi_

Recommended Posts

You will have to justify one line at a time.

left: easy, set your x coordinate to 1, or 5 or something small.

right: more fun... using imagettfbbox(), calculate the total width of the text, then subtract it from the imagesx() of the image.

center: the most fun... same as "right" but divide the end result by 2.

The example below isn't fully tested, but it should fly.
[code]
<?php
$im= imagecreatetruecolor(500,500);
$coordinates=imagettfbbox(12, 0, 'arial.ttf', 'this is my display text');
$width=$coordinates[0]-$coordinates[2];
if($width<0) $width=width*(-1);

$left=10; //pick a number
$right=imagesx($im)-$width;
$center=$right/2;
?>
[/code]
Link to comment
Share on other sites

Thanks,

I am still very new to php.  Is there a way to integrate that into what I currently have?

[code]
class textPNG {

var $font = 'ttf/Script/Amazone.ttf'; //default font. directory relative to script directory.
var $msg = "this is a sample of using dynamic fonts with PHP"; // default text to display.
var $size = 25;
var $rot = 0; // rotation in degrees.
var $pad = 15; // padding.
var $transparent = 0; // transparency set to on.
var $red = 0; // black text...
var $grn = 0;
var $blu = 0;
var $bg_red = 255; // on white background.
var $bg_grn = 255;
var $bg_blu = 255;
var $hex = 'FFFFFF';

function draw() {
$width = 0;
$height = 0;
$offset_x = 0;
$offset_y = 0;
$bounds = array();
$image = "";

// determine font height.
$bounds = ImageTTFBBox($this->size, $this->rot, $this->font, "W");
if ($this->rot < 0) {
$font_height = abs($bounds[7]-$bounds[1]);
} else if ($this->rot > 0) {
$font_height = abs($bounds[1]-$bounds[7]);
} else {
$font_height = abs($bounds[7]-$bounds[1]);
}

// determine bounding box.
$bounds = ImageTTFBBox($this->size, $this->rot, $this->font, $this->msg);
if ($this->rot < 0) {
$width = abs($bounds[4]-$bounds[0]);
$height = abs($bounds[3]-$bounds[7]);
$offset_y = $font_height;
$offset_x = 0;

} else if ($this->rot > 0) {
$width = abs($bounds[2]-$bounds[6]);
$height = abs($bounds[1]-$bounds[5]);
$offset_y = abs($bounds[7]-$bounds[5])+$font_height;
$offset_x = abs($bounds[0]-$bounds[6]);

} else {
$width = abs($bounds[4]-$bounds[6]);
$height = abs($bounds[7]-$bounds[1]);
$offset_y = $font_height;;
$offset_x = 0;
}

$image = imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);

$background = ImageColorAllocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
$foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);

if ($this->transparent) ImageColorTransparent($image, $background);
ImageInterlace($image, false);

// render it.
ImageTTFText($image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $this->msg);

// output PNG object.
imagePNG($image);

} //function draw
} //class

[/code]

Many Thanks!
Link to comment
Share on other sites

Ok, this is what I have tried, but it doesn't appear to be working.
Any Ideas??

[code]
class textPNG {

var $font = 'ttf/Script/Amazone.ttf'; //default font. directory relative to script directory.
var $msg = "this is a sample of using dynamic fonts with PHP"; // default text to display.
var $size = 25;
var $rot = 0; // rotation in degrees.
var $pad = 15; // padding.
var $transparent = 0; // transparency set to on.
var $red = 0; // black text...
var $grn = 0;
var $blu = 0;
var $bg_red = 255; // on white background.
var $bg_grn = 255;
var $bg_blu = 255;
var $hex = 'FFFFFF';
var $alignment = 'Left';

function draw() {
$width = 0;
$height = 0;
$offset_x = 0;
$offset_y = 0;
$bounds = array();
$image = "";

// determine font height.
$bounds = ImageTTFBBox($this->size, $this->rot, $this->font, "W");
if ($this->rot < 0)
{
$font_height = abs($bounds[7]-$bounds[1]);
}

else if ($this->rot > 0)
{
$font_height = abs($bounds[1]-$bounds[7]);
}

else
{
$font_height = abs($bounds[7]-$bounds[1]);
}

// determine bounding box.
$bounds = ImageTTFBBox($this->size, $this->rot, $this->font, $this->msg);
if ($this->rot < 0)
{
$width = abs($bounds[4]-$bounds[0]);
$height = abs($bounds[3]-$bounds[7]);
$offset_y = $font_height;
$offset_x = 0;
}

else if ($this->rot > 0)
{
$width = abs($bounds[2]-$bounds[6]);
$height = abs($bounds[1]-$bounds[5]);
$offset_y = abs($bounds[7]-$bounds[5])+$font_height;
$offset_x = abs($bounds[0]-$bounds[6]);
}

else
{
$width = abs($bounds[4]-$bounds[6]);
$height = abs($bounds[7]-$bounds[1]);
$offset_y = $font_height;;
// $offset_x = 0;

if ($this->alignment == "Right")
$offset_x -= $width;

else if ($this->alignment == "Centre")
$offset_x -= $width / 2;

else $offset_x = 0;
} //else

$image = imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);

$background = ImageColorAllocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
$foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);

if ($this->transparent) ImageColorTransparent($image, $background);
ImageInterlace($image, false);



// render it.
ImageTTFText($image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $this->msg);

// output PNG object.
imagePNG($image);

} //function draw
} //class

[/code]


Thanks!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.