note 65251 deleted from function.imagettftext by danbrown
| From: | danbrown@php.net | Date: | Mon, 03 Dec 2012 22:21:27 +0000 |
| Subject: | note 65251 deleted from function.imagettftext by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192264@lists.php.net to get a copy of this message | ||
Note Submitter: jpetsel at riverviewtech dot net
----
I was looking at ugo's center and it was great for left to right centering, but it
wouldn't center up and down. So with some changes to suit my needs this is what I came up
with. Some of the notations are just for my edifcation so may mean nothing for you all but you will
see I added a $posy that helps with centering up and down. I was also feeding my feilds from a
form.
<?php
// Set the content-type
header("Content-type: image/png");
//1line per column
$text=array(0=>$_POST['linei'],1=>$_POST['lineii'],
2=>$_POST['lineiii'],3=>$_POST['lineiv'],4=>$_POST['linev']);
$Image_Width=400;
$Text_Size=$_POST['textsize'];
$Image_Height=400;
// Create the image
$im = imagecreatetruecolor ($Image_Width, $Image_Height); /* Create a blank image */
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 1, 1, 398, 398, $white);
// Replace path by your own font path
$font = $_POST['font_name'];
for ($i=0; $i < sizeof($text); $i++) {
//Center the text
$Text_Box_Size = imagettfbbox($Text_Size, 0, $font, $text[$i]);
$Text_Box_Width = abs($Text_Box_Size[2] + $Text_Box_Size[0]);
$Text_Box_Height = abs($Text_Box_Size[7] + $Text_Box_Size[1]);
$Text_Height = $Text_Height + $Text_Box_Height;
$posx = ($Image_Width - $Text_Box_Width) / 2;
$posy = ($Image_Height - $Text_Box_Height * sizeof($text)) / 2 + $Text_Height;
/*
Add the text (***array imagettftext ( resource image, float size, float angle,
int x, int y, int color, string fontfile, string text )***)
*/
imagettftext($im, $Text_Size, 0, $posx, $posy, $black, $font, $text[$i]);
}
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>