Location: PHPKode > scripts > Transparent Image > transparent-image/transparentImage.class.php
<?php

# creatd by Ilayaraja M hide@address.com

class TransparentImage {
	var $filename; 				#the filename of the image
	var $width 	= 120; 			#the final width of your icon
	var $height = 120;			#the final height of the icon
	var $parts  = array(); 			#the different images that will be superimposed on top of each other

	/***************************************************************************************************************************
	SET WIDTH
	This function sets the final width of our avatar icon. 
	**************************************************************************************************************************/
	function set_width($width) {
		$this->width  = $width;
	}

	/***************************************************************************************************************************
	SET HEIGHT
	This function sets the final height of our avatar icon.
	**************************************************************************************************************************/
	function set_height($height) {
		$this->height  = $height;
	}
	
	/***************************************************************************************************************************
	SET FILENAME
	This sets the final filename of our icon. We set this variable if we want to save the icon to the hard drive.
	**************************************************************************************************************************/
	function set_filename($filename) {
		$this->filename = $filename;
	}

	/***************************************************************************************************************************
	ADD LAYER
	This function let's us add images to our final composition
	**************************************************************************************************************************/
	function add_layer($filename) {
		$this->parts[] = $filename;
	}

	/***************************************************************************************************************************
	BUILD COMPOSITION
	This function compiles all the information and builds the image
	**************************************************************************************************************************/
	function build_composition() {
		#----------------------------------------
		# The Canvas
		# Creating the canvas for the final image, by default the canvas is black
		#----------------------------------------
		$this->canvas = imagecreatetruecolor($this->width, $this->height);

		# create background color with transparency
		$white = imagecolorallocatealpha ($this->canvas, 255, 255, 255, 127);

		# fill the transparency background image with the image
		imagefill($this->canvas, 0, 0, $white);

		#making sure to preserve the alpha info
		imagesavealpha( $this->canvas, true );

		#----------------------------------------
		# Adding the body parts
		# Here we go, the best part
		#----------------------------------------
		#looping through the array of parts
		for($i=0; $i<count($this->parts); $i++) {
			#getting the width and height of the body part image, (should be the same size as the canvas)
			list($part_w, $part_h) = getimagesize($this->parts[$i]);

			#storing that image into memory (make sure it's a png image)
			$body_part = imagecreatefrompng($this->parts[$i]);

			#making sure that alpha blending is enabled
			imageAlphaBlending($body_part, true);

			#finally, putting that image on top of our canvas
			imagecopyresampled($this->canvas, $body_part, 0, 0, 0, 0, $this->width, $this->height, $part_w, $part_h);

		}

	}

	/**************************************************************************************************************************
	OUTPUT
	This function checks to see if we're going to ouput to the header or to a file
	**************************************************************************************************************************/
	function output() {
		# If the filename is set, save it to a file
		if(!empty($this->filename))
		{
			#notice that this function has the added $this->filename value (by setting this you are saving it to the hard drive)
			imagepng($this->canvas, $this->filename,9);

		#Otherwise output to the header
		}else{
			#before you can output to the header, you must tell the browser to interpret this document
			#as an image (specifically a jpeg image)
			header("content-type: image/png");

			#Output, notice that I ommitted $this->filename
			imagepng($this->canvas);
		}
		#Removes the image from the buffer and frees up memory
		imagedestroy($this->canvas);
	}

	/***************************************************************************************************************************
	BUILD
	The function, this builds the image and outputs it
	**************************************************************************************************************************/
	function build() {
		#builds the image
		$this->build_composition();

		#outputs the image
		$this->output();

	}
}
Return current item: Transparent Image