<?php
include ('upload_class.php');

class Foto_upload extends file_upload {
	
	var $x_size;
	var $y_size;
	var $thumb_folder;
	var $foto_folder;
	
	function process_image() {
		$filename = $this->upload_dir.$this->file_copy;
		$thumb = $this->thumb_folder.$this->file_copy;
		$foto = $this->foto_folder.$this->file_copy;
		$this->get_img_size($filename);
		if ($this->y_size > $this->x_size) {
			$this->img_rotate($filename);
			$this->get_img_size($filename); // repeat this because the img is changed
		}
		if ($this->x_size > 640) {
			$this->thumbs($filename, $foto, 640, 80);
		}
		$this->thumbs($filename, $thumb, 240, 85);
		$this->del_temp_file($filename);
	}
	function get_img_size($file) {
		$img_size = getimagesize($file);
		$this->x_size = $img_size[0];
		$this->y_size = $img_size[1];
	}
	function img_rotate($wr_file) {
		$new_x = $this->y_size;
		$new_y = $this->x_size;
		$src_img = imagecreatefromjpeg($wr_file);
		$rot_img = imagerotate($src_img, 90, 0);
		$new_img = imagecreatetruecolor($new_x, $new_y);
		imageantialias($new_img, TRUE);
		imagecopyresampled($new_img, $rot_img, 0, 0, 0, 0, $new_x, $new_y, $new_x, $new_y);
		imagejpeg($new_img, $this->upload_dir.$this->file_copy, 85);
	}
	function thumbs($file_name_src, $file_name_dest, $weight, $quality=80) {
		$size = getimagesize($file_name_src);
		$w = number_format($weight, 0, ',', '');
		$h = number_format(($size[1]/$size[0])*$weight,0,',','');
		$dest = imagecreatetruecolor($w, $h);
		imageantialias($dest, TRUE);
		$src = imagecreatefromjpeg($file_name_src);
		imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
		imagejpeg($dest, $file_name_dest, $quality);
	}
	function del_temp_file($file) {
		$delete = @unlink($file); 
		clearstatcache();
		if (@file_exists($file)) { 
			$filesys = eregi_replace("/","\\",$file); 
			$delete = @system("del $filesys");
			clearstatcache();
			if (@file_exists($file)) { 
				$delete = @chmod ($file, 0775); 
				$delete = @unlink($file); 
				$delete = @system("del $filesys");
			}
		}
	}
}

$DOCUMENT_ROOT = '.'; 
$max_size = 1024*1536; // the max. size for uploading
define("MAX_SIZE", $max_size);
$foto_upload = new Foto_upload;

$foto_upload->upload_dir = $DOCUMENT_ROOT."/files/tmp/"; // "files" is the folder for the uploaded files (you have to create these folder)
$foto_upload->thumb_folder = $DOCUMENT_ROOT."/files/thumbs/";
$foto_upload->foto_folder = $DOCUMENT_ROOT."/files/fotos/";
$foto_upload->extensions = array(".jpg"); // specify the allowed extension(s) here
		
if (isset($_POST['Submit']) && $_POST['Submit'] == "Upload") {
	$foto_upload->the_temp_file = $_FILES['upload']['tmp_name'];
	$foto_upload->the_file = $_FILES['upload']['name'];
	$foto_upload->http_error = $_FILES['upload']['error'];
	$foto_upload->rename_file = true;
	$foto_upload->replace = "n";
	$foto_upload->do_filename_check = "n"; 
	if ($foto_upload->upload()) {
		$foto_upload->process_image();
	}
}
$error = $foto_upload->show_error_string();
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Photo-upload form</title>

<style type="text/css">
<!--
body {
	text-align:center;
}
label {
	margin:0;
	float:left;
	display:block;
	width:80px;
}
#main {
	width:350px;
	margin:0 auto;
	padding:20px 0;
	text-align:left;
}
-->
</style>
</head>
<body>
<div id="main">
  <h1>Photo-upload form</h1>
  <form action="<?php echo $PHP_SELF; ?>" method="post" enctype="multipart/form-data" name="form">
	<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_size; ?>"><br>
	<label for="upload">The photo:</label>
	<input type="file" name="upload" id="upload" size="20"><br>
	<input type="submit" name="Submit" id="Submit" value="Upload">
  </form>
  <p><?php echo $error; ?></p>
</div>  
</body>
</html>