Left Right Center align/justify of text in image. It is easy and simple to do in PHP.
Create an image from text and align them as you want. After that save or display image.
<?php
$str = "New life in programming.\nNext Line of Image.\nLine Number 3\n" .
"This is line numbet 4\nLine number 5\nYou can write as you want.";
header("Content-type: image/gif");
imagegif(imagettfJustifytext($str,"CENTURY.TTF",2));
function imagettfJustifytext($text, $font="CENTURY.TTF", $Justify=2, $W=0, $H=0, $X=0, $Y=0, $fsize=12, $color=array(0x0,0x0,0x0), $bgcolor=array(0xFF,0xFF,0xFF)){
$angle = 0;
$L_R_C = $Justify;
$_bx = imageTTFBbox($fsize,0,$font,$text);
$W = ($W==0)?abs($_bx[2]-$_bx[0]):$W; $H = ($H==0)?abs($_bx[5]-$_bx[3]):$H; $im = @imagecreate($W, $H)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, $bgcolor[0], $bgcolor[1], $bgcolor[2]); $text_color = imagecolorallocate($im, $color[0], $color[1], $color[2]); if($L_R_C == 0){ imagettftext($im, $fsize, $angle, $X, $fsize, $text_color, $font, $text);
}elseif($L_R_C == 1){ $s = split("[\n]+", $text);
$__H=0;
foreach($s as $key=>$val){
$_b = imageTTFBbox($fsize,0,$font,$val);
$_W = abs($_b[2]-$_b[0]);
$_X = $W-$_W;
$_H = abs($_b[5]-$_b[3]);
$__H += $_H;
imagettftext($im, $fsize, $angle, $_X, $__H, $text_color, $font, $val);
$__H += 6;
}
}
elseif($L_R_C == 2){ $s = split("[\n]+", $text);
$__H=0;
foreach($s as $key=>$val){
$_b = imageTTFBbox($fsize,0,$font,$val);
$_W = abs($_b[2]-$_b[0]);
$_X = abs($W/2)-abs($_W/2);
$_H = abs($_b[5]-$_b[3]);
$__H += $_H;
imagettftext($im, $fsize, $angle, $_X, $__H, $text_color, $font, $val);
$__H += 6;
}
}
return $im;
}
?>