Please explai these text image fractions for me

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
the_one2003a
Forum Newbie
Posts: 4
Joined: Sun Aug 07, 2005 7:06 pm

Please explai these text image fractions for me

Post by the_one2003a »

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:

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;
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

Code: Select all

tags.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the first line, first fraction finds the horizontal center of the image. First line, second fraction finds the horizontal center of the text being created. The first line finds the left starting anchor point for the text.

The second line, first fraction finds the vertical center of the image. Second line, second fraction finds the vertical center of the text being created. The second line finds the top starting anchor point for the text.

Basically, it finds the place where the text will be perfectly centered in the image.
the_one2003a
Forum Newbie
Posts: 4
Joined: Sun Aug 07, 2005 7:06 pm

Post by the_one2003a »

thanks but why doesn't it just start from image_width/2 why does it minus it from text_width/2 to find the place to position the text

and also what do the

Code: Select all

if ($text_left<0) 
    $text_x -=$text_left;
and

Code: Select all

$text_y+=2;
mean?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the_one2003a wrote:thanks but why doesn't it just start from image_width/2 why does it minus it from text_width/2 to find the place to position the text
the starting point for the text drawing is the upper left corner of its box. If just text_width was used, you'd only get nudged in as wide as the text is.. not necessarily as wide as the image may be.. Think of the two as two rectangles. How does one position one rectangle in the exact center of the other when you can only control the upper left corners?
and also what do the

Code: Select all

if ($text_left<0) 
    $text_x -=$text_left;
I believe it's if the text's left position is to the left of the image's (less than the image's left), then adjust the text's position.
and

Code: Select all

$text_y+=2;
mean?
add 2 to text_y
the_one2003a
Forum Newbie
Posts: 4
Joined: Sun Aug 07, 2005 7:06 pm

Post by the_one2003a »

Thanks for you reply
I think I knew why it is subtracting the two widths (thanks for helping me on this),so this way it is just trying to find a point that the two rectangle have the same center huh?(but I still don't know why it is trying to centeralize them :oops: )

but as I tested phpTTFtext function its x and y are based on downer left part of the text bounding box.

and for the code:
if ($text_left<0)
$text_x -=$text_left;
the book says that because imageTTFBBox function returns the coordinates relative to base line of the text, text that is outer than the bounding box (mean before the base line start point that is origin of the TTFBBox calculation) so it is checking it and adding it to the $text_x, thats what the book says about it

and the end code

Code: Select all

$text_y+=2;
should I always calculate this extra adding number with trial and error method?

Thanks in advence
Post Reply