Location: PHPKode > projects > FUNKWERK > apifunk/inc/class.gdfont.inc.php
<?php
	/*******************************************************************\
	* FUNKWERK - GD Font                                                *
	* http://www.funkwerk.net                                           *
	*                                                                   *
	* Written by by Bettina Gille [hide@address.com]                    *
	*                                                                   *
	* Creates graphical buttons using GD and a TTF font                 *
	* Copyright (C) 2002 Bettina Gille                                  *
	* ------------------------------------------------------------------*
	* This library is part of the FUNKWERK API                          *
	* ------------------------------------------------------------------*
	* This library is free software; you can redistribute it and/or     *
	* modify it under the terms of the GNU General Public License as    *
	* published by the Free Software Foundation; either version 2 of    *
	* the License, or (at your option) any later version.               *
	*                                                                   *
	* This program is distributed in the hope that it will be useful,   *
	* but WITHOUT ANY WARRANTY; without even the implied warranty of    *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  *
	* General Public License for more details.                          *
	*                                                                   *
	* You should have received a copy of the GNU General Public License *
	* along with this program; if not, write to the Free Software       *
	* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.         *
	\*******************************************************************/
	/* $Id: class.gdfont.inc.php,v 1.11 2002/08/31 03:05:40 bettina Exp $ */

	class gdfont
	{
		var $debug;

		var $image;
		var $bg_color;

		var $width;
		var $height;

		var $xpad;
		var $ypad;

		var $font_size;
		var $font_color;
		var $font_text;
		var $font_align;
		var $font_valign;
		var $size_dependence;
		var $ttf_font;

		var $filename;
		var $savename;
		var $tmpfile;

		var $account;

		var $pic;
		var $img;

		function gdfont($debug=False)
		{
			if(!function_exists('imagecreate'))
			{
				echo '<br><center>' . lang('No GD support in this PHP install !') . '</center><br><br>';
			}

			$this->debug			= $debug;

			$this->image			= 0;
			$this->width			= 0;
			$this->height			= 0;

			$this->bg_color			= 0;

			$this->font_size		= 0;
			$this->font_color		= 0;
			$this->font_text		= '';
			$this->font_align		= '';
			$this->font_valign		= '';

			$this->size_dependence	= '';

			$this->xpad				= 0;
			$this->ypad				= 0;

			$this->filename			= '';
			$this->savename			= '';
			$this->tmpfile			= '';

			$this->account			= $GLOBALS['phpgw_info']['user']['account_id'];

			$this->pic				= CreateObject('apifunk.pictures',$debug);
			$this->img				= CreateObject('apifunk.gdimage',$debug);

			/* ttf font vars */

			$this->ttf_font_dir		=  PHPGW_SERVER_ROOT . SEP . 'apifunk' . SEP . 'fonts' . SEP;
			$this->ttf_font			= '';
		}

		function get_ttf_fonts()
		{
			$basedir = opendir($this->ttf_font_dir);

			while ($fonts = readdir($basedir))
			{
				if (($fonts != '.') && ($fonts != '..') && ($fonts != 'README') && ($fonts != 'CVS'))
				{
					$ttf_fonts[] = $fonts;
				}
			}
			closedir($basedir);
			return $ttf_fonts;
		}

		function ttf_font_path()
		{
			$this->ttf_font = $this->ttf_font_dir . $this->ttf_font;
			return True;
		}

		function color_select($selected)
		{
			return $this->img->color_select($selected);
		}

		function file_name()
		{
			$this->filename = 'funk_button_' . md5($this->font_text) . '.png';
			$this->filename = strtolower($this->filename);

			return True;
		}

		function save_name()
		{
			srand((double)microtime()*1000000);
			$this->savename = rand(1,100000) . '_' . $this->filename;
			return True;
		}

		function tmp_file()
		{
			$this->tmpfile = $this->account . '_' . $this->filename;
		}

		function set_font_size($font = '3', $ttf = False)
		{
			if (!$ttf)
			{
				if ($font >=1 && $font <=5)
				{
					$this->font_size = $font;
				}
				elseif ($font < 1)
				{
					$this->font_size = 1;
				}
				else
				{
					$this->font_size = 5;
				}
			}
			else
			{
				$this->font_size = $font;
			}
			return True;
		}

		function set_font_color_by_name()
		{
			$this->img->set_color_by_name($this->font_color,'font');
			$this->font_color = $this->img->font_color;
		}

		function get_font_height()
		{
			return ImageFontHeight($this->font_size);
		}

		function get_font_width()
		{
			return ImageFontWidth($this->font_size);
		}

		function button_init()
		{
			$this->file_name();
			$this->tmp_file();
			$this->img->width		= $this->width;
			$this->img->height		= $this->height;
			$this->img->bg_color	= $this->bg_color;

			if ($this->img->button_init())
			{
				$this->image = $this->img->image;
				return True;
			}
			return False;
		}

		function gd_button()
		{
			$tx	= ImageFontWidth($this->font_size) * strlen($this->font_text);
			$ty	= ImageFontHeight($this->font_size);

			switch ($this->size_dependence)
			{
				case 'text':
					$this->width		= intval(($this->xpad*2) + $tx);
					$this->height		= intval($this->ypad + $ty);

					$this->font_align	= 'center';
					$this->font_valign	= 'middle';
					break;
			}

			switch($this->font_align)
			{
				case 'center':
					$xpos = ($this->width/2) - ($tx/2);
					if ($xpos < 0 || $xpos < $this->xpad)
					{
						$xpos = $this->xpad;
					}
					break;
				case 'right':
					$xpos = $this->width - $tx - $this->xpad;
					if ($xpos < 0 || $xpos < $this->xpad)
					{
						$xpos = $this->xpad;
					}
					break;
				default:
					$xpos = $this->xpad;
					break;
			}

			switch($this->font_valign)
			{
				case 'middle':
					$ypos = ($this->height/2) - ($ty/2);
					if ($ypos < 0 || $ypos < $this->ypad)
					{
						$ypos = $this->ypad;
					}
					break;
				case 'bottom':
					$ypos = $this->height - $ty - $this->ypad;
					if ($ypos < 0 || $ypos < $this->ypad)
					{
						$ypos = $this->ypad;
					}
					break;
				default:
					$ypos = $this->ypad;
					break;
			}

			$this->button_init();

			$black = ImageColorAllocate($this->image, 0,0,0);
			ImageRectangle($this->image,0,0,$this->width-1,$this->height-1,$black);

			$this->set_font_color_by_name();
			ImageRectangle($this->image,0,0,$this->width,$this->height,$this->font_color);

			ImageString($this->image, $this->font_size, intval($xpos+1), intval($ypos), $this->font_text, $black);
			ImageString($this->image, $this->font_size, intval($xpos), intval($ypos-1), $this->font_text, $this->font_color);
			return True;
		}

		function draw_text_vert($valign = 'middle', $align = 'center')
		{
			ImageStringUp($this->image, $this->font_size, $this->cur_x, $this->cur_y, $this->font_text, $this->font_color);
		}

		function ttf_box()
		{
			$ttf_coordinates = ImageTTFBBox($this->font_size, 0,$this->ttf_font,$this->font_text);

			$ttf_coord = array
			(
				'low_left_x'	=> $ttf_coordinates[0],
				'low_left_y'	=> $ttf_coordinates[1],
				'low_right_x'	=> $ttf_coordinates[2],
				'low_right_y'	=> $ttf_coordinates[3],
				'up_right_x'	=> $ttf_coordinates[4],
				'up_right_y'	=> $ttf_coordinates[5],
				'up_left_x'		=> $ttf_coordinates[6],
				'up_left_y'		=> $ttf_coordinates[7]
			);
			return $ttf_coord;
		}

		function ttf_lolo_button()
		{
			$this->ttf_font_path();

			$coord	= $this->ttf_box();

			$tx		= abs($coord['low_right_x']-$coord['low_left_x']);
			$ty		= abs($coord['up_right_y']-$coord['low_right_y']);

			switch ($this->size_dependence)
			{
				case 'text':
					$this->width	= ($this->xpad/2) + $this->xpad + $tx;
					$this->height	= $ty + $this->ypad;
					break;
				case 'fixed':
					switch($this->font_align)
					{
						case 'center':
							$xpos = ($this->width - $tx) / 2;
							if ($xpos < 0 || $xpos < $this->xpad)
							{
								$xpos = $this->xpad;
							}
							break;
						case 'right':
							$xpos = ($this->width-5) - $tx - $this->xpad;
							if ($xpos < 0 || $xpos < $this->xpad)
							{
								$xpos = $this->xpad;
							}
							break;
						default:
							$xpos = $this->xpad;
							break;
					}

					switch($this->font_valign)
					{
						case 'middle':
							$ypos = (($this->height - $ty) / 2) + $ty;
							break;
						case 'bottom':
							$ypos = $this->height - $this->ypad;
							break;
						default:
							$ypos = $this->height - $ty - $this->ypad;
							break;
					}
					break;
			}

			$this->button_init();

			$black = ImageColorAllocate($this->image, 0,0,0);
			ImageRectangle($this->image,0,0,$this->width-1,$this->height-1,$black);
			$this->set_font_color_by_name();
			ImageRectangle($this->image,0,0,$this->width,$this->height,$this->font_color);

			switch ($this->size_dependence)
			{
				case 'text':
					ImageTTFText($this->image,$this->font_size, 0,intval($this->xpad/2)+1,$ty+intval($this->ypad/2),-$black,$this->ttf_font,$this->font_text);
					ImageTTFText($this->image,$this->font_size, 0, intval($this->xpad/2), $ty+intval($this->ypad/2)-1, -$this->font_color, $this->ttf_font, $this->font_text);
					break;
				case 'fixed':
					ImageTTFText($this->image,$this->font_size, 0,intval($xpos)+1, $ypos,-$black,$this->ttf_font,$this->font_text);
					ImageTTFText($this->image,$this->font_size, 0, $xpos, intval($ypos)-1, -$this->font_color, $this->ttf_font, $this->font_text);
					break;
			}
			return True;
		}

		function to_browser()
		{
			return $this->img->image_to_browser();
		}

		function button_done()
		{
			$this->img->image_done();
		}

		function save_tmp()
		{
			ImagePNG($this->image,$this->pic->img_dir . $this->tmpfile);
		}

		function save_button()
		{
			$this->file_name();
			$this->tmp_file();
			$im = imagecreatefrompng($this->pic->img_dir . $this->tmpfile);
			$this->save_name();

			$pix = array
			(
				'title' => $this->font_text,
			'file_name' => $this->savename,
			'img_file'	=> $this->pic->image2png($im,$this->savename)
			);

			ImageDestroy($im);
			$this->drop_tmp();
			return $pix;
		}

		function drop_tmp()
		{
			if (is_file($this->pic->img_dir . $this->tmpfile))
			{
				unlink($this->pic->img_dir . $this->tmpfile);
				return True;
			}
		}
	}
Return current item: FUNKWERK