<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2005, J. Carlos Nieto <xiam@users.sourceforge.net>
	*	This program is Free Software.
	*
	*	@package	Gekko
	*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
	*	@author		J. Carlos Nieto <xiam@users.sf.net>
	*	@version	CVS: $Id:$
	*	@link		http://www.gekkoware.org
	*/

	if (!defined("IN-GEKKO")) die("Get a life!");

	Class Image {
		var $buff;
		var $size = Array("w" => 0, "h" => 0);
		var $info = Array();
		var $type;
		var $status;
		function Image($path) {
			if (file_exists($path)) {

				switch (file_extension($path)) {
					case "jpg": case "jpeg":
						$this->buff = imagecreatefromjpeg($path);
						$this->type = "jpg";
					break;
					case "png":
						$this->buff = imagecreatefrompng($path);
						$this->type = "png";
					break;
					case "bmp":
						$this->buff = imagecreatefromwbmp($path);
						$this->type = "wbmp";
					break;
					case "gif":
						$this->buff = imagecreatefromgif($path);
						$this->type = "png";
					break;
					default:
						$img->status = 0;
						return;
						//trigger_error("Unsupported/unknown image format! (".basename($path).")", E_USER_ERROR);
					break;
				}
				$img->status = 1;

				$tmp = getimagesize($path);

				list($this->size["w"], $this->size["h"]) = $tmp;

				$this->info["mime"] = $tmp["mime"];

				$this->path = $path;

			} else {
				trigger_error("Couldn't find image path!", E_USER_ERROR);
			}
		}
		function get_size() {
			return $this->size;
		}
		function get_mime() {
			if (isset($this->info["mime"])) {
				return $this->info["mime"];
			} else {
				switch ($this->type) {
					case "jpg": return "image/jpeg"; break;
					case "png": return "image/png"; break;
					case "bmp": return "image/bmp"; break;
					case "gif": return "image/gif"; break;
				}
			}
		}
		function resize($nw, $nh = null) {

			if (!$nh) {
				// calculating relative width and height, larger side
				if ($this->size["w"] > $nw || $this->size["h"] > $nw) {
					$scale = $nw/(($this->size["w"] > $this->size["h"]) ? $this->size["w"] : $this->size["h"]);
					$nw = round($this->size["w"]*$scale);
					$nh = round($this->size["h"]*$scale);
				} else {
					$nh = $nw;
				}
			}

			// checking for the better available function
			$gd_resize = (function_exists("imagecopyresampled") ? "imagecopyresampled" : "imagecopyresized");
			$gd_create = (function_exists("imagecreatetruecolor") ? "imagecreatetruecolor" : "imagecreate");

			// creating destination image buffer
			$nbuff = $gd_create($nw, $nh);

			// copying a resized image
			$gd_resize($nbuff, $this->buff, 0, 0, 0, 0, $nw, $nh, $this->size["w"], $this->size["h"]);

			// updating info
			$this->size["w"] = $nw;
			$this->size["h"] = $nh;

			// using the new buffer
			$this->buff = $nbuff;
		}
		function flush($destroy = true) {
			switch ($this->type) {
				case "jpg": imagejpeg($this->buff); break;
				case "png": imagepng($this->buff); break;
				case "wbmp": imagewbmp($this->buff); break;
				case "gif": imagegif($this->buff); break;
			}
			if ($destroy) imagedestroy($this->buff);
		}
		function save($file, $destroy = true) {
			switch ($this->type) {
				case "jpg": imagejpeg($this->buff, $file); break;
				case "png": imagepng($this->buff, $file); break;
				case "wbmp": imagewbmp($this->buff, $file); break;
				case "gif": imagegif($this->buff, $file); break;
			}
			if ($destroy) imagedestroy($this->buff);
		}
	}

	/*
		$thumb = mkthumbnail("/tmp/image.png", 150);
	*/
	function mkthumbnail($file, $size, $samefile = false, $unlink = true, $dest_dir = "data/thumbs/") {
		if (substr($dest_dir, -1) != "/")
			$dest_dir .= "/";

		@mkdir(GEKKO_ROOT.$dest_dir, 0777);
		@chmod(GEKKO_ROOT.$dest_dir, 0777);

		$save_to = $samefile ? $dest_dir.basename($file) : $dest_dir.allocate_basename(GEKKO_ROOT.$dest_dir, "_".basename($file));

		$abs_des = GEKKO_ROOT.$save_to;

		$img = new Image($file);


		// fixing size
		$img_size = $img->get_size();

		if ($img_size['w'] > $size || $img_size['h'] > $size) {
			$size = explode("x", $size);

			if (isset($size[1])) {
				$img->resize($size[0], $size[1]);
			} else {
				$img->resize($size[0]);
			}
		}

		$img->save($abs_des);

		@chmod($abs_des, 0777);
		@chmod($file, 0777);

		if ($unlink && !$samefile)
			@unlink($file);

		$thumb = Array(
			"width" => $img->size["w"],
			"height" => $img->size["h"],
			"url" => $save_to,
			"path" => $abs_des,
			"origin" => $file
		);

		return $thumb;

	}
?>