<?php
/* jefrey.sobreira@gmail.com */
class imgtool {
	var $file,$format,$width,$height,$saveto;
	var $convert = false, $resize = false;
	
	public function upload($pfile, $psaveto, $prandom = true) {
		$getfile = $pfile['tmp_name'];
		if(!$getfile) return -1; // no pic uploaded
		$ext = strtolower($end = end($sep = explode(".", $pfile['name'])));
		if($ext=="jpeg") $ext = "jpg";

		if($ext!="jpg" && $ext!="png" && $ext!="gif" && $ext!="bmp") {
			return -2; // invalid extension
		}
		
		if(is_dir($psaveto)) {
			if(substr($psaveto, -1, 1)=="/") $psaveto = substr($psaveto, 0, strlen($psaveto)-1);
			if($prandom) $psaveto = $psaveto . "/".uniqid().".".$ext;
			else $psaveto = $psaveto . "/".$pfile['name'];
		}
		
		move_uploaded_file($getfile, $psaveto.$ext);
	}
	
	function setinput($pfile) {
		if(is_file($pfile)) $this->file = $pfile;
		else trigger_error("$pfile is not a file");
	}
	
	function setoutput($poutput) {
		$this->saveto = $poutput;
	}
	
	function convert($pformat = 'jpg') {
		$this->convert = true;
		if(preg_match("/^(jpg|png|gif|bmp)$/", $pformat)) $this->format = $pformat;
		else trigger_error("Only jpg, png, gif or bmp allowed");
	}
	
	function resize($pwidth, $pheight) {
		$this->resize = true;
		$this->width = $pwidth;
		$this->height = $pheight;
	}
	
	function save() {
		$gd = $this->__workresize();
		$tmp = tempnam(sys_get_temp_dir(), "igt").$this->__format();
		$this->__image($gd, $tmp);
		if($this->__filename()==$this->file) unlink($this->file);
		rename($tmp, $this->__filename());
	}
	
	function show() {
		$gd = $this->__workresize();
		$this->__headers();
		$this->__image($gd);
	}
	
	private function __workresize() {
		$ext = end($arr = explode(".", $this->file));
		switch($ext) {
			case "png":
				$img = imagecreatefrompng($this->file);
				break;
				
			case "bmp":
				$img = imagecreatefromwbmp($this->file);
				break;
				
			case "gif":
				$img = imagecreatefromgif($this->file);
				break;
				
			case "jpg":
				$img = imagecreatefromjpeg($this->file);
				break;
				
			default:
				trigger_error("Unsupported format: $ext");
				break;
		}
		
		if($this->resize) {
			list($source_image_width, $source_image_height, $source_image_type) = getimagesize($this->file);
			
			$source_aspect_ratio = $source_image_width / $source_image_height;
			$thumbnail_aspect_ratio = $this->width / $this->height;
			if ($source_image_width <= $this->width && $source_image_height <= $this->height) {
			$thumbnail_image_width = $source_image_width;
			$thumbnail_image_height = $source_image_height;
			} elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
			$thumbnail_image_width = (int) ($this->height * $source_aspect_ratio);
			$thumbnail_image_height = $this->height;
			} else {
			$thumbnail_image_width = $this->width;
			$thumbnail_image_height = (int) ($this->width / $source_aspect_ratio);
			}
			
			$img2 = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);
			imagecopyresampled($img2, $img, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);
			imagedestroy($img);
			return $img2;
		} else return $img;
	}
	
	private function __format() {
		if($this->convert) return $this->format;
		else return end($arr = explode(".", $this->file));
	}
	
	private function __filename() {
		if($this->convert) {
			if($this->saveto) return $this->saveto;
			else {
				$ext = end($arr = explode(".", $this->file));
				return str_replace(".$ext", ".".$this->__format(), $this->file);
			}
		} else {
			if($this->saveto) return $this->saveto;
			else return $this->file;
		}
	}
	
	private function __image($in, $out = null, $quality=90) {
		switch($this->__format()) {
			case "jpg":
				return imagejpeg($in, $out, $quality);
				break;
				
			case "png":
				return imagepng($in, $out);
				break;
				
			case "gif":
				return imagegif($in, $out, $quality);
				break;
				
			case "bmp":
				return imagebmp($in, $out, $quality);
				break;
		}
	}
	
	private function __headers() {
		header("Content-type: image/".$this->__format());
	}
}