Please explai these text image fractions for me
Posted: Sun Aug 07, 2005 7:41 pm
Hi recently I was reading a book about php and making Text images in it.
the code for making different buttons (with different texts) from a flat button template is as follow:
what I didn't understad is from after the do loop
the book didn't say much about these part of the code except that they are some fractions to show text at correct position but didn't explain each code and what each of these caculations do.
now if someone know what these caculations do in depth please explain them for me?
sincerely
the_one2003a
feyd | please post php in
the code for making different buttons (with different texts) from a flat button template is as follow:
Code: Select all
<?php
$button_text ='love';
$im= imagecreatefrompng('blue-button.png');
if (!$im)
{
echo 'couldn\'t create image from file';
}
$image_width = imagesx($im);
$image_height = imagesy($im);
$image_width_wo_marign = $image_width - (2 * 6);
$image_height_wo_marign = $image_height - (2 * 6);
$fontsize = 33;
putenv('GDFONTPATH=D:\win.xp\fonts');
$fontname = 'arial';
$green = imagecolorallocate($im,22,255,8);
do
{
$fontsize--;
$bbox = imageTTFbbox($fontsize,0,$fontname,$button_text);
$text_lef = $bbox[0];
$text_right = $bbox[2];
$text_width = $text_right - $text_lef;
$text_height = abs($bbox[7] - $bbox[1]);
}
while ($fontsize>8 && ($text_width > $image_width_wo_marign ||
$text_height > $image_height_wo_marign));
if ( $text_width> $image_width_wo_marign ||
$text_height >$image_height_wo_marign)
{
echo "Text cannot fit on the button";
}
else
{
$text_x = $image_width/2.0 - $text_width/2.0;
$text_y = $image_height/2.0 - $text_height/2.0;
if ($text_left<0)
$text_x -=$text_left;
$above_line_text = abs($bbox[7]);
$text_y += $above_line_text;
$text_y+=2;
$red = imagecolorallocate($im,230,0,0);
//imageTTFtext($im,$fontsize,$angle,$text_x,$text_y,$red,$fontname,$button_text);
imageTTFtext($im,12,0,10,25,$green,$fontname,'L');
//header("Content-type = image/png");
imagepng($im);
imagedestroy($im);
}
?>what I didn't understad is from after the do loop
Code: Select all
$text_x = $image_width/2.0 - $text_width/2.0;
$text_y = $image_height/2.0 - $text_height/2.0;
if ($text_left<0)
$text_x -=$text_left;
$above_line_text = abs($bbox[7]);
$text_y += $above_line_text;
$text_y+=2;now if someone know what these caculations do in depth please explain them for me?
sincerely
the_one2003a
feyd | please post php in
Code: Select all
tags.[/color]