<?php
/*
osCommRes, Services Online 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare

Released under the GNU General Public License 
*/
if(!defined('IN_CB'))die('You are not allowed to access to this page.');

/**
 * Holds the drawing $im and can also holds all forms and texts
 * You must call init().
 * You can use get_im() to add other kind of form not held into these classes.
 */
class FDrawing {
	var $w, $h;		// int
	var $color;		// Fcolor
	var $filename;	// char *
	var $im;		// {object}
	var $barcode = array();// BarCode *

	/**
	 * Constructor
	 *
	 * @param int $w
	 * @param int $h
	 * @param string filename
	 * @param FColor $color
	 */
	function FDrawing($w,$h,$filename,Fcolor $color) {
		$this->w = $w;
		$this->h = $h;
		$this->filename = $filename;
		$this->color = $color;
	}

	/**
	 * Destructor
	 */
	/*function __destruct() {
		$this->destroy();
	}*/

	/**
	 * Init Image and color background
	 */
	function init(){
		$this->im = imagecreate($this->w, $this->h)
		or die('Can\'t Initialize the GD Libraty');
		imagecolorallocate($this->im,$this->color->r(),$this->color->g(),$this->color->b());
	}

	/**
	 * @return ressource
	 */
	function get_im() {
		return $this->im;
	}

	/**
	 * @param ressource $im
	 */
	function set_im($im) {
		$this->im = $im;
	}

	/**
	 * Add barcode into the drawing array (for future drawing)
	 *
	 * @param BarCode $barcode
	 */
	function add_barcode(BarCode $barcode) {
		$this->barcode[] = $barcode;
	}

	/**
	 * Draw first all forms and after all texts on $im
	 */
	function draw_all() {
		for($i=0;$i<count($this->barcode);$i++)
			$this->barcode[$i]->draw($this->im);
	}

	/**
	 * Save $im into the file (many format available)
	 *
	 * @param int $image_style
	 * @param int $quality
	 */
	function finish($image_style=IMG_FORMAT_PNG,$quality=100) {
		$temp=$this->rotateImage($this->im);
		if($image_style==constant('IMG_FORMAT_PNG')){
			if(empty($this->filename))
				imagepng($temp);
			else
				imagepng($temp,$this->filename);
		}
		elseif($image_style==constant('IMG_FORMAT_JPEG'))
			imagejpeg($temp,$this->filename,$quality);
	}

	/**
	 * Free the memory of PHP (called also by destructor)
	 */
	function destroy() {
		imagedestroy($this->im);
	}
	
	function &rotateImage(&$in)
	{
			$w=imagesx($in);
			$h=imagesy($in);
		   $size = ($w > $h ? $w : $h);
		   // Create a square image the size of the largest side of our src image
		   if (($tmp = imageCreateTrueColor($size, $size)) == false) {
			   echo "Failed create square trueColor";
			   return $src;
		   }
		   // Exchange sides
		   if (($out = imageCreateTrueColor($h, $w)) == false) {
			   echo "Failed create trueColor ";
			   return $src;
		   }
		   // Now copy our src image to tmp where we will rotate and then copy that to $out
		   imageCopy($tmp, $in, 0, 0, 0, 0, $w, $h);
		   $tmp2 = imageRotate($tmp, 90, 0);
		   // Now copy tmp2 to $out;
		   imageCopy($out, $tmp2, 0, 0, 0, 0, $h, $w);
		   imageDestroy($tmp);
		   imageDestroy($tmp2);
		   return $out;
	}
};
?>