Simple question about GD

Discussion in 'PHP' started by NoamBarz, Jul 7, 2008.

  1. #1
    I am trying to use GD to create a thumbnail.
    I started by trying to create a simple rectangle with some text inside.

    Here's the code I used:

    Header("Content-type: image/gif");
      if(isset($_GET["s"])) $s = $_GET["s"];
      else $s=11;
      
      if(isset($_GET["text"])) $text = $_GET["text"];
      else $text = "lala";
      
      $font = "../Fonts/arial.TTF";
      
      $size = imagettfbbox($s,0,"tahoma.TTF",$text);
      $dx = abs($size[2]-$size[0]);
      $dy = abs($size[5]-$size[3]);
        
      $im = imagecreate($dx,$dy);
      $black = ImageColorAllocate($im, 0,0,0);
      $white = ImageColorAllocate($im, 255,255,255);
      ImageTTFText($im, $s, 0, 0, $dy, $white, $font, $text);
        
      ImageGif($im);
      ImageDestroy($im);
    Code (markup):
    What I was expecting to happen was to create a black rectangle of the exact size of my text and on it, to display the actual text with no padding at all. The problem is that the code produced an image with the text but with what looks like left and top padding. This causes my text to excede the image boundries and therefore to appear cut.

    I think the "imagettfbbox" function returns the correct values, but the problem is, in my opinion, that the "imagecreate" function only accepts integer values and therefore produces an image of close but not the exact size. If I were only using one font type and one font size, I could manually fix the problem. But since I need to be able to load different fonts with different sizes, I cannot manually fix the problem.

    Can anyone help? Any ideas?
    This is very important, please help!
     
    NoamBarz, Jul 7, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Danltn, Jul 7, 2008 IP
  3. NoamBarz

    NoamBarz Active Member

    Messages:
    242
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Thanks a lot for the help.
    So I tried using the "imagefontwidth" function, but the thing is that this function works with only 5 built in fonts or with the "imageloadfont" function. The "imageloadfont" function does not accept True Type fonts. I need to find a way to make this work with True Type fonts.

    Can you help?
     
    NoamBarz, Jul 7, 2008 IP