Greetings learned PHP(eople),

I am using the code below to create buttons based on entries that are
inserted into a text field from the preceding page.

I got this code from my trusty tome "PHP and MySQL Web Development" by
Luke Wellign/Laura Thomson.

Problem is this :

If I type in more than 18 characters the image creation fails with :

"The image "http://xxxxxx.xxxx.xx.xx/Generic/Admin/make_buttons.php";
cannot be displayed, because it contains errors"

When I view the HTML page source code, it simply states
"Text given is too large for button" 

Any ideas on how to make it so that the error I specified comes up and
not the "The image "http://xxxxxxx.xxx.xxx.xxx.x blah blah" part.....

Here`s the code :
-------
<?php
  Header('Content-type: image/png');
   
  //Posted from previous page
  //Button text and colour
  $button_text = $HTTP_POST_VARS['button_text'];
  $colour = $HTTP_POST_VARS['colour'];

  if (empty($button_text) || empty($colour))
     {
     echo 'Could not create button';
     exit;
     }
  
  //Create image from existing image
  $image = imagecreatefrompng ('images/purple.png');

  $image_width = ImageSX($image);
  $image_height = ImageSY($image);

  //Image needs 18 pixel margin in from the edge image 
  $image_width_wo_margins = $image_width - (2 * 18);
  $image_height_wo_margins = $image_height - (2 * 18);

  //Work out if the font size will fit and make it
  //smaller until it does. Start with large size and scale down
  $font_size = 33;
  
  //Set font name and location
  $fontname =
"/home/data/ClientWebs/chrisplay/Generic/Admin/a_d_mono.ttf";

  do
   {
   $font_size--;

   //Find out the size of the text at that font size
   $bbox = imagettfbbox ($font_size, 0, $fontname, $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 > $image_height_wo_margins || $width_text >
$image_width_wo_margins)
        );
  
     if ($height_text > $image_height_wo_margins || $width_text >
$image_width_wo_margins)
      {
      //No readable font size will fit on button
      echo 'Text given is too large for button';
      }
     else
      {
      //The font size fits
      //Now where to put it
      $text_x = $image_width/2.0 - $width_text/2.0;
      $text_y = $image_height/2.0 - $height_text/2.0;
          if ($left_text < 0 )
      
      $text_x += abs($left_text); //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($image, 0,0,255);

      ImageTTFText ($image, $font_size, 0, $text_x, $text_y, $white,
$fontname , $button_text);

      ImagePng($image);
      }
       ImageDestroy($image);
   ?>
  
  
Thanks in advance for any assistance......

Regards
-- 
Chris Blake
Office : (011) 782-0840
Cell : 083 985 0379

All most men really want in life is a wife, a house, two kids and a car,
a cat, no maybe a dog.  Ummm, scratch one of the kids and add a dog.
Definitely a dog.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to