<?php
class auto_avatar_resizerTemplate {
	var $height;
	var $width;
	var $ratio;
	var $type;
	function auto_avatar_resizerTemplate($h, $w, $t, $a) {
		if ($a != "") {
			$this->height = (int) $a['tem_maxY'];
			$this->width = (int) $a['tem_maxX'];
			$this->type = (int) $a['tem_format'];
		} else {
			$this->height = $h;	
			$this->width = $w;	
			$this->ratio = $w / $h;	
			$this->type = $t;	
		}
	}
	function getTemplate($templateID) {
		global $db_prefix;
		$a = dbarray(dbquery("SELECT * FROM ".$db_prefix."auto_avatar_resizer_templates WHERE tem_id=".$templateID));
		$this->height = (int) $a['tem_maxY'];
		$this->width = (int) $a['tem_maxX'];
		$this->type = (int) $a['tem_format'];
	}
}
class auto_avatar_resizerImage {
	var $width;
	var $height;
	var $imageType;
	var $file_location;
	var $real_name;
	var $ok;
	var $path;
	var $ratio;
	var $resized_location;
	
	function auto_avatar_resizerImage ($l, $n) {
		

$this->path = "".BASEDIR."images/avatars/";

		list($w, $h, $t, $attr) = getimagesize($l);
		switch ($t) {
			case IMAGETYPE_GIF:
				$this->ok = true;
				break;
			case IMAGETYPE_JPEG:
				$this->ok = true;
				break;
			case IMAGETYPE_PNG:
				$this->ok = true;
				break;
			default:
				$this->ok = false;
		}
		$this->width = $w;
		$this->height = $h;
		$this->imageType = $t;
		$this->file_location = $l;
		$this->real_name = $n;
		$this->ratio = (float) ($w / $h);
		//echo "### Object up - $w, $h, $t, $l, $n, ".$this->ratio.", ".$this->dimensions;
	}
	function dbAdd () {
		global $userdata, $db_prefix;
		if (dbrows(dbquery("SELECT * FROM ".$db_prefix."auto_avatar_resizer_images WHERE img_file='".$this->resized_location."'")) > 0) {
			dbquery("DELETE FROM ".$db_prefix."auto_avatar_resizer_images WHERE img_file='".$this->resized_location."'");
		}
	       
$sql = "UPDATE ".$db_prefix."users SET user_avatar='".$this->resized_location."' WHERE user_id='".$userdata['user_id']."'";

		$result = dbquery($sql);
		$id = dbarray(dbquery("SELECT LAST_INSERT_ID() as theID FROM ".$db_prefix."auto_avatar_resizer_images"));
		$id = (int) $id['theID'];
		return $id;
	}
	
	function deleteOldImages() {
		global $db_prefix, $resize_settings;
		$sql = "SELECT * FROM ".$db_prefix."auto_avatar_resizer_images WHERE DATE_ADD(img_resizeddate, INTERVAL ".$resize_settings['buffer_length']." MINUTE) < NOW()";
		$result = dbquery($sql);
		while ($data = dbarray($result)) {
			$delsql = "DELETE FROM ".$db_prefix."auto_avatar_resizer_images WHERE img_id=".$data['img_id'];
			$delresult = dbquery($delsql);
			unlink($data['img_file']);
		}
	}
	function getResized ($maxX, $maxY, $outExt, $quality) {
		global $userdata;
		if ($this->file_location == "") { return "error"; }
		if ($outExt == "") {
			$outExt = $this->imageType;
		}
		$r = $this->ratio;
		
		if ($this->width > $this->height && $maxX > $maxY) {
			$X = $maxX;
			$Y = $maxX / $r;
		} else if ($this->width > $this->height && $maxX < $maxY) {
			$X = $maxX;
			$Y = $maxX / $r;
		} else if ($this->width > $this->height && $maxX == $maxY) {
			$X = $maxX;
			$Y = $maxX / $r;
		} else if ($this->width < $this->height && $maxX > $maxY) {
			$Y = $maxY;
			$X = $Y * $r;
		} else if ($this->width < $this->height && $maxX < $maxY) {
			$X = $maxX;
			$Y = $X / $r;
		} else if ($this->width < $this->height && $maxX == $maxY) {
			$Y = $maxY;
			$X = $Y * $r;
		} else if ($this->width == $this->height && $maxX > $maxY) {
			$X = $maxY;
			$Y = $maxY;			
		} else if ($this->width == $this->height && $maxX < $maxY) {
			$X = $maxX;
			$Y = $maxX;
		} else if ($this->width == $this->height && $maxX == $maxY) {
			$X = $maxX;
			$Y = $maxY;			
		} else {
			// ERROR?
		}
		
		$X = (int) $X;
		$Y = (int) $Y;
		//echo "<br />This image = ".$this->width."x".$this->height;
		//echo "<br />This template = ".$maxX."x".$maxY;
		//echo "<br />Ratio is = ".$r;
		//echo "<br />That makes = ".$X."x".$Y;
		$source = false;
		switch ($this->imageType) {
			case IMAGETYPE_GIF:
				$source = imagecreatefromgif($this->file_location);
				$oldType = ".gif";
				break;
			case IMAGETYPE_PNG:
				$source = imagecreatefrompng($this->file_location);
				$oldType = ".png";
				break;
			case IMAGETYPE_JPEG:
				$source = imagecreatefromjpeg($this->file_location);
				$oldType = ".jpg";
				break;
			default:
				$source = false;
		}
		
		if ($source != false) {
			$image = imagecreatetruecolor($X, $Y);
			imagecopyresampled($image, $source, 0, 0, 0, 0, $X, $Y, $this->width, $this->height);
			$newname = $this->path."[".$userdata['user_id']."]".$this->real_name;
			//echo "Image: Gif ".IMAGETYPE_GIF." - Jpeg ".IMAGETYPE_JPEG." - Png ".IMAGETYPE_PNG." - selected : ".$outExt;
			//echo $newname;
			//echo $userdata['user_name']." - ".$userdata['user_name']." - ".$userdata['user_name']." - ".$userdata['user_name'];
			//echo "asdasdasdlkjasdlkj afdlk jsda lfjagf lkjafgl kjdfg lkjflkjdgø jdfg lafø gg algøfgjkølkj ølk";
			switch ($outExt) {
				case IMAGETYPE_GIF:
					//echo "<br />gif";
					
					$newname = str_replace($oldType, ".gif", $newname);
					imagegif($image, $newname);
					break;
				case IMAGETYPE_PNG:
					//echo "<br />png";
					$newname = str_replace($oldType, ".png", $newname);
					imagepng($image, $newname);
					break;
				case IMAGETYPE_JPEG:
					//echo "<br />jpeg";
					$newname = str_replace($oldType, ".jpg", $newname);
					imagejpeg($image, $newname, $quality);
					break;
				default:
					break;
			}
			$this->resized_location = $newname;
			return $newname;
		} else {
			return "error";
		}
	}
}
?>