imageftbbox()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

imageftbbox()

Post by Ollie Saunders »

I've just migrated a project from one server to another only to find that although my new server has newer version of just about everything it doesn't come with the FreeType library for GD. I didn't know this even existed until 10 minutes ago but now that I do I've realised it is really important to this code i'm using to generate graphs.

Code: Select all

class Text
{
	var $HORIZONTAL_LEFT_ALIGN = 1;		// PHP4 doesn't support class constants
	var $HORIZONTAL_CENTER_ALIGN = 2;
	var $HORIZONTAL_RIGHT_ALIGN = 4;
	var $VERTICAL_TOP_ALIGN = 8;
	var $VERTICAL_CENTER_ALIGN = 16;
	var $VERTICAL_BOTTOM_ALIGN = 32;

	/**
	* Creates a new text drawing helper
	*
	* @access	public
	*/
	
	function Text()
	{
		// Free low-res fonts based on Bitstream Vera <http://dejavu.sourceforge.net/wiki/>

		$this->fontCondensed = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed.ttf";
		$this->fontCondensedBold = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed-Bold.ttf";
	}

	/**
	* Print text
	*
	* @access	public
	* @param	Image		GD image
	* @param	integer		text coordinate (x)
	* @param	integer		text coordinate (y)
	* @param	Color		text color
	* @param	string		text value
	* @param	string		font file name
	* @param	bitfield	text alignment
	*/
	
	function printText($img, $px, $py, $color, $text, $fontFileName, $align = 0)
	{
		if(!($align & $this->HORIZONTAL_CENTER_ALIGN) && !($align & $this->HORIZONTAL_RIGHT_ALIGN))
			$align |= $this->HORIZONTAL_LEFT_ALIGN;

		if(!($align & $this->VERTICAL_CENTER_ALIGN) && !($align & $this->VERTICAL_BOTTOM_ALIGN))
			$align |= $this->VERTICAL_TOP_ALIGN;

		$fontSize = 8;
		$lineSpacing = 1;

		//**
		//** imageftbbox undefined   
		//** so is imagettfbbox
		//**
		list($llx, $lly, $lrx, $lry, $urx, $ury, $ulx, $uly) = imageftbbox($fontSize, 0, $fontFileName, $text, array("linespacing" => $lineSpacing));
		//**
		//**
		//**
		//**

		$textWidth = $lrx - $llx;
		$textHeight = $lry - $ury;

		$angle = 0;

		if($align & $this->HORIZONTAL_CENTER_ALIGN)
			$px -= $textWidth / 2;

		if($align & $this->HORIZONTAL_RIGHT_ALIGN)
			$px -= $textWidth;

		if($align & $this->VERTICAL_CENTER_ALIGN)
			$py += $textHeight / 2;

		if($align & $this->VERTICAL_TOP_ALIGN)
			$py += $textHeight;

		imagettftext($img, $fontSize, $angle, $px, $py, $color->getColor($img), $fontFileName, $text);
	}
	
	/**
	* Print text centered horizontally on the image
	*
	* @access	public
	* @param	Image		GD image
	* @param	integer		text coordinate (y)
	* @param	Color		text color
	* @param	string		text value
	* @param	string		font file name
	*/
	
	function printCentered($img, $py, $color, $text, $fontFileName)
	{
		$this->printText($img, imagesx($img) / 2, $py, $color, $text, $fontFileName, $this->HORIZONTAL_CENTER_ALIGN | $this->VERTICAL_CENTER_ALIGN);
	}
imageftbbox() seems to be very similar to imagettfbbox(), they both require the FreeType library which I don't have on this new server. Could anyone suggest any code to do the job that imageftbbox() does?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

bump, i really need help with this.
Post Reply