Location: PHPKode > projects > FUNKWERK > apifunk/inc/class.gdimage.inc.php
<?php
	/*******************************************************************\
	* FUNKWERK - GD Image                                               *
	* http://www.funkwerk.net                                           *
	*                                                                   *
	* Written by by Bettina Gille [hide@address.com]                    *
	*                                                                   *
	* Create images using GD                                            *
	* Copyright (C) 2002 Bettina Gille                                  *
	* ------------------------------------------------------------------*
	* Some methods based on file htmlGD.php3 of                         *
	* Double Choco Latte - Source Configuration Management System       *
	* Copyright (C) 1999  Michael L. Dean & Tim R. Norman               *
	* ------------------------------------------------------------------*
	* 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.gdimage.inc.php,v 1.5 2002/08/28 02:10:17 bettina Exp $ */

	class gdimage
	{
		var $debug = False;
		var $type;
		var $cur_x;
		var $cur_y;

		var $image;
		var $bg_color;
		var $trans_color;
		var $width;
		var $height;
		var $ncolor;

		var $object;
		var $ncolor;
		var $obj_color;
		var $fg_color;
		var $obj_width;
		var $obj_height;

		var $color_select;
		var $object_select;

		var $font_color;

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

			$this->debug			= $debug;
			$this->tmp_file			= PHPGW_APP_ROOT . SEP . 'images' . SEP . 'draw_tmp.png';

			$this->image			= 0;
			$this->color			= 0;
			$this->trans_color		= 0;
			$this->bg_color			= 0;
			$this->width			= 0;
			$this->height			= 0;

			$this->object			= 0;
			$this->ncolor			= 0;
			$this->fg_color			= 0;
			$this->obj_color		= 0;
			$this->obj_width		= 0;
			$this->obj_height		= 0;

			$this->object_select	= array('circle1'	=> 'Circle 1',
											'circle3'	=> 'Circle 3',
											'line1'		=> 'Stroke Line',
											'poly1'		=> 'Polygon 1',
											'square1'	=> 'Square 1',
											'tri1'		=> 'Triangle 1');

			$this->pic				= CreateObject('apifunk.pictures');
			$this->icr				= $this->pic->icr;

			/* used by class.gdfont */
			$this->font_color			= 0;
		}

		function image_init()
		{
			if (is_file($this->tmp_file))
			{
				$this->get_image_size_array();
				$this->image = ImageCreatefromPNG($this->tmp_file) or die;
				return True;
			}
			else
			{
				$this->image = ImageCreate($this->width,$this->height) or die;

				if ($this->bg_color == 'transparent')
				{
					$this->set_color_by_name($this->bg_color,'img');
					$this->set_transparent('img');
				}
				else
				{
					$this->set_color_by_name($this->bg_color,'img');
				}

				$this->pic->image2png($this->image,'draw_tmp.png');
				return True;
			}
			return False;
		}

		function button_init()
		{
			$this->image = ImageCreate($this->width,$this->height) or die;

			if ($this->bg_color == 'transparent')
			{
				$this->set_color_by_name($this->bg_color,'img');
				$this->set_transparent('img');
			}
			else
			{
				$this->set_color_by_name($this->bg_color,'img');
			}
			return True;
		}

		function object_init()
		{
			$this->object = ImageCreate($this->obj_width, $this->obj_height) or die;
			return True;
		}

		function get_image_size_array()
		{
			$img_data		= $this->pic->get_image_size_array($this->tmp_file);
			$this->width	= $img_data['width'];
			$this->height	= $img_data['height'];
		//	return True;
		}

		function color_select($selected = '')
		{
			$color_select = array('snow'	=> 'Snow',
							'lemonchiffon'	=> 'Lemon Chiffon',
									'gold'	=> 'Gold',
							'darkorange'	=> 'Dark Orange',
								'tomato'	=> 'Tomato',
								'maroon'	=> 'Maroon',
							'cornflower'	=> 'Cornflower Blue',
							'turquoise1'	=> 'Turquoise 1',
							'chartreuse'	=> 'Chartreuse',
								'seagreen'	=> 'Sea Green',
							'olivedrab4'	=> 'Olive Drab 4',
								'darkgreen'	=> 'Dark Green',
							'chocolate3'	=> 'Chocolate 3',
						'lightslategrey'	=> 'Light Slate Grey',
									'dove'	=> 'Doves Blue',
							'midnightblue'	=> 'Midnight Blue',
								'black'		=> 'Black');

			while(list($key,$c_name) = each($color_select))
			{
				$format .= '<option value="' . $key . '"';
				if($selected == $key)
				{
					$format .= ' selected';
				}
				$format .= '>'. $c_name . '</option>' . "\n";
			}
			return $format;
		}

		function set_color($r = 0, $g = 0, $b = 0, $im = 'obj')
		{
			switch ($im)
			{
				case 'obj':		$this->color		= ImageColorAllocate($this->object, $r, $g, $b); break;
				case 'img':		$this->bg_color		= ImageColorAllocate($this->image, $r, $g, $b); break;
				case 'ob':		$this->obj_color	= ImageColorAllocate($this->object, $r, $g, $b); break;
				case 'nc':		$this->ncolor		= ImageColorAllocate($this->object, $r, $g, $b); break;
				case 'fg':		$this->fg_color		= ImageColorAllocate($this->object, $r, $g, $b); break;
				case 'font':	$this->font_color	= ImageColorAllocate($this->image, $r, $g, $b); break;
			}
			return True;
		}

		function set_trans_color($r = 0, $g = 0, $b = 0, $im = 'obj')
		{
			switch ($im)
			{
				case 'obj': $this->trans_color = ImageColorAllocate($this->object, $r, $g, $b); break;
				case 'img': $this->trans_color = ImageColorAllocate($this->image, $r, $g, $b); break;
			}
			return True;
		}

		function set_transparent($im = 'obj')
		{
			switch ($im)
			{
				case 'obj':	ImageColorTransparent($this->object, $this->trans_color); break;
				case 'img':	ImageColorTransparent($this->image, $this->trans_color); break;
			}
			return True;
		}

		function set_color_by_name($name = '', $im = 'obj')
		{
			$r = 0;
			$g = 0;
			$b = 0;
			switch ($name)
			{
				case 'snow':
					$r = 255;
					$g = 250;
					$b = 250;
					break;
				case 'lemonchiffon':
					$r = 255;
					$g = 250;
					$b = 205;
					break;
				case 'gold':
					$r = 255;
					$g = 215;
					break;
				case 'darkorange':
					$r = 255;
					$g = 140;
					break;
				case 'tomato':
					$r = 255;
					$g = 99;
					$b = 71;
					break;
				case 'maroon':
					$r = 176;
					$g = 48;
					$b = 96;
					break;
				case 'cornflower':
					$r = 100;
					$g = 149;
					$b = 237;
					break;
				case 'turquoise1':
					$g = 245;
					$b = 255;
					break;
				case 'chartreuse':
					$r = 127;
					$g = 255;
					break;
				case 'seagreen':
					$r = 46;
					$g = 139;
					$b = 87;
					break;
				case 'olivedrab4':
					$r = 105;
					$g = 139;
					$b = 34;
					break;
				case 'darkgreen':
					$g = 100;
					break;
				case 'chocolate3':
					$r = 205;
					$g = 102;
					$b = 29;
					break;
				case 'lightslategrey':
					$r = 119;
					$g = 136;
					$b = 153;
					break;
				case 'midnightblue':
					$r = 25;
					$g = 25;
					$b = 112;
					break;
				case 'dove':
					$r = 0x2c;
					$g = 0x6D;
					$b = 0xAF;
					break;
				case 'transparent':
					$r = 255;
					$b = 255;
					break;
			}

			if ($name == 'transparent')
			{
				return $this->set_trans_color($r, $g, $b, $im);
			}
			else
			{
				return $this->set_color($r, $g, $b, $im);
			}
		}

		function translate_point(&$x, &$y, $angle, $about_x, $about_y, $shift_x, $shift_y)
		{
			$x -= $about_x;
			$y -= $about_y;
			$angle = ($angle / 180) * M_PI;

			// math:
			//[x2,y2] = [x,  *  [[cos(a),-sin(a)],
           	//y]      [sin(a),cos(a)]]
			//==>
			//x = x * cos(a) + y*sin(a) 
			//y = x*-sin(a) + y*cos(a)

			$new_x = $x * cos($angle) - $y * sin($angle);
			$new_y = $x * sin($angle) + $y * cos($angle);
			$x = $new_x+ $about_x + $shift_x ;
			$y = $new_y + $about_y + $shift_y;
		}

		function translate_poly($point_array, $angle, $about_x, $about_y, $shift_x, $shift_y)
		{
			$translated_poly = Array();
			while(count($point_array) > 1)
			{
				$temp_x = array_shift($point_array);
				$temp_y = array_shift($point_array);

				translate_point($temp_x, $temp_y, $angle, $about_x, $about_y,$shift_x,$shift_y);
				array_push($translated_poly, $temp_x);
				array_push($translated_poly, $temp_y);
			}
			return $translated_poly;
		}

		function draw_polygon($points, $num_points, $style = 'filled')
		{
			if ($style == 'filled')
			{
				imagefilledpolygon($this->object,$points,$num_points,$this->color);
				return True;
			}
			else
			{
				imagepolygon($this->object,$points,$num_points,$this->color);
				return True;
			}
			return False;
		}

		function get_coordinates()
		{
			$coordinates = array
			(
				'middle_x'	=> round($this->obj_width/2),
				'middle_y'	=> round($this->obj_height/2),
				'third_x'	=> round($this->obj_width/3),
				'third_y'	=> round($this->obj_height/3),
				'fourth_x'	=> round($this->obj_width/4),
				'fourth_y'	=> round($this->obj_height/4),
				'sixt_x'	=> round($this->obj_width/6),
				'sixt_y'	=> round($this->obj_height/6),
				'eight_x'	=> round($this->obj_width/8),
				'eight_y'	=> round($this->obj_height/8),
				'twelft_x'	=> round($this->obj_width/12),
				'twelft_y'	=> round($this->obj_height/12),
						'x'	=> $this->obj_width,
						'y'	=> $this->obj_height
			);

			if($this->debug)
			{
				_debug_array($coordinates);
			}
			return $coordinates;
		}

		function draw_arc($xmiddle, $ymiddle, $width, $height, $sarc, $earc)
		{
			imagearc($this->object, $xmiddle, $ymiddle, $width, $height, $sarc, $earc, $this->color);
			return True;
		}
		
		function draw_rectangle($x_start, $y_start, $x_end, $y_end, $style = 'filled')
		{
			if ($style == 'filled')
			{
				imagefilledrectangle ($this->object, $x_start, $y_start, $x_end, $y_end, $this->color);
			}
			return True;
		}

		function fill_border($x, $y)
		{
			ImageFillToBorder($this->object, $x, $y, $this->color, $this->color);
			return True;
		}

		function move_to($x, $y)
		{
			if ($x >= 0 && $x <= $this->width && $y >= 0 && $y <= $this->height)
			{
				$this->cur_x = $x;
				$this->cur_y = $y;

				return True;
			}
			return False;
		}

		function styled_line($x1, $y1, $x2, $y2, $style)
		{
			ImageSetStyle($this->object, $style);
			ImageLine($this->object, $x1, $y1, $x2, $y2, IMG_COLOR_STYLED);
			return True;
		}

		function line_to($x, $y, $linestyle = 'solid')
		{
			if ($x >= 0 && $x <= $this->width && $y >= 0 && $y <= $this->height)
			{
				if ($linestyle == 'dashed')
					ImageDashedLine($this->image, $this->cur_x, $this->cur_y, $x, $y, $this->color);
				else
					ImageLine($this->image, $this->cur_x, $this->cur_y, $x, $y, $this->color);

				$this->cur_x = $x;
				$this->cur_y = $y;

				return True;
			}
			return False;
		}

		function _line($x1, $y1, $x2, $y2, $linestyle = 'solid')
		{
			if ($x1 >= 0 && $x1 <= $this->width && $y1 >= 0 && $y1 <= $this->height && $x2 >= 0 && $x2 <= $this->width && $y2 >= 0 && $y2 <= $this->height)
			{
				if ($linestyle == 'solid')
				{
					ImageLine($this->image, $x1, $y1, $x2, $y2, $this->color);
				}
				else
				{
					ImageDashedLine($this->image, $x1, $y1, $x2, $y2, $this->color);
				}
				$this->cur_x = $x2;
				$this->cur_y = $y2;

				return True;
			}
			return False;
		}

		function circle1_object()
		{
			$this->set_color_by_name($this->ncolor);
			$this->draw_arc('20','20','40','40','0','360');
			$this->fill_border('20','20');
			$this->set_color_by_name($this->obj_color);
			$this->draw_arc('20','20','15','15','0','360');
			$this->fill_border('20','20');
			return True;
		}

		function circle3_object()
		{
			$coo = $this->get_coordinates();
			$this->set_color_by_name($this->ncolor);
			$this->draw_arc($coo['middle_x'],$coo['middle_y'],$coo['x'],$coo['y'],'0','360');
			$this->fill_border($coo['middle_x'],$coo['middle_y']);
			$this->set_color_by_name($this->obj_color);
			$this->draw_arc($coo['middle_x'],$coo['middle_y'],$coo['x'],$coo['middle_y'],'0','360');
			$this->fill_border($coo['middle_x'],$coo['middle_y']);
			$this->set_color_by_name($this->fg_color);
			$this->draw_arc($coo['middle_x'],$coo['middle_y'],$coo['fourth_x'],$coo['middle_y'],'0','360');
			$this->fill_border($coo['middle_x'],$coo['middle_y']);
			return True;
		}

		function square1_object()
		{
			$coo = $this->get_coordinates();
			$this->set_color_by_name($this->ncolor);
			$this->draw_rectangle('0','0',$coo['x'],$coo['y']);

			$this->set_color_by_name($this->obj_color);
			$this->draw_rectangle($coo['fourth_x'],'0',$coo['x']-$coo['fourth_x'],$coo['y']);

			$this->set_color_by_name($this->obj_color);
			$this->draw_rectangle('0',$coo['fourth_y'],$coo['x'],$coo['y']-$coo['fourth_y']);

			$this->set_color_by_name($this->fg_color);
			$this->draw_rectangle($coo['fourth_x'],$coo['fourth_y'],$coo['x']-$coo['fourth_x'],$coo['y']-$coo['fourth_y']);
			return True;
		}

		function poly1_object()
		{
			$coo = $this->get_coordinates();
			$this->set_color_by_name($this->obj_color);
			$points = array($coo['sixt_x'],$coo['fourth_y'],$coo['twelft_x'],$coo['y'],$coo['fourth_x'],$coo['fourth_y'],$coo['x'],$coo['twelft_y'],$coo['fourth_x'],$coo['sixt_y'],'0','0');
	//		$points = array('40','60','20','240','60','60','240','20','60','40','0','0');
			$this->draw_polygon($points,'6');
			return True;
		}

		function triangle1_object()
		{
			$coo = $this->get_coordinates();
			$this->set_color_by_name($this->ncolor);
			$points = array('0',$coo['y'],$coo['x'],$coo['y'],$coo['middle_x'],'0');
			$this->draw_polygon($points,'3');

			$this->set_color_by_name($this->obj_color);
			$points = array($coo['middle_x'],$coo['y'],$coo['x']-$coo['fourth_x'],$coo['middle_y'],$coo['fourth_x'],$coo['middle_y']);
			$this->draw_polygon($points,'3');

			$this->set_color_by_name($this->fg_color);
			$points = array($coo['fourth_x']+$coo['eight_x'],$coo['y']-$coo['fourth_y'],$coo['middle_x']+$coo['eight_x'],$coo['y']-$coo['fourth_y'],$coo['middle_x'],$coo['middle_y']);
			$this->draw_polygon($points,'3');
			return True;
		}

		function line1_object()
		{
			$coo = $this->get_coordinates();
			$this->set_color_by_name($this->ncolor,'nc');
			$this->set_color_by_name($this->obj_color,'ob');
			$this->set_color_by_name($this->fg_color,'fg');

			$this->styled_line('0','0',$coo['x'],$coo['y'],array($this->ncolor,$this->ncolor,$this->ncolor,$this->obj_color));
			$this->styled_line('0',$coo['third_y'],$coo['x'],$coo['third_y'],array($this->obj_color,$this->obj_color,$this->fg_color));
			$this->styled_line('0',$coo['y']-$coo['third_y'],$coo['x'],$coo['y']-$coo['third_y'],array($this->fg_color,$this->ncolor,$this->ncolor,$this->fg_color,$this->obj_color,$this->obj_color));
			return True;
		}

		function copy_image($dest_x, $dest_y)
		{
			ImageCopy($this->image, $this->object, $dest_x, $dest_y, '0', '0', $this->obj_width, $this->obj_height);
			return True;
		}

		function save_tmp()
		{
			ImagePNG($this->image,$this->pic->img_dir . 'draw_tmp.png');
		//	$this->pic->image2png($this->image,'draw_tmp.png');
		}

		function save_image($file_name)
		{
			$file_name = $this->pic->trim_name($file_name);
			$file_name = $file_name . '.png';

			return $this->pic->image2png($this->image,$file_name);
		}

		function object_to_browser()
		{
			if (!$this->type)
			{
				$this->type = 'png';
			}

			header('Content-type: image/' . $this->type);
			switch ($this->type)
			{
				case 'png':
					ImagePNG($this->object);
					break;
				case 'gif':
					ImageGIF($this->object);
					break;
				case 'jpeg':
					ImageJPEG($this->object);
					break;
			}
		}

		function image_to_browser()
		{
			if (!$this->type)
			{
				$this->type = 'png';
			}

			$show = header('Content-type: image/' . $this->type);
			switch ($this->type)
			{
				case 'png':
					$show .= ImagePNG($this->image);
					break;
				case 'gif':
					$show .= ImageGIF($this->image);
					break;
				case 'jpeg':
					$show .= ImageJPEG($this->image);
					break;
			}
			return $show;
		}

		function object_done()
		{
			ImageDestroy($this->object);
		}

		function image_done()
		{
			ImageDestroy($this->image);
		}

		function drop_tmp()
		{
			if (is_file($this->tmp_file))
			{
				unlink($this->tmp_file);
				return True;
			}
		}
	}
?>
Return current item: FUNKWERK