Small error with PHP Button/Menu Script - Working Code Included Too.
From: Shaun (smacpher_at_gmail.com)
Date: 01/18/05
- Next message: HansH: "Re: Problem with rewritten URLs"
- Previous message: Good Man: "Re: php vs. perl"
- Next in thread: Steve: "Re: Small error with PHP Button/Menu Script - Working Code Included Too."
- Reply: Steve: "Re: Small error with PHP Button/Menu Script - Working Code Included Too."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 18 Jan 2005 12:33:23 -0800
Good day everyone :),
I am having some problems with a script to make buttons/menus from
here (http://www.jwweb.com/20020731.html) the text is released under
the "Gnu Public License for open-source material".
I will put ////// where the code breaks down:
--- Code for buttons ---
<?
// check we have the appropriate variable data
// variables are button-text and color
if (empty($button_text))
$button_text = "Sample";
if (empty($color))
$color = "blue"; if (empty($red))
$red= 0;
if (empty($grn))
$grn = 0;
if (empty($blu))
$blu = 0;
// create an image of the right background and check size
$im = imagecreatefromgif ("$color-button.gif");
$width_image = ImageSX($im);
$height_image = ImageSY($im);
// Our images need a 10 pixel margin in from the edge image
$width_image_wo_margins = $width_image - (2 * 10);
$height_image_wo_margins = $height_image - (2 * 10);
// Work out if the font size will fit and make it smaller until it
does
// Start out with the biggest size that will reasonably fit on our
buttons
$font_size = 33;
do
{
$font_size--;
// find out the size of the text at that font size
$bbox=imagettfbbox ($font_size, 0, $font_name, $button_text);
$right_text = $bbox[2]; // right co-ordinate
$left_text = $bbox[0]; // left co-ordinate
$width_text = $right_text - $left_text; // how wide is it?
$height_text = abs($bbox[7] - $bbox[1]); // how tall is it?
//////The code breaks down after the 8 in "8 &&"
} while ( $font_size>8 &&
( $height_text>$height_image_wo_margins ||
$width_text>$width_image_wo_margins )
);
if ( $height_text>$height_image_wo_margins ||
$width_text>$width_image_wo_margins )
{
// no readable font size will fit on button
echo "Text given will not fit on button.<BR>";
}
else
{
// We have found a font size that will fit
// Now work out where to put it
$text_x = $width_image/2.0 - $width_text/2.0;
$text_y = $height_image/2.0 - $height_text/2.0 ;
if ($left_text < 0)
$text_x += abs($left_text); // add factor for left
overhang
$above_line_text = abs($bbox[7]); // how far above the baseline?
$text_y += $above_line_text; // add baseline factor
$text_y -= 2; // adjustment factor for shape of our template
$white = ImageColorAllocate ($im, $red, $grn, $blu);
ImageTTFText ($im, $font_size, 0, $text_x, $text_y, $white,
$font_name, $button_text);
Header ("Content-type: image/gif");
ImageGif ($im);
}
ImageDestroy ($im);
?>
---
I cut away some of the non working code and it works well here:
--- Working code ---
<?
// check we have the appropriate variable data
// variables are button-text and color
if (empty($button_text))
$button_text = "Sample";
if (empty($color))
$color = "blue"; if (empty($red))
$red= 0;
if (empty($grn))
$grn = 0;
if (empty($blu))
$blu = 0;
// create an image of the right background and check size
$im = imagecreatefrompng("$color-button.png");
$width_image = ImageSX($im);
$height_image = ImageSY($im);
// Our images need a 10 pixel margin in from the edge image
$width_image_wo_margins = $width_image - (2 * 10);
$height_image_wo_margins = $height_image - (2 * 10);
// Work out if the font size will fit and make it smaller until it
does
// Start out with the biggest size that will reasonably fit on our
buttons
$font_size = 33;
do
{
$font_size--;
// find out the size of the text at that font size
$bbox=imagettfbbox ($font_size, 0, $font_name, $button_text);
$right_text = $bbox[2]; // right co-ordinate
$left_text = $bbox[0]; // left co-ordinate
$width_text = $right_text - $left_text; // how wide is it?
$height_text = abs($bbox[7] - $bbox[1]); // how tall is it?
} while ( $font_size>8 &&
( $height_text>$height_image_wo_margins ||
$width_text>$width_image_wo_margins )
);
if ( $height_text>$height_image_wo_margins ||
$width_text>$width_image_wo_margins )
{
// no readable font size will fit on button
echo "Text given will not fit on button.<BR>";
}
else
{
// We have found a font size that will fit
// Now work out where to put it
$text_x = $width_image/2.0 - $width_text/2.0;
$text_y = $height_image/2.0 - $height_text/2.0 ;
if ($left_text < 0)
$text_x += abs($left_text); // add factor for left
overhang
$above_line_text = abs($bbox[7]); // how far above the baseline?
$text_y += $above_line_text; // add baseline factor
$text_y -= 2; // adjustment factor for shape of our template
$white = ImageColorAllocate ($im, $red, $grn, $blu);
ImageTTFText ($im, $font_size, 0, $text_x, $text_y, $white,
$font_name, $button_text);
Header ("Content-type: image/gif");
ImageGif ($im);
}
ImageDestroy ($im);
?>
---
I was wondering if someone could tell me why it breaks down after the
8, and what I could do to fix it. It works as it is now but I'd
prefer the feature for auto adjusting font etc. :).
The website in the first paragraph explains most/all of what the code
is doing - it is a very great website. I wish there was a little
download kit though so people could be working from the same code /
image set :) (you have to copy the code section by section).
Thanks for your help,
Shaun
- Next message: HansH: "Re: Problem with rewritten URLs"
- Previous message: Good Man: "Re: php vs. perl"
- Next in thread: Steve: "Re: Small error with PHP Button/Menu Script - Working Code Included Too."
- Reply: Steve: "Re: Small error with PHP Button/Menu Script - Working Code Included Too."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]