Location: PHPKode > projects > TVEz - Media Library > tvez-0.2.1/html/shared/Graph/Image.php
<?php

# -----------------------------------------------------------------------------
#
# The base class of all the graph images
#
# -----------------------------------------------------------------------------
#
# Copyright (C) 2004 Christian Eheim
#
# This file is part of TVEz (tvez.sourceforge.net).
#
# TVEz 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.
#
# TVEz 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 TVEz; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# -----------------------------------------------------------------------------
#
# Created on 02/01/2004 by Christian Eheim (hide@address.com)
#
# LAST MODIFIED:
# $Date: 2004/02/03 23:21:59 $
# $Revision: 1.3 $
# $Author: eheim $
#
# -----------------------------------------------------------------------------

class Image {
	var $_image;		// The image ID
	var $_rgb;			// The RGB color class

	var $_imageType;    // Type of the image ("png","gif","jpeg")

	var $_width;		// The size of the image
	var $_height;

	var $_bgColor;		// The background color of the image

	var $_title;		// The title of the plot (optional)
	var $_titleFont;
	var $_titleColor;

	var $_showBorder;	// Show/Hide the border
	var $_borderColor;	// The color of the border around the image
	var $_borderWidth;	// The width of the border around the image

#	var $_showShadow;	// Show/Hide the shadow around the image
#	var $_shadowColor;	// The color of the shadow around the image
#	var $_shadowWidth;	// The width of the shadow around the image

	var $_topMargin;	// The margin at the top of the image
	var $_rightMargin;	// The margin at the right of the image
	var $_bottomMargin;	// The margin at the bottom of the image
	var $_leftMargin;	// The margin at the left of the image


	var $_contentTop;		// The size of the content area
	var $_contentLeft;		// without image title, border, and margin
	var $_contentWidth;
	var $_contentHeight;

	var $_plotTop;			// The size of the plotting area
	var $_plotLeft;			// without legend, padding and margins
	var $_plotWidth;
	var $_plotHeight;

	var $_graphTop;			// The size of the graphing area
	var $_graphLeft;		// without the axis and labels
	var $_graphWidth;
	var $_graphHeight;


	# -------------------------------------------------------------------
	# Public member functions
	# -------------------------------------------------------------------
	# Constructor - Creates the image
	function Image($width,$height,$title=null) {

        # Determine the default image type
        $support = imagetypes();
        if ( $support & IMG_PNG )       $this->_imageType = "png";
        elseif ( $support & IMG_GIF )   $this->_imageType = "gif";
        elseif ( $support & IMG_JPG )   $this->_imageType = "jpeg";
        else die("No image support on this PHP server. Please install GD 1.5 or higher.");
                                                                                
		$this->_image 		= ImageCreate( $width, $height );
		$this->_rgb 		= new RGB($this->_image);

		$this->_width		= $width;
		$this->_height		= $height;
		$this->_bgColor		= "#ffffff";

		$this->set_title($title);
		$this->set_title_margin(4);
		$this->set_title_font(5);
		$this->set_title_color("#555555");

		$this->_showBorder	= true;
		$this->_borderColor = "black";
		$this->_borderWidth = 1;

#		$this->_showShadow	= true;
#		$this->_shadowColor = "#aaaaaa";
#		$this->_shadowWidth = 2;

		$this->_topMargin		= 2;
		$this->_rightMargin		= 1;
		$this->_bottomMargin	= 1;
		$this->_leftMargin		= 1;
	}
	# The image background and border
	function init() {
		$backgroundColor = $this->_rgb->allocate_color($this->_bgColor);
		$borderColor = $this->_rgb->allocate_color($this->_borderColor);

		if ($this->_showBorder) {
			ImageFill( $this->_image, 0, 0, $borderColor );
			ImageFilledRectangle(
				$this->_image,
				$this->_borderWidth, $this->_borderWidth,
				$this->_width-$this->_borderWidth-1,
				$this->_height-$this->_borderWidth-1,
				$backgroundColor
			);
		} else {
			ImageFill( $this->_image, 0, 0, $backgroundColor );
		}

		ImageInterlace( $this->_image, 1 );

		if ($this->_title)
			$this->print_title();
	
		$this->content_area();

	}
	function get_content_area() {
		$this->content_area();
		return array(
			$this->_contentLeft, $this->_contentTop,
			$this->_contentWidth, $this->_contentHeight
		);
	}
	function set_plot_area($left,$top,$width,$height) {
		$this->_plotLeft	= $left;
		$this->_plotTop		= $top;
		$this->_plotWidth	= $width;
		$this->_plotHeight	= $height;
	}
	function get_graph_area() {
		return array(
			$this->_graphLeft, $this->_graphTop,
			$this->_graphWidth, $this->_graphHeight
		);
	}
	function set_graph_area($left,$top,$width,$height) {
		$this->_graphLeft	= $left;
		$this->_graphTop	= $top;
		$this->_graphWidth	= $width;
		$this->_graphHeight	= $height;
	}
	function get_plot_area() {
		return array(
			$this->_plotLeft, $this->_plotTop,
			$this->_plotWidth, $this->_plotHeight
		);
	}
    function set_type($type) {
        $this->_imageType = strtolower($type);
    }
	function set_border($bool,$width=1,$color="#000000") {
		$this->_showBorder	= $bool;
		$this->_borderWidth	= $width;
		$this->_borderColor	= $color;
	}
	function set_bgcolor($color) {
		$this->_bgColor = $color;
	}
	function set_margin($top,$right,$bottom,$left) {
		$this->_topMargin		= $top;
		$this->_rightMargin		= $right;
		$this->_bottomMargin	= $bottom;
		$this->_leftMargin		= $left;
		$this->content_area();
	}
	function set_title($title) {
		if ($title)
			$this->_title	= new Text($title,3);
		else
			$this->_title	= null;
	}
	function set_title_color($color) {
		if ($this->_title)
			$this->_title->set_color($color);
	}
	function set_title_font($font) {
		if ($this->_title)
			$this->_title->set_font($font);
	}
	function set_title_margin($margin) {
		if ($this->_title)
			$this->_title->set_margin($margin);
	}
	function draw() {
		switch($this->_imageType) {
			case "gif":
				header("Content-type: image/gif");
				ImageGIF($this->_image);
				break;
			case "jpeg":
				header("Content-type: image/jpeg");
				ImageJPEG($this->_image, "", 0.5);
				break;
			default:
				header("Content-type: image/png");
				ImagePNG($this->_image);
				break;
		}

		# Free up image resources
		ImageDestroy($this->_image);
	}
	# -------------------------------------------------------------------
	# Private members
	# -------------------------------------------------------------------
	function print_title() {
		$title_x = round(
			(imagesx($this->_image) - $this->_title->get_width()) / 2
		);
		$this->_title->set_pos(
			$title_x,($this->_topMargin+$this->_borderWidth)
#			$title_x,($this->_borderWidth)
		);

		$this->_title->draw($this);
	}
	# Compute the size of the content area
	function content_area() {
		if ($this->_title) {
			$this->_contentTop		=
				$this->_borderWidth + $this->_topMargin
					+ $this->_title->get_height();
			$this->_contentHeight	=
				$this->_height - 2*$this->_borderWidth
				- $this->_bottomMargin - $this->_topMargin
				- $this->_title->get_height();
		} else {
			$this->_contentTop		= $this->_borderWidth + $this->_topMargin;
			$this->_contentHeight	=
				$this->_height - 2*$this->_borderWidth - $this->_bottomMargin
				- $this->_topMargin;
		}
		$this->_contentLeft	= $this->_borderWidth + $this->_leftMargin;
		$this->_contentWidth	=
			$this->_width - 2*$this->_borderWidth - $this->_rightMargin - $this->_leftMargin;
	}


}
Return current item: TVEz - Media Library