<?
include "header.php";
if(isset($_GET['page'])) { $page = $_GET['page']; } else { $page = 1; }
if(isset($_GET['r'])) { $r = $_GET['r']; } else { exit(); }
if(isset($_GET['i_id'])) { $i_id = $_GET['i_id']; } else { exit(); }
if(mysql_num_rows(mysql_query("SELECT * FROM ims_image WHERE i_id='$i_id' AND u_id='$user_info[u_id]'")) != 1) { exit(); }
$image_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_image WHERE i_id='$i_id' AND u_id='$user_info[u_id]'"));
if($image_info[ext] == "gif" | $image_info[ext] == "pdf") { exit(); }
if($admin_info[rotate_feature] != "yes") { exit(); }

$image_filename = "./u/$user_info[u_id]/$image_info[f_id]/$image_info[i_id].$image_info[ext]";
$temp_image_filename = "./temp/$image_info[i_id].$image_info[ext]";

copy($image_filename, $temp_image_filename);

if($image_info[ext] == "bmp") {

$temp_image_size = getimagesize($temp_image_filename);
  if($temp_image_size[0] > $temp_image_size[1]) {
  $new_dimensions = $temp_image_size[0];
  } else {
  $new_dimensions = $temp_image_size[1];
  }
$original = imagecreatefromjpeg($temp_image_filename);
$intermediate = imagecreatetruecolor($new_dimensions, $new_dimensions);
$final = imagecreatetruecolor($temp_image_size[1], $temp_image_size[0]);
imagecopyresampled($intermediate, $original, 0, 0, 0, 0, $temp_image_size[0], $temp_image_size[1], $temp_image_size[0], $temp_image_size[1]);
  if($r == 1) {
  $rotated = imagerotate($intermediate, 90, 0);
    if($temp_image_size[1] > $temp_image_size[0]) {
    imagecopyresampled($final, $rotated, 0, 0, 0, $temp_image_size[1]-$temp_image_size[0], $temp_image_size[1], $temp_image_size[0], $temp_image_size[1], $temp_image_size[0]);
    } else {
    imagecopyresampled($final, $rotated, 0, 0, 0, 0, $temp_image_size[1], $temp_image_size[0], $temp_image_size[1], $temp_image_size[0]);
    }
  } else {
  $rotated = imagerotate($intermediate, -90, 0);
    if($temp_image_size[0] > $temp_image_size[1]) {
    imagecopyresampled($final, $rotated, 0, 0, $temp_image_size[0]-$temp_image_size[1], 0, $temp_image_size[1], $temp_image_size[0], $temp_image_size[1], $temp_image_size[0]);
    } else {
    imagecopyresampled($final, $rotated, 0, 0, 0, 0, $temp_image_size[1], $temp_image_size[0], $temp_image_size[1], $temp_image_size[0]);
    }
  }

imagejpeg($final, $temp_image_filename);
ImageDestroy($original);
ImageDestroy($intermediate);
ImageDestroy($rotated);
ImageDestroy($final);



} elseif($image_info[ext] == "jpeg" | $image_info[ext] == "jpg") {

$temp_image_size = getimagesize($temp_image_filename);
  if($temp_image_size[0] > $temp_image_size[1]) {
  $new_dimensions = $temp_image_size[0];
  } else {
  $new_dimensions = $temp_image_size[1];
  }
$original = imagecreatefromjpeg($temp_image_filename);
$intermediate = imagecreatetruecolor($new_dimensions, $new_dimensions);
$final = imagecreatetruecolor($temp_image_size[1], $temp_image_size[0]);
imagecopyresampled($intermediate, $original, 0, 0, 0, 0, $temp_image_size[0], $temp_image_size[1], $temp_image_size[0], $temp_image_size[1]);
  if($r == 1) {
  $rotated = imagerotate($intermediate, 90, 0);
    if($temp_image_size[1] > $temp_image_size[0]) {
    imagecopyresampled($final, $rotated, 0, 0, 0, $temp_image_size[1]-$temp_image_size[0], $temp_image_size[1], $temp_image_size[0], $temp_image_size[1], $temp_image_size[0]);
    } else {
    imagecopyresampled($final, $rotated, 0, 0, 0, 0, $temp_image_size[1], $temp_image_size[0], $temp_image_size[1], $temp_image_size[0]);
    }
  } else {
  $rotated = imagerotate($intermediate, -90, 0);
    if($temp_image_size[0] > $temp_image_size[1]) {
    imagecopyresampled($final, $rotated, 0, 0, $temp_image_size[0]-$temp_image_size[1], 0, $temp_image_size[1], $temp_image_size[0], $temp_image_size[1], $temp_image_size[0]);
    } else {
    imagecopyresampled($final, $rotated, 0, 0, 0, 0, $temp_image_size[1], $temp_image_size[0], $temp_image_size[1], $temp_image_size[0]);
    }
  }

imagejpeg($final, $temp_image_filename);
ImageDestroy($original);
ImageDestroy($intermediate);
ImageDestroy($rotated);
ImageDestroy($final);

} elseif($image_info[ext] == "png") {

$temp_image_size = getimagesize($temp_image_filename);
  if($temp_image_size[0] > $temp_image_size[1]) {
  $new_dimensions = $temp_image_size[0];
  } else {
  $new_dimensions = $temp_image_size[1];
  }
$original = imagecreatefrompng($temp_image_filename);
$intermediate = imagecreatetruecolor($new_dimensions, $new_dimensions);
$final = imagecreatetruecolor($temp_image_size[1], $temp_image_size[0]);
imagecopyresampled($intermediate, $original, 0, 0, 0, 0, $temp_image_size[0], $temp_image_size[1], $temp_image_size[0], $temp_image_size[1]);
  if($r == 1) {
  $rotated = imagerotate($intermediate, 90, 0);
    if($temp_image_size[1] > $temp_image_size[0]) {
    imagecopyresampled($final, $rotated, 0, 0, 0, $temp_image_size[1]-$temp_image_size[0], $temp_image_size[1], $temp_image_size[0], $temp_image_size[1], $temp_image_size[0]);
    } else {
    imagecopyresampled($final, $rotated, 0, 0, 0, 0, $temp_image_size[1], $temp_image_size[0], $temp_image_size[1], $temp_image_size[0]);
    }
  } else {
  $rotated = imagerotate($intermediate, -90, 0);
    if($temp_image_size[0] > $temp_image_size[1]) {
    imagecopyresampled($final, $rotated, 0, 0, $temp_image_size[0]-$temp_image_size[1], 0, $temp_image_size[1], $temp_image_size[0], $temp_image_size[1], $temp_image_size[0]);
    } else {
    imagecopyresampled($final, $rotated, 0, 0, 0, 0, $temp_image_size[1], $temp_image_size[0], $temp_image_size[1], $temp_image_size[0]);
    }
  }

imagepng($final, $temp_image_filename);
ImageDestroy($original);
ImageDestroy($intermediate);
ImageDestroy($rotated);
ImageDestroy($final);

}

$space_used = mysql_fetch_assoc(mysql_query("SELECT sum(size) AS size FROM ims_image WHERE u_id='$user_info[u_id]'"));
if($space_used[size] == NULL) { $space_used[size] = 0; }
$space_avail = $user_info[space_avail] * (1024 * 1024);
$space_left = (int)$space_avail - (int)$space_used[size];

$temp_image_size = filesize($temp_image_filename);
if(($temp_image_size - filesize($image_filename)) >= $space_left) {
unlink($temp_image_filename);
echo $header;
echo "
<table cellpadding='0' cellspacing='0' width='520'>
<tr><td>
<b>An Error Has Occurred:</b><br>
You do not have enough space in your account.
<br>
</td></tr></table>
";
echo $footer;
exit();
}

if(round($temp_image_size/1024, 0) > $user_info[max_filesize]) {
unlink($temp_image_filename);
echo $header;
echo "
<table cellpadding='0' cellspacing='0' width='520'>
<tr><td>
<b>An Error Has Occurred:</b><br>
Your file could not be rotated because the new filesize 
exceeds the maximum as set by your administrator. You can use your browser's back button to return.
<br>
</td></tr></table>
";
echo $footer;
exit();
}


unlink($image_filename);
copy($temp_image_filename, $image_filename);
unlink($temp_image_filename);


if($image_info[ext] == "bmp") {
$thumb = imagecreatetruecolor(60, 60);
$new = imagecreatefromjpeg($image_filename);
  for($i=0; $i<256; $i++) {
  imagecolorallocate($thumb, $i, $i, $i);
  }
$size = getimagesize($image_filename);
imagecopyresized($thumb, $new, 0, 0, 0, 0, 60, 60, $size[0], $size[1]);
imagejpeg($thumb, "./u/$user_info[u_id]/$image_info[f_id]/thumbs/".$image_info[i_id].".".$image_info[ext]);
ImageDestroy($new);
ImageDestroy($thumb);


} elseif($image_info[ext] == "jpeg" | $image_info[ext] == "jpg") {

$thumb = imagecreatetruecolor(60, 60);
$new = imagecreatefromjpeg($image_filename);
  for($i=0; $i<256; $i++) {
  imagecolorallocate($thumb, $i, $i, $i);
  }
$size = getimagesize($image_filename);
imagecopyresampled($thumb, $new, 0, 0, 0, 0, 60, 60, $size[0], $size[1]);
imagejpeg($thumb, "./u/$user_info[u_id]/$image_info[f_id]/thumbs/".$image_info[i_id].".".$image_info[ext]);
ImageDestroy($new);
ImageDestroy($thumb);


} elseif($image_info[ext] == "png") {

$thumb = imagecreatetruecolor(60, 60);
$new = imagecreatefrompng($image_filename);
  for($i=0; $i<256; $i++) {
  imagecolorallocate($thumb, $i, $i, $i);
  }
$size = getimagesize($image_filename);
imagecopyresampled($thumb, $new, 0, 0, 0, 0, 60, 60, $size[0], $size[1]);
imagepng($thumb, "./u/$user_info[u_id]/$image_info[f_id]/thumbs/".$image_info[i_id].".".$image_info[ext]);
ImageDestroy($new);
ImageDestroy($thumb);
}

$image_size = filesize($image_filename);
mysql_query("UPDATE ims_image SET size='$image_size' WHERE i_id='$image_info[i_id]'");

header("Location: view_folder.php?f_id=$image_info[f_id]&page=$page");
exit();
?>