SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using TTF within an image

    Can someone please show me how to change the display font to a TTF in this code? I looked at the online manual under imageloadfont(), but I did not understand it enough. I have the MS TTFs installed on my server and they are working correctly.

    Thanks

    Chris

    PHP Code:
     <?php
    header 
    ("Content-type: image/png");
    $string "Text message goes here";
    $font   "2";
    $width  ImageFontWidth($font) * strlen($string);
    $height ImageFontHeight($font);

    $im = @imagecreate ($width,$height);
    $background_color imagecolorallocate ($im255255255); //white background
    $text_color imagecolorallocate ($im00,0);//black text
    imagestring ($im$font00,  $string$text_color);
    imagepng ($im);
    ?>

  2. #2
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this class :
    PHP Code:
    <?
    /*
    To draw the PNG - call this script with a URL like the following:
    [url=http://www.yoursite.com/text_png.php3?msg=testing+class&rot=15&size=48&font=fonts/ARIAL.TTF]http://www.yoursite.com/text_png.php3?msg=testing+class&rot=15&size=48&font=fonts/ARIAL.TTF[/url]
    */
    Header("Content-type: image/png");
    class 
    textPNG {
     var 
    $font 'mecha.ttf'//default font. directory relative to script directory.
     
    var $msg "undefined"// default text to display.
     
    var $size 24;
     var 
    $rot 0// rotation in degrees.
     
    var $pad 0// padding.
     
    var $transparent 1// transparency set to on.
     
    var $red 0// white text...
     
    var $grn 0;
     var 
    $blu 0;
     var 
    $bg_red 255// on black background.
     
    var $bg_grn 255;
     var 
    $bg_blu 255;
     var 
    $px 0;
     var 
    $py 0;

    function 
    draw() {
     
    $width 0;
     
    $height 0;
     
    $offset_x 0;
     
    $offset_y 0;
     
    $bounds = array();
     
    $image "";
     
     
    // determine font height.
     
    $bounds ImageTTFBBox($this->size$this->rot$this->font"W");
     if (
    $this->rot 0) {
      
    $font_height abs($bounds[7]-$bounds[1]);  
     } else if (
    $this->rot 0) {
      
    $font_height abs($bounds[1]-$bounds[7]);
     } else {
      
    $font_height abs($bounds[7]-$bounds[1]);
     }
     
    // determine bounding box.
     
    $bounds ImageTTFBBox($this->size$this->rot$this->font$this->msg);
     if (
    $this->rot 0) {
      
    $width abs($bounds[4]-$bounds[0]);
      
    $height abs($bounds[3]-$bounds[7]);
      
    $offset_y $font_height;
      
    $offset_x 0;
      
     } else if (
    $this->rot 0) {
      
    $width abs($bounds[2]-$bounds[6]);
      
    $height abs($bounds[1]-$bounds[5]);
      
    $offset_y abs($bounds[7]-$bounds[5])+$font_height;
      
    $offset_x abs($bounds[0]-$bounds[6]);
      
     } else {
      
    $width abs($bounds[4]-$bounds[6]);
      
    $height abs($bounds[7]-$bounds[1]);
      
    $offset_y $font_height;
      
    $offset_x 0;
     }
       
     
    $image imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);
     
     
    $background ImageColorAllocate($image$this->bg_red$this->bg_grn$this->bg_blu);
     
    $foreground ImageColorAllocate($image$this->red$this->grn$this->blu);
     
     if (
    $this->transparentImageColorTransparent($image$background);
     
    ImageInterlace($imagefalse);
      if(
    $this->py)
        
    $offset_y=$this->py+$offset_y ;
      
      if(
    $this->px)
        
    $offset_x=$this->px$offset_x ;

     
    // render it.
     
    ImageTTFText($image$this->size$this->rot$offset_x+$this->pad$offset_y+$this->pad$foreground$this->font$this->msg);
     
     
    // output PNG object.
     
    imagePNG($image);
    }
    }
    $text = new textPNG;
    if (isset(
    $px)) $text->px $px// text margins
    if (isset($py)) $text->py $py// text margins
    if (isset($msg)) $text->msg $msg// text to display
    if (isset($font)) $text->font $font// font to use (include directory if needed).
    if (isset($size)) $text->size $size// size in points
    if (isset($rot)) $text->rot $rot// rotation
    if (isset($pad)) $text->pad $pad// padding in pixels around text.
    if (isset($red)) $text->red $red// text color
    if (isset($grn)) $text->grn $grn// ..
    if (isset($blu)) $text->blu $blu// ..
    if (isset($bg_red)) $text->bg_red $bg_red// background color.
    if (isset($bg_grn)) $text->bg_grn $bg_grn// ..
    if (isset($bg_blu)) $text->bg_blu $bg_blu// ..
    if (isset($tr)) $text->transparent $tr// transparency flag (boolean).
    $text->draw();
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •