_SAi_ Posted January 5, 2007 Share Posted January 5, 2007 Hi,Is there a way to specify the justification of text using the imagettftext function? IF so, how would I go about aligning my multi line text left, right or center?Thanks! Quote Link to comment Share on other sites More sharing options...
simcoweb Posted January 5, 2007 Share Posted January 5, 2007 I'm not sure what you're trying to accomplish but the simplest way might be to use CSS then just place the class name into your output in the script. Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted January 5, 2007 Share Posted January 5, 2007 You will have to justify one line at a time.left: easy, set your x coordinate to 1, or 5 or something small.right: more fun... using imagettfbbox(), calculate the total width of the text, then subtract it from the imagesx() of the image.center: the most fun... same as "right" but divide the end result by 2.The example below isn't fully tested, but it should fly.[code]<?php$im= imagecreatetruecolor(500,500);$coordinates=imagettfbbox(12, 0, 'arial.ttf', 'this is my display text');$width=$coordinates[0]-$coordinates[2];if($width<0) $width=width*(-1);$left=10; //pick a number$right=imagesx($im)-$width;$center=$right/2;?>[/code] Quote Link to comment Share on other sites More sharing options...
_SAi_ Posted January 6, 2007 Author Share Posted January 6, 2007 Thanks,I am still very new to php. Is there a way to integrate that into what I currently have?[code]class textPNG { var $font = 'ttf/Script/Amazone.ttf'; //default font. directory relative to script directory. var $msg = "this is a sample of using dynamic fonts with PHP"; // default text to display. var $size = 25; var $rot = 0; // rotation in degrees. var $pad = 15; // padding. var $transparent = 0; // transparency set to on. var $red = 0; // black text... var $grn = 0; var $blu = 0; var $bg_red = 255; // on white background. var $bg_grn = 255; var $bg_blu = 255; var $hex = 'FFFFFF';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->transparent) ImageColorTransparent($image, $background); ImageInterlace($image, false); // 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);} //function draw } //class[/code]Many Thanks! Quote Link to comment Share on other sites More sharing options...
_SAi_ Posted January 6, 2007 Author Share Posted January 6, 2007 Ok, this is what I have tried, but it doesn't appear to be working.Any Ideas??[code]class textPNG { var $font = 'ttf/Script/Amazone.ttf'; //default font. directory relative to script directory. var $msg = "this is a sample of using dynamic fonts with PHP"; // default text to display. var $size = 25; var $rot = 0; // rotation in degrees. var $pad = 15; // padding. var $transparent = 0; // transparency set to on. var $red = 0; // black text... var $grn = 0; var $blu = 0; var $bg_red = 255; // on white background. var $bg_grn = 255; var $bg_blu = 255; var $hex = 'FFFFFF'; var $alignment = 'Left';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; if ($this->alignment == "Right") $offset_x -= $width; else if ($this->alignment == "Centre") $offset_x -= $width / 2; else $offset_x = 0; } //else $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->transparent) ImageColorTransparent($image, $background); ImageInterlace($image, false); // 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);} //function draw } //class[/code]Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.